派生类访问控制:例一(续) class Student public Person /1派生类 public: Student(string str,int age,int stuid):Person(str,age) this->stuid stuid; void showstu() {this->show();I/不能直接访问name和age cout <<"Stuid:"<<stuid <endl; } private: int stuid; 1学号 ex13 Inheritance_01.cpp int main() string str="Xi Jiajia"; Student stul(str,18,20150108); stu1.showStu(); http://math.ecnu.edu.cn/~jypan 12
http://math.ecnu.edu.cn/~jypan 12 派生类访问控制:例一(续) class Student : public Person // 派生类 { public: Student(string & str, int age, int stuid) : Person(str, age) { this->stuid = stuid; } void showStu() { this->show(); // 不能直接访问 name 和 age cout << "Stuid: " << stuid << endl; } private: int stuid; // 学号 }; int main() { string str="Xi Jiajia"; Student stu1(str, 18, 20150108); stu1.showStu(); } ex13_Inheritance_01.cpp
派生类访问控制:例二 class Person { public: Person(string str,int age):name(str) this->age age; void ShowName(){cout <"Name:"<name <endl; void ShowAge(){cout <"Age:"<<age <endl; private: string name; protected: int age; } http://math.ecnu.edu.cn/~jypan 13
http://math.ecnu.edu.cn/~jypan 13 派生类访问控制:例二 class Person { public: Person(string & str, int age) : name(str) { this->age = age; } void ShowName() { cout << "Name: " << name << endl; } void ShowAge() { cout << "Age: " << age << endl; } private: string name; protected: int age; };