adding exercise 1
This commit is contained in:
committed by
JamesFlare1212
parent
34f972e3f4
commit
7b9c74b098
@@ -172,3 +172,40 @@ We develop this student_test.cpp program.
|
||||
### version 3
|
||||
|
||||
[student_test3.cpp](student_test3.cpp) In this version, the child class has its own member functions introduce() and sleep(); and because these functions need to access the parent class's member variables, so we changed the member variables from private to protected.
|
||||
|
||||
### version 4
|
||||
|
||||
[student_test4.cpp](student_test4.cpp) In this version, we added the destructor to both the child class and the parent class. This program shows that when the child class object is destroyed, first its own destructor gets called, and then its parent destructor gets called.
|
||||
|
||||
## 25.7 What will be printed when running this program?
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() {
|
||||
std::cout << "A";
|
||||
}
|
||||
~A() {
|
||||
std::cout << "A";
|
||||
}
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
B() {
|
||||
std::cout << "B";
|
||||
}
|
||||
~B() {
|
||||
std::cout << "B";
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
{
|
||||
A a;
|
||||
B b;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
31
lectures/25_inheritance/exercises/exercise1.cpp
Normal file
31
lectures/25_inheritance/exercises/exercise1.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() {
|
||||
std::cout << "A";
|
||||
}
|
||||
~A() {
|
||||
std::cout << "A";
|
||||
}
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
B() {
|
||||
std::cout << "B";
|
||||
}
|
||||
~B() {
|
||||
std::cout << "B";
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
{
|
||||
A a;
|
||||
B b;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user