设计一个人员类Person,其拥有公有变量名字name、保护变量性别sex和私有变量年龄age,以及相应的构造函数、公有函数showInfo和其他必要函数,showInfo函数的功能是输出name、sex和age。从类Person派生出类Student(学生类),类Student的继承方式为公有继承。类Student新增了公有变量班级号classID、保护变量学号num和私有变量身高height,以及公有函数print,用于输出学生的所有变量。在主函数中声明类Student的对象s,通过对象s调用print函数输出学生的所有变量。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string _name,string _sex,int _age)
{
name = _name;
sex = _sex;
age = _age;
}
public:
void showInfo()
{
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
}
public:
string name;
protected:
string sex;
private:
int age;
};
class Student:public Person
{
public:
Student(string _name,string _sex,int _age,string _classID,string _num,double _height):
Person(_name,_sex,_age)
{
classID = _classID;
num = _num;
height = _height;
}
public:
void print()
{
Person::showInfo();
cout<<"classID:"<<classID<<endl;
cout<<"num:"<<num<<endl;
cout<<"height:"<<height<<endl;
}
public:
string classID;
protected:
string num;
private:
double height;
};
int main()
{
Student s("zhangsan","man",20,"10001","0000101",178);
s.print();
return 0;
}