第11周编程练习(heuiwhffw)

第11周编程练习

1公有继承中派生类Student对基类Person成员的访问(20分)

题目内容:

已知基类Person的定义如下:
class Person
{ char Name[20];
char Sex;
int
Age;
public:
void Register(char *name, int age, char sex) ;
void
ShowMe();
};
请通过继承的方法建立一个派生类Student,其中
1.新增的数据成员有:
int
Number;
char ClassName[10];
2.新增的成员函数有:
void RegisterStu(char
*classname, int number, char *name, int age, char sex)

//对数据成员赋值,并使用基类的Register
void
ShowStu() //显示数据成员信息,并使用基类的ShowMe
在主程序中建立一个派生类对象,利用已有的成员函数分别显示派生类对象和基类对象的数据成员。

输入格式:

学生的班级、学号、姓名、年龄和性别

输出格式:

先输出派生类对象的数据成员,依次为 学号、班级、姓名、年龄和性别

再输出基类的数据成员,依次为姓名、年龄和性别

输入样例:

计算机51 85071011 张弓长 18 m

输出样例:

85071011 计算机51 张弓长 18 m
张弓长 18 m

//公有继承中派生类Student对基类Person成员的访问
#include<iostream>
using namespace std;
#pragma warning(disable:4996)
#include<cstring>
class Person
{
	char name[20];
	char sex;
	int age;
public:
	void Register(char* name, int age, char sex);
	void Showme();
};
void Person::Register(char* name, int age, char sex)
{
	strcpy(this->name, name);//形参与数据成员同名。用this指针加以区分
	this->age = age;
	this->sex = sex;
}
void Person::Showme()
{
	cout << name << ' ' << age << ' ' << sex << endl;
}
//
class Student :public Person
{
private:
	int number;
	char classname[10];
public:
	void Registerstu(char* classname, int number, char* name, int age, char sex);
	void Showme();
};
void Student::Showme()
{
	cout << number << " " << classname << ' ';
	Person::Showme();
}
void Student::Registerstu(char* classname, int number, char* name, int age, char sex)
{
	Register(name, age, sex);
	strcpy(this->classname, classname);
	this->number = number;
}
//
int main()
{
	Student a;
	char classname[20], name[20];
	int number, age;
	char sex;
	cin >> classname >> number >> name >> age >> sex;
	a.Registerstu(classname, number, name, age, sex);
	a.Showme();
	a.Person::Showme();
	return 0;
}

2一个基类Person的多个派生类(20分)

题目内容:

已知基类Person的定义如下:
class Person
{
protected:
char Name[10];
char
Sex;
int Age;
public:
void Register(char *name,int age,char
sex);
void
ShowMe();
};
请通过继承的方法建立两个派生类,其中
派生类Teacher:
1.新增的数据成员有:
char
Dept[20];
int
Salary;
2.新增的成员函数有:
构造函数,并使用基类的Register
3.重写的成员函数有:
void
ShowMe() //显示数据成员信息,并使用基类的ShowMe
派生类Student:
1.新增的数据成员有:
char
ID[12];
char Class[12];
2.新增的成员函数有:
Student(char *name,int age,char
sex, char *id,char *classid);
3.重写的成员函数有:
void
ShowMe() //显示数据成员信息,并使用基类的ShowMe
在主程序中分别建立两个派生类对象,利用已有的成员函数分别显示两个派生类对象的数据成员。

输入格式:

教师对象的初始化参数

学生对象的初始化参数

输出格式:

请参考输出样例严格按照格式输出教师对象和学生对象的详细信息

输入样例:

章立早 38 m 电信学院 2300
李木子 22 f 02035003 能动01

输出样例:

姓名 章立早
性别 男
年龄 38
工作单位 电信学院
月薪 2300
学号 02035003
姓名 李木子
性别 女
年龄 22
班级 能动01

