FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppdemo/inheritance.cpp at main · bsclifton/cppdemo · GitHub
bsclifton
/
cppdemo
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppdemo
/
inheritance.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (49 loc) · 1.44 KB
Breadcrumbs
cppdemo
/
inheritance.cpp
Copy path
File metadata and controls
60 lines (49 loc) · 1.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
;
}
Back
|
FazBrowse Home
|
New Git URL