-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverloading.cpp
More file actions
72 lines (50 loc) · 1.09 KB
/
Overloading.cpp
File metadata and controls
72 lines (50 loc) · 1.09 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
// Overloading.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Test {
private:
int id;
string name;
public:
Test(): id(0), name("") {
}
Test(int id, string name): id(id), name(name) {
}
Test(const Test &other) {
cout << "Copy constructor running" << endl;
*this = other;
}
void print() const {
cout << id << ": " << name << endl;
}
const Test &operator=(const Test &other) {
cout << "Assignment running" << endl;
id = other.id;
name = other.name;
return *this;
}
};
int main1(int argc, _TCHAR* argv[])
{
Test test1(10, "Mike");
cout << "Print test1 " << flush;
test1.print();
Test test2(20, "Bob");
test2 = test1;
cout << "Print test2 " << flush;
test2.print();
Test test3;
//test3 = test2 = test1;
//test3 = test2;
test3.operator=(test2);
cout << "Print test3 " << flush;
test3.print();
cout << endl;
//Copy initialization
Test test4 = test1;
test4.print();
return 0;
}