体现后为: 继承方式下的代码复用功能。
帮忙编写一下咯,明天考试,谢谢各位了!
非常感谢!!!
用C++编写出以人类为基类,以public方式派生出学生类,使之能输出学生姓名、性别、年龄和学号等信息,体现
答案:2 悬赏:10 手机版
解决时间 2021-02-18 14:56
- 提问者网友:戎马万世
- 2021-02-17 22:38
最佳答案
- 五星知识达人网友:笑迎怀羞
- 2021-02-17 23:37
#include <iostream>
#include <string>
using namespace std;
class human
{
protected:
string m_name;
bool m_male;
int m_age;
};
class student:public human
{
int m_id;
public:
student(string name,bool isMale,int age,int id)
{
m_id=id;
m_name=name;
m_male=isMale;
m_age=age;
}
string Name()
{
return m_name;
}
string Sex()
{
if(m_male)
return "男";
else
return "女";
}
int Age()
{
return m_age;
}
int Id()
{
return m_id;
}
};
int main()
{
student s1("张三",true,20,12345);
cout<<"姓名:"<<s1.Name()<<endl;
cout<< "性别:" << s1.Sex()<<endl;
cout<<"年龄:"<<s1.Age()<<endl;
cout<<"学号:"<<s1.Id()<<endl;
}
#include <string>
using namespace std;
class human
{
protected:
string m_name;
bool m_male;
int m_age;
};
class student:public human
{
int m_id;
public:
student(string name,bool isMale,int age,int id)
{
m_id=id;
m_name=name;
m_male=isMale;
m_age=age;
}
string Name()
{
return m_name;
}
string Sex()
{
if(m_male)
return "男";
else
return "女";
}
int Age()
{
return m_age;
}
int Id()
{
return m_id;
}
};
int main()
{
student s1("张三",true,20,12345);
cout<<"姓名:"<<s1.Name()<<endl;
cout<< "性别:" << s1.Sex()<<endl;
cout<<"年龄:"<<s1.Age()<<endl;
cout<<"学号:"<<s1.Id()<<endl;
}
全部回答
- 1楼网友:话散在刀尖上
- 2021-02-18 00:25
#include
class human
{string name;
string sex;
int age;
public:
human(string n,string s,int a)
{name=n;
sex=s;
age=a;
}
void display1()
{
cout<
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