-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContructors.cpp
41 lines (33 loc) · 889 Bytes
/
Contructors.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
#include <iostream>
#include <string>
class Foo{
private:
int data;
public:
Foo(int data){
this->data = data;
}
~Foo(){};
//Assigment contructor
Foo& operator = (const Foo& rhs){
std::cout << "Assigment contructors" << std::endl;
data = rhs.data;
return *this;
}
void printData(){
std::cout << data << std::endl;
}
// Copying contructors
Foo(const Foo& rhs){
std::cout << "Cpoying contructors" << std::endl;
data = rhs.data;
}
};
int main(){
Foo obj1(2); //Foo(int data) / Normal Constructor called
Foo obj2 = obj1; // Copying Constructor Called
obj2.printData(); // Prints 2
Foo obj3(42);
obj3 = obj1; //Assignment Constructor Called
obj3.printData(); // Prints 2
}