Skip to content

Commit d2d32fd

Browse files
authored
Merge pull request #3 from satya200/develop
Added changes for operator overloading
2 parents 6c2198d + 40ab730 commit d2d32fd

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

cpp_note

+53-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ https://www.youtube.com/watch?v=apdzg2A90CI&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IU
1818

1919
https://www.youtube.com/watch?v=gh51t49tKUQ&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=33
2020

21-
lecture goibg to start: 9, 10, 12, 14, 16, 21, 23, 25, 29, 32
21+
https://www.youtube.com/watch?v=DOhMUTHrdJI&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=36
22+
23+
lecture going to start: 9, 10, 12, 14, 16, 21, 23, 25, 29, 32, 35
2224

2325
//////////////////////////////////////////////////////////////////////////////////////////
2426

@@ -189,4 +191,53 @@ Static Members:
189191
iv> it will access using class name :: operator.
190192
v> Can not declare as const becz it does not have this pointer.
191193

192-
4>
194+
195+
Friend Function:
196+
-----------------
197+
1> A friend function of a class has access to private and protected data member of class.
198+
199+
2> Must have to include prototype inside the scope of class mention key word friend.
200+
201+
3> This feature required when i want to access 2 or more different classes private data member.
202+
203+
Operator Overloading For User define:
204+
--------------------------------------
205+
1> Post increment will use retun by value.
206+
Basic implementation:
207+
Myclass operator++(int)
208+
{
209+
Myclass t(data);// Here data has to be assign to temp varibale.
210+
// Any opeartion //
211+
return t;
212+
}
213+
214+
2> Pre increment is retun by reference and ii always return same object means this pointer
215+
Basic implementation:
216+
Myclass& operator++()
217+
{
218+
// Any Operation //
219+
return *this;// Here this has to be return this pointer
220+
}
221+
222+
3> explicit is use befpre member function to inform compiler to not to do any casting.
223+
224+
Overloading of cout and cin.
225+
-----------------------------
226+
1>
227+
Complex d1, d2;
228+
cout << d1 << d2;
229+
230+
1>The signature of operator global function
231+
ostream& operator<< (ostream& os, const Complex &a);
232+
233+
2>Member function in ostream
234+
ostream& ostream::operator<< (const complex &a);
235+
236+
3>Member function in Complex
237+
ostream& Complex::operator<< (ostream& os);
238+
239+
https://www.youtube.com/watch?v=JPtXZblI1sg&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=35 ==> URL is very much help full
240+
241+
THIS COUT AND CIN IS NOT POSSIBLE TO OVELOAD. ONLY PT 1 IS POSSIBLE BUT IT WILL NOT ACCORDING TO THE ENCAPSULATION SO BETTER USE FRIEND FUNCTION. PT 2 IS NOT POSSIBLE BECZ OSTREAM IS STANDARD C++ LIB OBJECT WE CAN NOT ADD ANYTHING. IN PT 3 WILL WORK IF I WILL RIGHT (D1 << COUT) BUT IT WILL NOT LOOKES GOOD BECZ THIS IS NOT THE STANDARD C++ SYNTAX FOR COUT.
242+
243+

friend.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Myclass {
6+
int data;
7+
public:
8+
Myclass (int i)
9+
{
10+
cout << "In side const" << endl;
11+
data = i;
12+
}
13+
//friend void display(const Myclass& a);
14+
friend void display(Myclass& a);
15+
};
16+
17+
void display(Myclass& a)
18+
{
19+
cout << "In display data =" << a.data << endl;
20+
}
21+
22+
int main()
23+
{
24+
Myclass obj(10);
25+
display(obj);
26+
return 0;
27+
}

ope_over_loading.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Myclass {
6+
int data;
7+
public:
8+
Myclass(int d) : data(d)
9+
{
10+
cout<< "In constru data = " << data << ":"<< d << endl;
11+
}
12+
/* Pre incremnt operator overloading */
13+
Myclass& operator++()
14+
{
15+
data = data * 2;
16+
return *this;// Here this has to be return this pointer
17+
}
18+
/* Post incremnt operator overloading */
19+
Myclass operator++(int)
20+
{
21+
Myclass t(data);// Here data has to be assign to temp varibale.
22+
data = data / 3;
23+
return t;
24+
}
25+
void disp()
26+
{
27+
cout << "data = " << data << endl;
28+
}
29+
};
30+
31+
int main()
32+
{
33+
Myclass obj(10);
34+
obj.disp();
35+
Myclass obj1 = obj++;
36+
cout << "After post increment" << endl;
37+
obj1.disp();
38+
obj.disp();
39+
obj1 = ++obj;
40+
cout << "After pre increment" << endl;
41+
obj1.disp();
42+
obj.disp();
43+
return 0;
44+
}

0 commit comments

Comments
 (0)