From 57f8f813dce641accdfe2bd49b0e9820ab6dd9ea Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 1 Dec 2023 13:43:31 -0500 Subject: [PATCH] adding inheritance example --- lectures/26_inheritance/inheritance.cpp | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lectures/26_inheritance/inheritance.cpp diff --git a/lectures/26_inheritance/inheritance.cpp b/lectures/26_inheritance/inheritance.cpp new file mode 100644 index 0000000..ab86f80 --- /dev/null +++ b/lectures/26_inheritance/inheritance.cpp @@ -0,0 +1,46 @@ +#include + +class Parent +{ +public: + Parent(){ + std::cout << "Parent default constructor" << std::endl; + } + Parent(std::string name, int age) : name(name), age(age){ + std::cout << "Parent other constructor" << std::endl; + } + void print(){ + std::cout << "Parent: " << name << ":" << age << std::endl; + } +protected: + std::string name; + int age; +private: + int id; +}; + +// public inheritance +class Child: public Parent +{ +public: + Child(){ + std::cout << "Child default constructor" << std::endl; + } + Child(std::string name, int age): Parent(name, age) { + std::cout << "Child other constructor" << std::endl; + } + void printChild(){ + std::cout << "Child: " << name << ":" << age << std::endl; + // std::cout << "id:" << id << std::endl; + } +protected: +}; + +int main(){ + Parent parent("bob", 30); + parent.print(); + Child child; + Child child2("james", 10); + + return 0; +}