-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperatoroverloading5.cpp
More file actions
39 lines (36 loc) · 989 Bytes
/
operatoroverloading5.cpp
File metadata and controls
39 lines (36 loc) · 989 Bytes
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
// Any constructor that can be called with a single argument works as a conversion constructor,
// which means it can also be used for implicit conversion to the class being constructed
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int i = 0, int j = 0)
{
x = i;
y = j;
cout << "Constructor called." << endl;
cout << "object address: " << this << endl;
}
void print()
{
cout << "x = " << x << ", y = " << y << '\n';
}
Point* getAddr(){
return this;
}
};
int main()
{
Point t(20, 20);
t.print();
Point* ptr = t.getAddr();
t = 30; // invoke conversion constructor -> temporary Point(30,0)
// then compiler assigns Point(30,0) to t, and destroys the temporary Point(30,0)
t.print();
cout << "First constructed object address: " << ptr << endl;
cout << "Current object address: " << t.getAddr() << endl;
return 0;
}