-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcpp_ch1_ch2.cpp
More file actions
92 lines (68 loc) · 1.61 KB
/
Ecpp_ch1_ch2.cpp
File metadata and controls
92 lines (68 loc) · 1.61 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <sstream>
#include <exception>
#include <string>
using namespace std;
class Ch1 {
public:
static const int _integral = 5; // static integral init
static string _static;
Ch1() : _str("testing") {
}
void print() {
cout << endl;
cout << "_str: " << _str << endl;
cout << "_static: " << _static << endl;
}
Ch1& operator=(const Ch1& rhs) {}
private:
const string _str;
};
class Derived : public Ch1 {
public:
//Default constructor, member initialisation list
Derived() : Ch1(), _b(0) {}
Derived(int a) : Ch1(), _b(a) {}
// Copy constructor
// calls the parent's one
Derived(const Derived& rhs) : Ch1(rhs), _b(rhs._b) {
cout << "Derived copy ctor" << endl;
}
// Assignment operator
// calls the base's one
Derived& operator=(const Derived& rhs)
{
cout << "Derived's operator =" << endl;
if(this == &rhs) return *this;
Ch1::operator=(rhs);
_b = rhs._b;
return *this;
}
void print() {
Ch1::print();
cout << "_b: " << _b << endl;
}
private:
int _b;
};
string Ch1::_static = "static string";
int main(int argc, char const * argv[])
{
Ch1 ch;
const char* const a = "const data const pointer";
char const * b = "const data const pointer";
char const c = 'c';
b = "blah";
ch.print();
Ch1 obj2(ch);
obj2.print();
Derived d(4);
d.print();
Derived d2(d); // copy ctor
d2.print();
Derived d3 = d; // copy ctor
Derived d4(10);
d4 = d;
d4.print();
return 0;
}