当前位置 : 主页 > 编程语言 > c++ >

c++,继承,也称基类,派生类,自己研究吧,很简单想法

来源:互联网 收集:自由互联 发布时间:2021-06-23
#include iostreamusing namespace std;//基类Peopleclass People{public: People(char *name, int age); void display();protected: char *m_name; int m_age;};People::People(char *name, int age): m_name(name), m_age(age){}void People::display(){

#include <iostream>using namespace std;//基类Peopleclass People{public: People(char *name, int age); void display();protected: char *m_name; int m_age;};People::People(char *name, int age): m_name(name), m_age(age){}void People::display(){ cout<<m_name<<"今年"<<m_age<<"岁了,是个无业游民。"<<endl;}//派生类Teacherclass Teacher: public People{public: Teacher(char *name, int age, int salary); void display();private: int m_salary;};Teacher::Teacher(char *name, int age, int salary): People(name, age), m_salary(salary){}void Teacher::display(){ cout<<m_name<<"今年"<<m_age<<"岁了,是一名教师,每月有"<<m_salary<<"元的收入。"<<endl;}int main(){ People *p = new People("王志刚", 23); p -> display(); p = new Teacher("赵宏佳", 45, 8200); p -> display(); return 0;}

网友评论