Skip to content

Commit 9b81f8e

Browse files
committed
program to demonstrate overloading () operator
1 parent 0c54cbf commit 9b81f8e

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
//overloading ()-function call operator
6+
//one special thing about overloading () operator function is that,only this operator can take default arguments ,no other operator can take default arguments
7+
8+
class Marks {
9+
10+
public:
11+
int mark;
12+
Marks(int m) {
13+
mark = m;
14+
}
15+
16+
17+
//operator function
18+
Marks operator () (int m=20) {
19+
20+
cout<<"Operator function called"<<endl;
21+
mark=m;
22+
23+
return *this; //this operator fuction returns the current address of the object
24+
}
25+
26+
27+
};
28+
29+
int main () {
30+
31+
Marks m1(90);
32+
cout<<m1.mark; //prints 90
33+
34+
cout<<"\n";
35+
m1(); //operator function called
36+
37+
cout<<m1.mark; //now marks is 20
38+
return 0;
39+
}

constructors.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,32 @@ class human
3232

3333
}
3434

35-
private:
35+
3636
int age;
3737
string name;
3838

3939

4040
};
4141

42+
class anish : private human {
43+
public:
44+
human::age;
45+
human::name;
46+
anish () {
47+
cout<<"Anish constructor called"<<endl;
48+
}
49+
void intro(int sage,string sname) {
50+
age = sage;
51+
name = sname;
52+
53+
cout<<"Hello I am "<< " " << sname << " " <<"age is :"<< sage <<endl;
54+
}
55+
};
56+
4257

4358
int main() {
44-
human *obj= new human;//constructor is called as soon as an object is created
45-
obj->introduce();
59+
// human *obj= new human;//constructor is called as soon as an object is created
60+
// obj->introduce();
61+
anish A;
62+
A.intro(20,"anish");
4663
}

constructors.exe

2.41 KB
Binary file not shown.

0 commit comments

Comments
 (0)