-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOperatorOverloadingInheritence.cpp
80 lines (54 loc) · 1.21 KB
/
OperatorOverloadingInheritence.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
//program to overload = operator
//Special thing about = operator is that the derived class cannot overload = operator function
using namespace std;
class Marks {
public:
int mark;
Marks() {
cout<<"parent class constructor called"<<endl;
}
Marks(int m) {
mark = m;
}
void getmarks() {
cout<<"The marks are: "<<mark<<endl;
}
Marks operator ++ () {
mark += 1;
cout<<"Overloading ++ operator in Prefix form"<<endl;
return *this;
}
};
class Math : public Marks {
public:
Math(int m)
{
mark =m;
}
//overloading -- operator function in derived class in POSTFIX FORM
Math operator -- (int)
{
Math duplicate(*this);
mark -= 1;
cout<<"Overloading -- operator in POSTFIX form"<<endl;
return duplicate;
}
};
int main() {
// Marks m1(90);
//
// m1.getmarks(); //prints 90
//
// //overloading = operator
// Marks m2 = m1 =85;
//
// m2.getmarks();
Math m1(89);
m1.getmarks();
(++m1).getmarks(); //marks are incremented first;
(m1--).getmarks();//marks still 90, first printed
//now marks are decremented after usage
m1.getmarks();//now marks have decremented by 1
return 0;
}