day7: 类模版
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#ifndef __MYPOINT_H__
|
||||
#define __MYPOINT_H__
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
class Point
|
||||
{
|
||||
private:
|
||||
T x, y;
|
||||
|
||||
public:
|
||||
Point(T x, T y);
|
||||
void show();
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Point<T>::Point(T x, T y) : x(x), y(y)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Point<T>::show()
|
||||
{
|
||||
cout << "x = " << x << ", y = " << y << endl;
|
||||
}
|
||||
|
||||
#endif // __MYPOINT_H__
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "MyPoint.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "p1: ";
|
||||
Point<int> p1(10, 20);
|
||||
p1.show();
|
||||
|
||||
cout << "p2: ";
|
||||
Point<float> p2(10.2, 14.5);
|
||||
p2.show();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user