-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOperatorOverloading1.cpp
55 lines (36 loc) · 967 Bytes
/
OperatorOverloading1.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
#include<iostream>
using namespace std;
//operator overlaoding example
class marks {
public:
marks() {
intmark=0;
extmark=0;
}
marks(int im,int em){
intmark=im;
extmark=em;
}
int intmark;
int extmark;
void display();
//defining a operator function which return an object of type class marks
//overloading the + operator method which takes argument as a object and returns an obj
//now here I can overload any operator and inside the function implement any opeartion using any operator
//let's overload '=' operator
marks operator+(marks m) {
marks temp; // a temperarory object
temp.intmark= intmark + m.intmark;
temp.extmark = extmark + m.extmark;
return temp;
}
};
void marks::display() {
cout<<"Internal Marks are: "<<intmark<<endl<<"external marks are: "<<extmark<<endl;
}
int main () {
marks m1(89,60),m2(15,55);
marks m = m1 + m2;//'=' overloaded
m.display();
return 0;
}