day7: 类模版

This commit is contained in:
2023-08-01 18:51:53 +08:00
parent 08a390b0b1
commit aae9aba049
19 changed files with 849 additions and 0 deletions
+30
View File
@@ -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__
+14
View File
@@ -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;
}