-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment_operator.cpp
59 lines (57 loc) · 1.14 KB
/
assignment_operator.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
#include "iostream"
using namespace std;
class Person
{
private:
int age;
string name;
public:
Person()
{
name = "noname";
age = 0;
}
// copy constructor
Person(Person& A)
{
age = A.age;
name = A.name;
}
// assignment constructor
Person& operator = (Person& A)
{
age = A.age;
name = A.name;
return *this;
}
// Person operator +(Person const &B)
// {
// Person C;
// C.age = age + B.age;
// C.name = name + B.name;
// return C;
// }
void getdata()
{
cout<<name<<" "<<age<<endl;
}
void setdata(int age,string name)
{
this->name = name;
this->age = age;
}
};
int main()
{
Person A,B;
A.setdata(40, "raju");
B.setdata(10, "raj");
// assignment operator is called
B = A;
B.getdata();
// copy constructor is called
Person C = A;
C.getdata();
// Person D = A + B;
// D.getdata();
}