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

Posted qianrushi1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++,继承,也称基类,派生类,自己研究吧,很简单想法相关的知识,希望对你有一定的参考价值。

#include <iostream>
using namespace std;
//基类People
class 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;

//派生类Teacher
class 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;

以上是关于c++,继承,也称基类,派生类,自己研究吧,很简单想法的主要内容,如果未能解决你的问题,请参考以下文章

基类派生类

C++学习19 类的多继承

JAVA 面向对象-2-继承(Inheritance)

多继承 与 多重继承

C++_练习—继承_公有继承

继承与派生