关闭

6.13~6.14

77人阅读 评论(0) 收藏 举报

6.13

#include<iostream>
using namespace std;
class Base{
public:
	Base(double h1)
	{h=h1;}
	virtual void disp()
  {}  
protected:  
    double h;  
};  
  
class Cuboid :public Base{          
public:  
   Cuboid(double h1, double l1, double w1) :Base(h1)  
   {  
       l = l1;  
       w = w1;  
    }  
    virtual void disp()  
    {  
        cout << "正方体的体积为:" <<l*w*h<< endl;  
    }  
protected:  
    double l;               
   double w;               
}; 
 
class Cylinder :public Base {    
public:  
    Cylinder(double h1, double r1) :Base(h1)  
  {  
       r = r1;  
   }  
  virtual void disp()  
    {  
        cout << "圆柱体的体积为:" << 3.14*r*r*h << endl;  
   }  
protected:  
   double r;                 
};  
  
int main()  
{  
   Base *pc;                     
   Cuboid cu(2.0,2.0,4.0); Cylinder cy(3.0,2.0);  
   pc = &cu;  
   pc->disp();  
   pc = &cy;  
   pc->disp();  
   return 0;  
}  


6.14

#include<iostream>
using namespace std;
class container
{
protected:
	double radius;
public:
	container(double radius1)
	{radius=radius1;}
	virtual double surface_area()=0;
    virtual double volume()=0;
};
class cube:public container
{
public:
	cube(double radius1):container(radius1)
	{};
virtual	double surface_area()
	{cout<<"正方体的表面积为:"<<6*radius*radius<<endl;
    return 0;}
virtual	double volume()
	{cout<<"正方体的体积为:"<<radius*radius*radius<<endl;
     return 0;}
};
class sphere:public container
{
public:
	sphere(double radius1):container(radius1)
	{};
 virtual double surface_area()
	{cout<<"球的表面积为:"<<4*3.14*radius*radius<<endl;
     return 0;}
 virtual double volume()
	{cout<<"球的体积为:"<<(4/3)*3.14*radius*radius*radius<<endl;
     return 0;}
};
class cylinder:public container
{
public:
	cylinder(double radius1,double h1):container(radius1)
	{h=h1;}
  virtual double surface_area()
	{cout<<"圆柱体的表面积为:"<<2*3.14*radius*(h+radius)<<endl;
     return 0;}
   virtual double volume()
	{cout<<"圆柱体的体积为:"<<3.14*radius*radius*h<<endl;
     return 0;}
private:
	double h;
};
int main()
{container *p;
cube c1(4.0);
sphere s1(5.0);
cylinder cy(4.0,5.0);
p=&c1;
p->surface_area();
p->volume();
p=&s1;
p->surface_area();
p->volume();
p=&cy;
p->surface_area();
p->volume();
return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1057次
    • 积分:110
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    阅读排行
    评论排行
    最新评论