-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.cpp
60 lines (49 loc) · 1.44 KB
/
inheritance.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
60
#include <iostream>
#include <string>
class BaseClass {
public:
std::string name;
virtual std::string get_details() {
return name;
}
BaseClass(BaseClass const&) = delete;
/*BaseClass(BaseClass const& obj) {
name = obj.name;
std::cout << "copying BaseClass (" << this << ") " << std::endl;
}*/
BaseClass() {
std::cout << "creating BaseClass (" << this << ") " << std::endl;
}
virtual ~BaseClass() {
std::cout << "destroying ~BaseClass(" << this << ") " << std::endl;
}
};
class DerivedClass : public BaseClass {
public:
int age;
std::string get_details() override {
return name + " / " + std::to_string(age);
}
DerivedClass(std::string n, int a) {
std::cout << "creating DerivedClass (" << this << ") " << std::endl;
name = n;
age = a;
}
virtual ~DerivedClass() {
std::cout << "destroying ~DerivedClass(" << this << ") " << std::endl;
}
};
int inheritance_main(int argc, wchar_t* argv[]) {
std::cout << std::endl << __FILE__ << std::endl;
DerivedClass a("Bubba", 50);
// downcast
BaseClass* b = &a;
// upcast
DerivedClass* c = static_cast<DerivedClass*>(b);
// static cast not as pointer
// not allowed because copy constructor deleted
//BaseClass d = static_cast<BaseClass>(a);
// resolving virtual function
std::cout << b->get_details() << std::endl;
return 0;
}