课堂习题添加三角形

来源:互联网 发布:pta编程 编辑:程序博客网 时间:2024/06/09 15:54
//扩展程序:创建一个三角形类//修改create_object函数,使得程序支持三角形的创建//和求面积、打印等操作#include <iostream>#include<math.h> 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 类对象tprivate: 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;}class Triangle:public Shape{protected:double side1;double  side2;double side3;public:Triangle(double x,double y,double z):side1(x),side2(y),side3(z){}; double getArea() const;    void print() const; } ;double Triangle::getArea() const{cout<<"Triangle类的getArea函数,面积是:"<<endl;    double p=0,q=0;    p=((side1+side2+side3)/2);    q=sqrt(p*(p-side1)*(p-side2)*(p-side3));    return q;}void Triangle::print() const{int p;p=(side1+side2+side3)/2;cout<<"Triangle:side1="<<side1<<",side2="<<side2<<",side3="<<side3<<",p=(side1+side2+side3)/2"<<",Area="<<sqrt(p*(p-side1)*(p-side2)*(p-side3))<<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类对象:t:Triangle类对象:"<<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;}case 't':{int side1,side2,side3;cout<<"请输入三角形的三条边长度";cin>>side1>>side2>>side3;*ptr=new Triangle(side1,side2,side3);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="http://img.blog.csdn.net/20150525231530938?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbW9sdXpodWk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

0 0
原创粉丝点击