-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_12.cpp
More file actions
88 lines (78 loc) · 1.63 KB
/
file_12.cpp
File metadata and controls
88 lines (78 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;
template<class T1, class T2> // typename ~=~ class
T1 sum(T1 a, T2 b){
return a + b;
}
class Point {
public:
Point(){
x_ = y_ = z_ = 0;
};
int getX(){
return x_;
}
private:
int x_;
int y_;
int z_;
};
template<class T>
class TypeSize{
public:
TypeSize(T value){
value_ = value;
}
/*T getValue(){
return value_;
}
void setValue(T value){
value_ = value;
}*/
void dataTypeSize(){
cout << sizeof(value_) << endl;
}
protected:
T value_;
};
template <class T>
class TypeInfo : public TypeSize <T>{
public:
TypeInfo(T value) : TypeSize <T> (value){}
void showTypeName(){
cout << "Type name is " << typeid(this->value_).name() << endl;
}
};
template <class T>
class Printer{
public:
void print(T value){
cout << value << endl;
}
};
template<>
class Printer<string>{
public:
void print(string value){
cout << "_" << value << "_" << endl;
}
};
int main(){
cout << sum(1,2) << endl;
cout << sum(1.5,2.6) << endl;
string s1 = "abc";
string s2 = "ABC";
cout << sum(s1,s2) << endl;
cout << sum(1.5,2) << endl;
Point a;
TypeInfo<Point> mc(a);
mc.dataTypeSize();
mc.showTypeName();
//mc.setValue(4.7);
//cout << mc.getValue() << endl;
Printer<int> printer;
printer.print(5);
Printer<string> printer1;
printer1.print("Hello!!!");
return 0;
}