实验八 综合

1.编写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle、Square、Rectangle、Trapezoid、Triangle。用虚函数分别计算几种图形面积,并求它们之和。
2.某学校对教师每个月工资的计算规定如下:固定工资+课时补贴;教授的固定工资为5000元,每个课时补贴50;副教授的固定工资为3000,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。

完整代码

#include<iostream>
using namespace std;

class Shape
{
	protected:
	double bas;
	public:
	Shape(double b)
	{bas=b;}
	virtual void display()
	{
		cout<<"base="<<bas<<endl;
	}
};

class Circle:public Shape 
{
	private:
	public:
	Circle(double b):Shape(b)
	{};
	void display()
	{
		cout<<"r="<<bas<<endl;
		cout<<"圆的面积为:"<<3.14*bas*bas<<endl<<endl;
	}
};

class Square:public Shape 
{
	private:
	public:
	Square(double b):Shape(b)
	{};
	void display()
	{
		cout<<"a="<<bas<<endl;
		cout<<"正方形的面积为:"<<bas*bas<<endl<<endl;
	}
};

class Rectangle:public Shape 
{
	private:
	double width;
	public:
	Rectangle(double b,double wid):Shape(b)
	{
		width=wid;
	}
	void display()
	{
		cout<<"length="<<bas<<" width="<<width<<endl;
		cout<<"矩形的面积为:"<<bas*width<<endl<<endl;
	}
};

class Trapezoid:public Shape 
{
	private:
	double under;
	double high;
	public:
	Trapezoid(double b,double und,double hig):Shape(b)
	{
		under=und;
		high=hig;
	}
	void display()
	{
		cout<<"上底:"<<bas<<" 下底:"<<under<<" 高:"<<high<<endl;
		cout<<"梯形的面积为:"<<(bas+under)*high*0.5<<endl<<endl;
	}
};

class Triangle:public Shape 
{
	private:
	double high;
	public:
	Triangle(double b,double hig):Shape(b)
	{
		high=hig;
	}
	void display()
	{
		cout<<"底:"<<bas<<" 高:"<<high<<endl;
		cout<<"三角形的面积为:"<<bas*high*0.5<<endl<<endl;
	}
};

int main()
{
	Shape sha(2);
	Circle cir(2);
	Square squ(2);
	Rectangle rec(2,3);
	Trapezoid tra(2,3,4);
	Triangle tir(2,3);
	Shape *pt=&sha;
	pt=&cir;
	pt->display();
	pt=&squ;
	pt->display();
	pt=&rec;
	pt->display();
	pt=&tra;
	pt->display();
	pt=&tir;
	pt->display();
	return 0;
}

#include<iostream>
using namespace std;

class Teacher 
{
	protected:
	string name;
	public:
	Teacher(string nam)
	{name=nam;}
	virtual void display()
	{
		cout<<"教师姓名:"<<name<<endl;
	}
};

class Jiaoshou:public Teacher 
{
	private:
	double classtime;
	public:
	Jiaoshou(string nam,double cla):Teacher(nam)
	{
	    classtime=cla;
	}
	void display()
	{
		cout<<"教师姓名:"<<name<<endl;
		cout<<"职称:教授"<<endl;
		cout<<"课时数:"<<classtime<<endl;
		cout<<"工资数:"<<5000+classtime*50<<endl<<endl;
	}
};

class Fujiaoshou:public Teacher 
{
	private:
	double classtime;
	public:
	Fujiaoshou(string nam,double cla):Teacher(nam)
	{
	    classtime=cla;
	}
	void display()
	{
		cout<<"教师姓名:"<<name<<endl;
		cout<<"职称:副教授"<<endl;
		cout<<"课时数:"<<classtime<<endl;
		cout<<"工资数:"<<3000+classtime*30<<endl<<endl;
	}
};

class Jiangshi:public Teacher 
{
	private:
	double classtime;
	public:
	Jiangshi(string nam,double cla):Teacher(nam)
	{
	    classtime=cla;
	}
	void display()
	{
		cout<<"教师姓名:"<<name<<endl;
		cout<<"职称:讲师"<<endl;
		cout<<"课时数:"<<classtime<<endl;
		cout<<"工资数:"<<2000+classtime*20<<endl<<endl;
	}
};


int main()
{
	Teacher tea("小王");
	Jiaoshou jiao("小刘",20);
	Fujiaoshou fujiao("小张",20);
	Jiangshi jiang("小李",20);
	Teacher *pt=&tea;
	pt=&jiao;
	pt->display();
	pt=&fujiao;
	pt->display();
	pt=&jiang;
	pt->display();
	return 0;
}

仅作留档。

发布了30 篇原创文章 · 获赞 12 · 访问量 884

猜你喜欢

转载自blog.csdn.net/weixin_43893854/article/details/104321973