Skip to content

Commit ec9cc75

Browse files
author
XuhuaHuang
committed
Add notes on constructor in inheritance
1 parent 35ad3bb commit ec9cc75

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

Inheritance/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

88
add_executable(inheritance_notes "notes.cpp")
99

10-
add_subdirectory("BankingAccounts")
11-
add_subdirectory("Relatives")
10+
add_executable(constructor_destructor "constructor_destructor.cpp")
11+
12+
if(MSVC)
13+
add_subdirectory("BankingAccounts")
14+
add_subdirectory("Relatives")
15+
endif()

Inheritance/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Inheritance and Access
2+
3+
- In public inheritance, all members are inherited with the same access levels as they had in the base class
4+
- In protected inheritance, all public members in the base class are inherited as protected
5+
- In private inheritance, all members in the base class are inherited as
6+
private
7+
- Generally, most use cases for inheritance should follow public inheritance, even though in C++ private would be the default
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @file constructor_destructor.cpp
3+
* @author Xuhua Huang
4+
* @brief Notes on inheritance of constructors and destructors.
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
#include <iostream>
12+
13+
class Parent {
14+
public:
15+
Parent() { std::cout << __LINE__ << " In Parent default constructor" << std::endl; }
16+
~Parent() { std::cout << __LINE__ << " In Parent destructor" << std::endl; }
17+
};
18+
19+
class Child : public Parent {
20+
public:
21+
Child() { std::cout << __LINE__ << " In Child default constructor" << std::endl; }
22+
~Child() { std::cout << __LINE__ << " In Child destructor" << std::endl; }
23+
};
24+
25+
int main() {
26+
Parent p;
27+
Child c;
28+
return 0;
29+
} // c is destroyed first, then p is destroyed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @file overloaded_constructor.cpp
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-04-03
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
12+
#include <iostream>
13+
14+
class Parent {
15+
public:
16+
Parent() { std::cout << __LINE__ << " In Parent default constructor" << std::endl; }
17+
18+
Parent([[maybe_unused]]
19+
int x) {
20+
std::cout << __LINE__ << " In Parent overloaded constructor" << std::endl;
21+
}
22+
};
23+
24+
class Child : public Parent {
25+
public:
26+
Child()
27+
: Parent() {
28+
std::cout << __LINE__ << " In Child default constructor" << std::endl;
29+
}
30+
31+
Child(int x)
32+
: Parent(x) {
33+
std::cout << __LINE__ << " In Child overloaded constructor" << std::endl;
34+
}
35+
};
36+
int main() {
37+
Child c1;
38+
Child c2(1);
39+
return 0;
40+
}

0 commit comments

Comments
 (0)