创建三角形类

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

class Shape{
	public:
		virtual double getArea() const =0;
		virtual void print() const =0;  			
		virtual ~Shape(){}
}; 	
class Circle:public Shape{
	public:
		Circle( int = 0, int = 0, double = 0.0 );  
		virtual double getArea() const;
		virtual void print() const;

	private:
		int x,y;
		double radius;
};
class Rectangle:public Shape{
	public:
		Rectangle( int = 0, int = 0);
		virtual double getArea() const;
		void print() const;
	protected:
		int a,b;
};
double Shape::getArea() const
{
	cout<<"基类的getArea函数,面积是:";   
	return 0.0;
}

void Shape::print() const
{
	cout<<"Base class Object"<<endl;
}

Circle::Circle( int xValue, int yValue, double radiusValue )
{
	x=xValue;  y=yValue;
	radius= radiusValue ;
}
double Circle::getArea() const
{
	cout<<"Circle类的getArea函数,面积是:";   
	return 3.14159 * radius * radius;
}
void Circle::print() const
{
	cout << "center is ";
	cout<<"x="<<x<<"   y="<<y;
	cout << "; radius is " << radius<<endl;
}
Rectangle::Rectangle( int aValue, int bValue )
{
	a=aValue;  b=bValue;
}
double Rectangle::getArea() const
{
	cout<<"Rectangle类的getArea函数,面积是:";   
	return a * b;
}
void Rectangle::print() const
{
	cout <<"hight is "<<a;
	cout<<"width is"<<b<<endl;
} 	
class Cube:public Rectangle{
	public:
		Cube(int x=0, int y=0, int z=0):Rectangle(x,y),c(z){};
		double getArea() const;
		void print() const; 
	private:
		int c;
};
double Cube::getArea() const
{return a*b*c;}

void Cube::print() const
{
	cout<<"Cube:h="<<c<<",length="<<a
	<<",width="<<b<<",Area="<<a*b*c<<endl;
}

class Triangle : public Shape
{
	protected:
		double x, y, z;
	public:
		Triangle(double a = 0, double b = 0, double c = 0):x(a), y(b), z(c){}
		double getArea() const;
		void print() const;
};

void Triangle::print() const
{
	cout << "Triangle:x = " << x << ",y =" << y << ",z =" << z << endl;
}

double Triangle::getArea() const
{
	double len = x + y + z;
	double p = len / 2;
	double s = sqrt(p*(p - x)*(p - y)*(p - z));
	cout<<"Triangle类的getArea函数,面积是:";
	return s;
}

void creat_object(Shape **ptr);
void display_area(Shape *ptr);
void display_side(Shape *ptr);
void delete_object(Shape *ptr);

int main()
{
	Shape *shape_ptr;
	creat_object(&shape_ptr);
	display_area(shape_ptr);
	display_side(shape_ptr);
	delete_object(shape_ptr);
	return 0;
}

void creat_object(Shape **ptr)
{
	char type;
	*ptr = NULL;
	do{
		cout<<"创建对象:"<<endl;
		cout<<"c:Circle类对象; r:Rectangle类对象; u:Cube类对象; t:Triangle类对象"<<endl;
		cin>>type;
		switch (type)
		{
			case 'c':{
				int xx,yy;
				double rr;
				cout<<"请输入圆心的座标和圆的半径:"<<endl;
				cin>>xx>>yy>>rr;
				*ptr = new Circle(xx,yy,rr);
				break;
			}
			case 'r':{
				int aa,bb;
				cout<<"请输入矩形的长和宽:"<<endl;
				cin>>aa>>bb;
				*ptr = new Rectangle(aa,bb);
				break;
			}
			case 'u':{
				int aa,bb,cc;
				cout<<"请输入立方体的长、宽、高:"<<endl;
				cin>>aa>>bb>>cc;
				*ptr = new Cube(aa,bb,cc);
				break;
			}
			case 't':{
				double x, y, z;
				cout << "请输入三角形的三条边:"<<endl;
				cin >> x >> y >> z;
				*ptr = new Triangle(x, y, z);
				break;
			}
			default:cout<<"类型错误,请重新选择\n";
		}
	}while(*ptr==NULL);
} 

void display_area(Shape *ptr)
{   
	cout<<"显示所创建对象的面积,调用的是:"<<endl;
	cout<<ptr->getArea()<<endl;
}

void display_side(Shape *ptr)
{
	cout<<"显示所创建对象的边:"<<endl;
	ptr -> print();
}

void delete_object(Shape *ptr)
{delete(ptr);}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值