//一个基类Person的多个派生类
#include<iostream>
using namespace std;
#pragma warning(disable:4996)
#include<cstring>
//基类
class Person
{protected:
	char name[20];
	char sex;
	int age;
public:
	void Register(char* name, int age, char sex);
	void Showme();
};
void Person::Register(char* name, int age, char sex)
{
	strcpy(this->name, name);//形参与数据成员同名。用this指针加以区分
	this->age = age;
	this->sex = sex;
}
void Person::Showme()
{
	cout << "姓名 "<<name << endl;
	cout << "性别 ";
	if (sex == 'f')
		cout << "女" << endl;
	if (sex == 'm')
		cout << "男" << endl;
	cout << "年龄 " << age << endl;
}
/教师类
class Teacher :public Person
{
private:
	char dept[20];
	int salary;
public:
	Teacher(char* name, char sex, int age, char* dept, int salary);
	void Showme();
};
Teacher::Teacher(char* name, char sex, int age, char* dept, int salary)
{
	Person::Register(name, age, sex);
	strcpy(this->dept, dept);
	this->salary = salary;
}
void Teacher::Showme()
{
	Person::Showme();
	cout << "工作单位 " << dept << endl;
	cout << "月薪 " << salary << endl;
}
学生类
class Student :public Person
{
private:
	char Id[12];
	char Class[12];
public:
	Student(char* name, int age, char sex, char* Id, char* Class);
	void Showme();
};
Student::Student(char* name, int age, char sex, char* Id, char* Class)
{
	Person::Register(name, age, sex);
	strcpy(this->Id, Id);
	strcpy(this->Class, Class);
}
void Student::Showme()
{
	cout << "学号 " << Id << endl;
	Person::Showme();
	cout << "班级 " << Class << endl;
}
/
int main()
{
	char name[20];
	char sex;
	int age;
	char dept[20];
	int salary;
	char NAME[20];
	char SEX;
	int AGE;
	char ID[12];
	char CLASS[12];
	cin >> name >> age >> sex >> dept >> salary;
	cin >> NAME >> AGE >> SEX >> ID >> CLASS;
	Teacher nt(name, sex, age, dept, salary);
	Student angel(NAME, AGE, SEX, ID, CLASS);
	nt.Showme();
	angel.Showme();
	return 0;
}

3派生类Student的构造函数和析构函数(20分)

题目内容:

