-
Notifications
You must be signed in to change notification settings - Fork 17
/
Template_class.cpp
64 lines (55 loc) · 1.02 KB
/
Template_class.cpp
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
#include<conio.h>
#include<iostream>
using namespace std;
template<class T>
class calculator{
private:
T num1,num2;
public:
calculator(T n1,T n2){
num=n1;
num=n2;
}
void displayResult(){
cout<<"numbers are:"<<num1<<"and"<<num2<<"."<<endl;
cout<<"addition is:"<<add()<<endl;
cout<<"subtraction is:"<<subtract()<<endl;
cout<<"multiply is:"<<multiply()<<endl;
cout<<"division is:"<<divide()<<endl;
}
T add()
{return num1+num2;
}
T subtract()
{return num1-num2;
}
T multiply()
{return num1*num2;
}
T divide()
{return num1/num2;
}
};
int main(){
calculator <int> calcInt(10,5);
cout<<"Int results:"<<endl;
calcInt.displayResult();
calculator <float> calcFloat(8.8,2.2);
cout<<endl<<"float results:"<<endl;
calcFloat.displayResult();
return0;
}
output
------
int result:
numbers are:10 and 5
addition is : 15
subtraction is : 5
product is : 50
division is : 2
float result:
numbers are: 8.8 and 2.2
addition is : 11
subtraction is : 6.6
product is : 19.36
division is : 4