c++多态性练习

<people.h>

#include<iostream>

#include<string>
using namespace std;
class people
{


protected:int age; string name;
public:
people();
people(int a, string b)
{
age = a;
name = b;
}
void showage();
void showname();
void showall();//重复函数


};
void people::showage()
{
cout << age << endl;


}
void people::showname()
{

}
people::people()
{
cout << "输入年龄以及姓名" << endl;
cin >> age;
cin >> name;
}
void people::showall()
{
cout << name << age << endl;


}




class students:public people
{
public:
students();
students(int a, int b, string c):people(a,c),socers(b)
{
};
void shwocs();
void showall();
protected:int socers;




}; 
students::students()
{
//不需要显示的调用people的构造函数
cout << "请输入分数" << endl;
cin >> socers;
};
void students::shwocs()
{
cout << socers;
}
void students::showall()
{
cout << name << age << socers << endl;

}







源.cpp

#include"base.h"
int main()
{
people a;


students b;
students c(18, 98, "li");


b.showall();//覆盖了基类的showall函数
b.people::showall();
c.showall();
char ch;
cin >> ch;
return 0;
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页