已知基类Person的定义如下:
class Person
{ char Name[10]; //姓名
int Age; //年龄
public:
Person(char* name,int age)
{ strcpy(Name, name);
Age = age;
cout<<"constructor of person "<<Name<<endl; }
~Person()
{ cout<<"deconstructor of person "<<Name<<endl; };
请通过继承的方法建立一个派生类Student,其中
1.新增的数据成员有:
char ClassName[10]; //班级
Person Monitor; //班长
2.新增的成员函数有:
Student(char *name, int age, char *classname, char *name1, int age1) //name1和age1是班长的信息
~Student()
在主程序中建立一个派生类对象。

输入格式:

Student类的初始化信息

输出格式:

派生类和基类构造函数和析构函数输出的信息,请参考输出样例的格式。

输入样例:

张弓长 18 计算机51 李木子 20

输出样例:

constructor of person 张弓长

constructor of person 李木子

constructor of Student

deconstructor of Student

deconstructor of person 李木子

deconstructor of person 张弓长

注意:person为小写,单词间有一个空格。

//派生类Student的构造函数和析构函数
#include<iostream>
using namespace std;
#pragma warning(disable:4996)
#include<cstring>
class Person
{
	char name[10]; //姓名
	int age; //年龄
public:
	Person(char* name, int age)
	{
		strcpy(this->name, name);
		this->age = age;
		cout << "constructor of person " << this->name << endl;
	}
	~Person()
	{
		cout << "deconstructor of person " << name << endl;
	}
};

class Student :public Person
{
private:
	char classname[10]; //班级
	Person monitor; //班长
public:
	Student(char* name, int age, char* classname, char* name1, int age1);
	~Student();
};
Student::Student(char* name, int age, char* classname, char* name1, int age1) :Person(name, age), monitor(name1, age1)
{
	strcpy(this->classname, classname);//派生类的构造函数:基类构造函数,函数成员构造
	cout << "constructor of Student" << endl;
}
Student::~Student()
{
	cout << "deconstructor of Student" << endl;
}//自动调用析构函数,首先执行函数体内内容,再执行基类析构函数,再执行数据成员的析构函数
//
int main()
{
	char name[20], name1[20], classname[20];
	int age, age1;
	cin >> name >> age >> classname >> name1 >> age1;
	Student a(name, age, classname, name1, age1);
	return 0;
}

4从Point类继承的Circle类(20分)

题目内容:

已知基类Point的定义如下:

class Point
{
int x, y; //点的x和y坐标
public: Point( int = 0, int = 0 ); // 构造函数
void SetPoint( int, int ); // 设置坐标
int GetX() { return x; } // 取x坐标
int GetY() { return y; } // 取y坐标
void Print(); //输出点的坐标 };
Point( int a, int b ) { SetPoint( a, b ); }
void SetPoint( int a, int b ) { x = a; y = b; }
void Print() { cout << “[” << x << “,” << y << “]”;
}

请通过继承的方法建立一个派生类Circle,其中
1.新增的数据成员有: double radius;
2.新增的成员函数有:

Circle(int x = 0, int y = 0 , double r = 0.0); //对数据成员赋值,并使用SetRadius和基类的Point
void SetRadius( double ); //设置半径
double GetRadius(); //取半径
double Area(); //计算面积
void Print(); //输出圆心坐标和半径,并使用基类的Print
在主程序中分别建立基类对象和派生类对象,使用用户输入的初值分别对基类对象和派生类对象的数据成员赋值后,利用已有的成员函数分别显示基类对象和派生类对象的数据成员信息。
圆周率取3.14。

输入格式:

第一行是Point类的初始化信息,第二行是Circle类的初始化信息

输出格式:

请参考输出样例,严格按照样例输出,建议直接将部分文字复制粘贴进程序代码中

输入样例:

30 50
120 80 10

输出样例:

Point p [30,50]
Circle c Center=[120,80]
Radius=10
The centre of circle c [120,80]
The area of circle c 314

//从Point类继承的Circle类
#include<iostream>
using namespace std;
#pragma warning(diable:4996)
#include<cstring>
class Point
{
	int x, y; //点的x和y坐标 
public: 
	Point(int a=0, int b=0);
	void setpoint(int, int); // 设置坐标 
	int getx() { return x; } // 取x坐标 
	int gety() { return y; } // 取y坐标 
	void print();
};
Point::Point(int a, int b)
{
	setpoint(a, b);
}
void Point::setpoint(int a, int b)
{
	x = a;
	y = b;
}
void Point::print()
{
	cout << "[" << x << "," << y << "]";
}
//
class Circle :public Point
{
private:
	double radius;
public:
	Circle(int x , int y, double r );
	void setradius(double r);
	double getradius() { return radius; }
	double area() { return 3.14 * radius * radius; }
	void print();
};
Circle::Circle(int x , int y , double r ):Point(x,y)
{
	radius = r;
}
void Circle:: setradius(double r)
{
	radius = r;
}
void Circle::print()
{
	cout << "Circle c Center=";
	Point::print();
	cout << endl << "Radius=" << radius << endl;
	cout << "The centre of circle c ";
	Point::print();
	cout << endl;
	cout << "The area of circle c " << area() << endl;
}
/
int main()
{
	int x, y, x1, y1, r1;
	cin >> x >> y >> x1 >> y1 >> r1;
	Point a(x, y);
	Circle b(x1, y1, r1);
	cout << "Point p ";
	a.print();
	cout << endl;
	b.print();
	return 0;
}

5从Student类和Teacher类多重派生Graduate类(20分)

题目内容:

已知基类Person定义如下:
class Person
{
char Name[10];
char Sex[10];
int Age;
public:
void Register(char *name,int age,char *sex);
void ShowMe();
};
请通过继承的方法建立两个派生类,其中
派生类Teacher:
1.新增的数据成员有:
char Dept[20];
int Salary;
2.新增的成员函数有:
Teacher(char *name,int age,char *sex,char *dept,int salary);
void Show() //显示新增数据成员
派生类Student:
1.新增的数据成员有:
char ID[12];
char Class[12];
2.新增的成员函数有:
Student(char *name,int age,char *sex,char *ID,char *Class);
void Show()//显示新增数据成员
请通过继承的方法从Teacher和Student中建立派生类Graduate,其中
1.新增的成员函数有:
Graduate(char *name,int age,char *sex,char *dept,int salary,char *id,char *classid);
2.重写的成员函数有:
void ShowMe()//显示数据成员,要求调用基类的Show和ShowMe
在主程序中建立一个派生类Graduate的对象,利用成员函数显示对象的数据成员。

输入格式:

Graduate对象的初始化信息

输出格式:

按照输出样例格式输出Graduate对象的信息

输入样例:

李木子 22 f 电信学院 2300 04035003 硕401

输出样例:

班级 硕401
学号 04035003
姓名 李木子
性别 女
年龄 22
工作单位 电信学院
月薪 2300

//从Student类和Teacher类多重派生Graduate类
#include<iostream>
using namespace std;
#pragma warning(disable:4996)
#include<cstring>
基类
class Person
{
protected:
	char name[20];
	char sex;
	int age;
public:
	void Register(char* name, int age, char sex);
	void Showme();
};
void Person::Register(char* name, int age, char sex)
{
	strcpy(this->name, name);//形参与数据成员同名。用this指针加以区分
	this->age = age;
	this->sex = sex;
}
void Person::Showme()
{
	cout << "姓名 " << name << endl;
	cout << "性别 ";
	if (sex == 'f')
		cout << "女" << endl;
	if (sex == 'm')
		cout << "男" << endl;
	cout << "年龄 " << age << endl;
}
/教师类
class Teacher :public Person
{
private:
	char dept[20];
	int salary;
public:
	Teacher(char* name, char sex, int age, char* dept, int salary);
	void Showme();
};
Teacher::Teacher(char* name, char sex, int age, char* dept, int salary)
{
	Person::Register(name, age, sex);
	strcpy(this->dept, dept);
	this->salary = salary;
}
void Teacher::Showme()
{

	cout << "工作单位 " << dept << endl;
	cout << "月薪 " << salary << endl;
}
学生类
class Student :public Person
{
private:
	char Id[12];
	char Class[12];
public:
	Student(char* name, int age, char sex, char* Id, char* Class);
	void Showme();
};
Student::Student(char* name, int age, char sex, char* Id, char* Class)
{
	Person::Register(name, age, sex);
	strcpy(this->Id, Id);
	strcpy(this->Class, Class);
}
void Student::Showme()
{
	cout << "班级 " << Class << endl;
	cout << "学号 " << Id << endl;
	Person::Showme();
	
}
/Student类和Teacher类派生出Graduate类
class Graduate :public Teacher, public Student
{
private:
public:
	Graduate(char* name, int age, char sex, char* dept, int salary, char* id, char* classid);
	void Showme();
};
Graduate::Graduate(char* name, int age, char sex, char* dept, int salary, char* id, char* classid):\
	Teacher(name,sex,age,dept,salary),Student(name,age,sex,id,classid)
{//没有新的数据成员
}
void Graduate::Showme()
{
	Student::Showme();
	Teacher::Showme();
}
/
int main()
{
	char name[20], dept[20], id[20], classid[20];
	int age, salary;
	char sex;
	cin >> name >> age >> sex >> dept >> salary >> id >> classid;
	Graduate a(name, age, sex, dept, salary, id, classid);
	a.Showme();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值