天天看點

C++的作業

//擴充程式:建立一個三角形類
//修改create_object函數,使得程式支援三角形的建立
//和求面積、列印等操作
#include <iostream>

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;  		// 輸出Circle 類對象t

private: 
	int x,y;        				// 圓心座标
	double radius;  				// 圓半徑
}; 						// 派生類Circle定義結束
class Rectangle : public Shape {	
public:
	Rectangle( int = 0, int = 0);  		// 構造函數
	virtual double getArea() const;   		// 傳回面積
	void print() const;  			// 輸出Rectangle類對象
protected:
	int a,b;       				// 矩形的長和寬
}; 				// 派生類
double Shape::getArea() const
{
    cout<<"基類的getArea函數,面積是 ";   
return 0.0;
}  						// Shape類getArea函數的定義

void Shape::print() const
{
	cout<<"Base class Object"<<endl;
}						//Shape類print函數定義

Circle::Circle( int xValue, int yValue, double radiusValue )
{
	x=xValue;  y=yValue;
	radius= radiusValue ;
} 						// Circle類構造函數
double Circle::getArea() const
{
   cout<<"Circle類的getArea函數,面積是 ";   
   return 3.14159 * radius * radius;
} 						// Circle類getArea函數定義
void Circle::print() const
{
   cout << "center is ";
   cout<<"x="<<x<<"   y="<<y;
   cout << "; radius is " << radius<<endl;
} 						// Circle類print函數定義
Rectangle::Rectangle( int aValue, int bValue )
{
	a=aValue;  b=bValue;
} 						// Rectangle類構造函數
double Rectangle::getArea() const
{
   cout<<"Rectangle類的getArea函數,面積是 ";   
   return a * b;
} 					      // Rectangle類getArea函數定義
void Rectangle::print() const
{
   cout << "hight is "<<a;
   cout<<"width is"<<b<<endl;
} 	
class Cube : public Rectangle {		//派生類Cube的定義
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;
}

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


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

void creat_object(Shape **ptr)
{	char type;
	*ptr = NULL;			//空指針
	do{
	   cout<<"建立對象。請選擇:";
	   cout<<"c:Circle類對象;r:Rectangle類對象;u:Cube類對象"<<endl;
	   cin>>type;
       switch (type)
	   {case 'c':			//建立Ciecle類對象
		{int xx,yy;
		 double rr;
		 cout<<"請輸入圓心的座标和圓的半徑:";
		 cin>>xx>>yy>>rr;
		 *ptr = new Circle(xx,yy,rr);
		 break;
		}
	  case 'r':			//建立Rectangle類對象 
		{int aa,bb;
		 cout<<"請輸入矩形的長和寬:";
		 cin>>aa>>bb;
		 *ptr = new Rectangle(aa,bb);
		 break;
		}
	  case 'u':			//建立Cube類對象 
		{int aa,bb,cc;
		 cout<<"請輸入立方體的長、寬、高:";
		 cin>>aa>>bb>>cc;
		 *ptr = new Cube(aa,bb,cc);
		 break;
		}
	    default:cout<<"類型錯誤,請重新選擇\n";
	   }
	   }while(*ptr==NULL);
} 

void display_area(Shape *ptr)
{   cout<<"顯示所建立對象的面積,調用的是"<<endl;
    cout<<ptr->getArea() << endl;   
}

void delete_object(Shape *ptr)
{	delete(ptr);
}
           
<img src="https://img-blog.csdn.net/20150519213307552?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGlwaTA5MDgwN2xpbGk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
           
<img src="https://img-blog.csdn.net/20150519213333807?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGlwaTA5MDgwN2xpbGk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
           
<img src="https://img-blog.csdn.net/20150519213423902?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGlwaTA5MDgwN2xpbGk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />