From 7f7b936341ca7981ba9025e8c8f1c013f03db326 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 11 Apr 2025 04:04:59 -0400 Subject: [PATCH] adding student program --- lectures/25_inheritance/README.md | 18 +++++- .../25_inheritance/students/student_test1.cpp | 35 ++++++++++++ .../25_inheritance/students/student_test2.cpp | 48 ++++++++++++++++ .../25_inheritance/students/student_test3.cpp | 57 +++++++++++++++++++ 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 lectures/25_inheritance/students/student_test1.cpp create mode 100644 lectures/25_inheritance/students/student_test2.cpp create mode 100644 lectures/25_inheritance/students/student_test3.cpp diff --git a/lectures/25_inheritance/README.md b/lectures/25_inheritance/README.md index c6923ec..316a12b 100644 --- a/lectures/25_inheritance/README.md +++ b/lectures/25_inheritance/README.md @@ -133,7 +133,7 @@ If no access specifier is provided: private members of the base class are never accessible directly in the derived class, regardless of the inheritance type. -### 25.5 Constructors and Destructors +## 25.5 Constructors and Destructors - Constructor Invocation: When a derived class object is created, the base class constructor is invoked first, followed by the derived class constructor. @@ -156,3 +156,19 @@ public: } }; ``` + +## 25.6 Example Programs: + +We develop this student_test.cpp program. + +### version 1 + +[student_test1.cpp](student_test1.cpp) In this version, there is only one class: class Human + +### version 2 + +[student_test2.cpp](student_test2.cpp) In this version, there is the base class - Human, and the child class - Student, but the child class doesn't have its own member variables, and its only member function is the constructor. + +### 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. diff --git a/lectures/25_inheritance/students/student_test1.cpp b/lectures/25_inheritance/students/student_test1.cpp new file mode 100644 index 0000000..e882b1d --- /dev/null +++ b/lectures/25_inheritance/students/student_test1.cpp @@ -0,0 +1,35 @@ +#include +#include + +class Human { +private: + std::string name; + int age; + int sleep_hours; + +public: + Human(std::string n, int a, int s) : name(n), age(a), sleep_hours(s) {} + + void introduce() { + std::cout << "Hello, I am " << name << ", and I am " << age << " years old.\n"; + } + + void sleep() { + std::cout << name << " is " << age << " years old who sleeps " << sleep_hours << " hours a night.\n"; + } +}; + +int main() { + // Creating instances of each class with member variables + Human h("Alice", 30, 8); + + // Introducing Humans + std::cout << "--- Human introducing ---\n"; + h.introduce(); // Output: Hello, I am Alice, and I am 30 years old. + + // Showing sleep behavior + std::cout << "--- Human sleep ---\n"; + h.sleep(); // Output: Alice is 30 years old and sleeps 8 hours a night. + + return 0; +} diff --git a/lectures/25_inheritance/students/student_test2.cpp b/lectures/25_inheritance/students/student_test2.cpp new file mode 100644 index 0000000..f5ca964 --- /dev/null +++ b/lectures/25_inheritance/students/student_test2.cpp @@ -0,0 +1,48 @@ +#include +#include + +class Human { +private: + std::string name; + int age; + int sleep_hours; + +public: + Human(std::string n, int a, int s) : name(n), age(a), sleep_hours(s) {} + + void introduce() { + std::cout << "Hello, I am " << name << ", and I am " << age << " years old.\n"; + } + + void sleep() { + std::cout << name << " is " << age << " years old who sleeps " << sleep_hours << " hours a night.\n"; + } +}; + +class Student : public Human { +public: + Student(std::string n, int a, int s) : Human(n, a, s) {} +}; + +int main() { + // Creating instances of each class with member variables + Human h("Alice", 30, 8); + Student s("Bob", 20, 5); + + // Introducing Humans + std::cout << "--- Human introducing ---\n"; + h.introduce(); // Output: Hello, I am Alice, and I am 30 years old. + + std::cout << "--- Student introducing ---\n"; + s.introduce(); // Output: I am a student. My name is Bob, and I am 20 years old. + + // Showing sleep behavior + std::cout << "--- Human sleep ---\n"; + h.sleep(); // Output: Alice is 30 years old and sleeps 8 hours a night. + + std::cout << "--- Student sleep ---\n"; + s.sleep(); // Output: Bob is a student, and they sleep 5 hours a night. + + return 0; +} + diff --git a/lectures/25_inheritance/students/student_test3.cpp b/lectures/25_inheritance/students/student_test3.cpp new file mode 100644 index 0000000..423d3de --- /dev/null +++ b/lectures/25_inheritance/students/student_test3.cpp @@ -0,0 +1,57 @@ +#include +#include + +class Human { +// change this to protect so that these variables can be accessed by the derived class. +protected: + std::string name; + int age; + int sleep_hours; + +public: + Human(std::string n, int a, int s) : name(n), age(a), sleep_hours(s) {} + + void introduce() { + std::cout << "Hello, I am " << name << ", and I am " << age << " years old.\n"; + } + + void sleep() { + std::cout << name << " is " << age << " years old who sleeps " << sleep_hours << " hours a night.\n"; + } +}; + +class Student : public Human { +public: + Student(std::string n, int a, int s) : Human(n, a, s) {} + + void introduce() { + std::cout << "Hello, I am " << name << ", and I am " << age << " years old. I’m majoring in 'How did I get here?' with a minor in 'It sounded easier when I signed up.'\n"; + } + + void sleep() { + std::cout << name << " is a college student who sleeps " << sleep_hours << " hours a night, and sleep 2 hours during boring lectures.\n"; + } +}; + +int main() { + // Creating instances of each class with member variables + Human h("Alice", 30, 8); + Student s("Bob", 20, 5); + + // Introducing Humans + std::cout << "--- Human introducing ---\n"; + h.introduce(); // Output: Hello, I am Alice, and I am 30 years old. + + std::cout << "--- Student introducing ---\n"; + s.introduce(); // Output: I am a student. My name is Bob, and I am 20 years old. + + // Showing sleep behavior + std::cout << "--- Human sleep ---\n"; + h.sleep(); // Output: Alice is 30 years old and sleeps 8 hours a night. + + std::cout << "--- Student sleep ---\n"; + s.sleep(); // Output: Bob is a student, and they sleep 5 hours a night. + + return 0; +} +