人与学生

设计一个类people,有保护数据成员:age(年龄,整型),name(姓名,string),行为成员:两个构造函数(一个默认,另一个有参数);默认析构函数;void setValue(int m, string str)给age和name赋值;void display()输出age和name。
设计一个学生类student,公有继承类people,有私有成员:ID(学号,整型),行为成员:两个构造函数(一个默认,另一个有参数);默认析构函数;void setID(int m)给ID赋值;void displayID()输出ID。
在main函数定义学生对象,给对象初始化赋值或调用setValue()和setID()赋值,并输出学生的信息。

#include <iostream>
using namespace std;
class Person
{
protected:
	string name;
	int age;
public:
	Person(){}
	Person(int a,string n):age(a),name(n){}
	~Person(){}
	void setValue(int m, string str) { age = m; name = str; }
};
class Student:public Person
{
private:
	int id;
public:
	Student() {}
	Student(int a, string n,int i) : Person(a,n),id(i){}
	~Student() {}
	void setid(int m) { id = m; }
	void display()
	{
		cout << "姓名:" << name << endl;
		cout << "年龄:" << age << endl;
		cout << "学号:" << id << endl;
	}
};
int main()
{
	Student s(19, "郭永刚", 10001);
	s.display();
}

多继承

学生具有姓名,班级,学号等属性,有上课等行为;教师具有工号,工资等属性,有教课等行为;助教既是学生,又是老师,具有学生和老师的双重属性。请用类的多继承机制实现上述问题

#include <iostream>
using namespace std;

class Student
{
protected:
	int id;
	string name;
	int grade;
public:
	Student() { cout << "上课" << endl; }
	Student(int i, string n,int g):id(i),name(n),grade(g){cout << "上课" << endl;}
	~Student() {}
	
	
};
class Teacher
{
protected:
	int Tid;
	int gongzi;
public:
	Teacher(){ cout << "教学" << endl; }
	Teacher(int t,int a):Tid(t),gongzi(a){ cout << "教学" << endl; }
};
class accont:public Student,public Teacher
{
public:
	accont(int i, string n, int g, int T, int a) :Student(i, n, g), Teacher(T, a) { cout << "助教即上课又教学" << endl; }

void display()
	{
	
		cout << "学号:" << id << endl;
		cout << "姓名:" << name << endl;
		cout << "年级:" << grade << endl;
		cout << "工号:" << Tid << endl;
		cout << "工资:" << gongzi << endl;
	}
};
int main()
{
	accont s(10001, "郭永刚", 16,20001,30000);
	s.display();
}

虚基类应用

编写动物类animal,受保护数据成员name(名称,string),age(年龄,int),公有函数成员void show(),输出“Animal, 名称, 年龄”;公有派生鱼类fish和兽类beast,鱼类增加受保护数据成员velocity(速度,int),公有函数成员void show(),输出“Fish, 名称, 年龄, 速度”;兽类增加受保护数据成员appetite(食量,int),公有函数成员void show(),输出“Beast, 名称, 年龄, 食量”;鱼类和兽类再公有派生两栖动物类amphibious,无添加数据成员,有公有函数成员void show(),输出 “Fish, 名称, 年龄, 速度”(第一行),“Beast, 名称, 年龄, 食量”(第二行)。每个类均有一个构造函数用于设置该类所有数据成员的属性值。
【输入形式】
第一个动物名称,第二个动物名称,第一个动物年龄,第二个动物年龄,速度信息,食量信息
【输出形式】
作为鱼类的信息输出
作为兽类的信息输出
【输入样例】
eyu wugui 10 100 120 300

【输出样例】
Fish, eyu, 10, 120
Beast, eyu, 10, 300
【样例说明】
输出时每项信息后的逗号为英文”,”,逗号后跟一个空格

eyu wugui 10 100 120 300
Fish, eyu, 10, 120
Beast, eyu, 10, 300
请按任意键继续. . .

wugui chanchu 120 3 2 3
Fish, wugui, 120, 2
Beast, wugui, 120, 3
请按任意键继续. . .

wkjf djkjf 12 14 200 400
Fish, wkjf, 12, 200
Beast, wkjf, 12, 400
请按任意键继续. . .

#include <iostream>
using namespace std;

class animal
{
protected:
	string name;
	int age;
public:
	animal(){}
	animal(string n,int a):name(n),age(a){}
	 virtual void show()
	{
		 cout << "Animal, " << name << ", " << age << " " << endl;
	}
};
class fish:public animal
{
protected:
	int veloc;
public:
	fish() {}
	fish(string n, int a, int v) :animal(n, a), veloc(v){}
	void show()
	{
		cout << "Fish, " << name << ", " << age << " , " << veloc << endl;;
	}
};
class Beast :public animal
{	
protected:
	int app;
public:
	Beast() {}
	Beast(string n, int a,int p):animal(n,a),app(p){}
	void show()
	{
		cout << "Beast, " << name << ", " << age << " , " << app << endl;
	}
	
};
int main()
{
	string name1, name2;
	int age1, age2, v, p;
	cin >> name1 >> name2 >> age1 >> age2 >> v >> p;
	fish f(name1, age1, v);
	Beast b(name2, age2, p);
	f.show();
	b.show();
}
posted on 2023-04-24 20:14  许七安gyg  阅读(10)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });