实验五 继承与派生

1、利用继承性与派生类啦管理学生和教师档案。假设要管理下述几类人员的如下一些数据。
teacher(教师)类:姓名、性别、年龄、职称、担任课程;
student(学生)类:姓名、性别、年龄、学号、系别;
gradstudent(研究生)类:姓名、性别、年龄、学号、系别、导师。
要求实现学生和教师的信息输出。

2、编写一个程序计算出球、圆柱和圆锥的表面积和体积。要求:
(1)定义一个基类圆,至少含有一个数据成员半径;
(2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数。 
(3)定义主函数,求球、圆柱、圆锥的和体积(初始化用构造函数)。

完整代码

#include <iostream>
#include <string>
#include <cmath> 
using namespace std;

class Circle
{
	protected:
	double radius;
	public:
	Circle(double r)
	{
		radius=r;
	}
};

class Ball:public Circle
{
	public:
	Ball(double r):Circle(r)
	{
		
	}
	double surface()
	{
		return (4.0*3.14*pow(radius,2));
	} 
	double volume()
	{
		return(4.0/3*3.14*pow(radius,3));
	}
};

class Column:public Circle
{
	private:
	double length;
	public:
	Column(double r,double l):Circle(r)
	{
		length=l;
	}
	double surface()
	{
		return (2*3.14*pow(radius,2)+2*3.14*radius*length);
	} 
	double volume()
	{
		return(2*3.14*pow(radius,2)*length);
	}
};

class Cone:public Circle
{
	private:
	double high;
	public:
	Cone(double r,double h):Circle(r)
	{
		high=h;
	}
	double surface()
	{
		return (2*3.14*pow(radius,2)+2*3.14*pow(high,2));
	} 
	double volume()
	{
		return(2*3.14*pow(radius,2)*high/3);
	}
};

int main()
{
	Ball b1(5);
	cout<<"球表面积:"<<b1.surface()<<endl;
	cout<<"球体积:"<<b1.volume()<<endl; 
	Column c1(2,3);
	cout<<"圆柱表面积:"<<c1.surface()<<endl;
	cout<<"圆柱体积:"<<c1.volume()<<endl;
	Cone c2(2,4);
	cout<<"圆锥表面积:"<<c2.surface()<<endl;
	cout<<"圆锥体积:"<<c2.volume()<<endl;
}
#include <iostream>
#include <string>
using namespace std;

class All
{
	public:
	All(string n,char s,int a)
	{
		name=n;
		sex=s;
		age=a;
	}
	protected:
	string name;
	char sex;
	int age;
};

class teacher:public All
{
	public:
	teacher(string n,char s,int a,string j,string c):All(n,s,a)
	{
		job=j;
		classes=c;
	}
	void output_teacher()
	{
		cout<<"教师姓名:"<<name<<endl<<"性别:"<<sex<<endl<<"年龄:"<<age<<endl
		<<"职称:"<<job<<endl<<"担任课程:"<<classes<<endl<<endl; 
	}
	private:
	string job;
	string classes;
};

class student:public All
{
	public:
	student(string n,char s,int a,int num,string spe):All(n,s,a)
	{
		number=num;
		special=spe;
	}
	void output_student()
	{
		cout<<"学生姓名:"<<name<<endl<<"性别:"<<sex<<endl<<"年龄:"<<age<<endl
		<<"学号:"<<number<<endl<<"系别:"<<special<<endl<<endl; 
	}
	protected:
	int number;
	string special;
};

class gradstudent:public student
{
	public:
	gradstudent(string n,char s,int a,int num,string spe,string t):student(n,s,a,num,spe)
	{
		teach=t;
	}
	void output_gradstudent()
	{
		cout<<"学生姓名:"<<name<<endl<<"性别:"<<sex<<endl<<"年龄:"<<age<<endl
		<<"学号:"<<number<<endl<<"系别:"<<special<<endl<<"导师:"<<teach<<endl<<endl; 
	}
	private:
	string teach;
};

int main()
{
	teacher t1("张三",'m',30,"副教授","大学物理");
	student s1("李四",'m',18,18104001,"计算机");
	gradstudent g1("王五",'f',22,132222,"软件工程","张三");
	t1.output_teacher();
	s1.output_student();
	g1.output_gradstudent(); 
	
	return 0;
}


仅作留档。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值