adding exercise 1

This commit is contained in:
Jidong Xiao
2025-04-11 04:41:44 -04:00
committed by JamesFlare1212
parent 34f972e3f4
commit 7b9c74b098
2 changed files with 68 additions and 0 deletions

View File

@@ -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;
}

View 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;
}