#include <iostream>
using namespace std;
class Base{
protected:
double h;
public:
Base(double h1)
{
h=h1;
}
virtual void disp()=0;
};
class cuboid:public Base{
private:
double wid,len;
public:
cuboid(double l,double w,double h1):Base(h1)
{ len=l;
wid=w;
}
void disp()
{ cout<<"长方体:"<<endl;
cout<<"长度: "<<len<<endl;
cout<<"宽度: "<<wid<<endl;
cout<<"高度: "<<h<<endl;
cout<<"长方体的体积: "<<len*wid*h<<endl;
}
};
class Cylinder:public Base{
private:
double r;
public:
Cylinder(double h1,double r1):Base(h1)//
{ r=r1;
}
void disp()
{ cout<<"圆柱体:"<<endl;
cout<<"高度: "<<h<<endl;
cout<<"半径: "<<r<<endl;
cout<<"圆柱体的体积: "<<3.1415*r*r*h<<endl;
}
};
int main()
{ Base *p;
Cylinder cy(8,9);
cuboid cu(5,6,7);
p=&cy;
p->disp();
p=&cu;
p->disp();
system("pause");
return 0;
}
using namespace std;
class Base{
protected:
double h;
public:
Base(double h1)
{
h=h1;
}
virtual void disp()=0;
};
class cuboid:public Base{
private:
double wid,len;
public:
cuboid(double l,double w,double h1):Base(h1)
{ len=l;
wid=w;
}
void disp()
{ cout<<"长方体:"<<endl;
cout<<"长度: "<<len<<endl;
cout<<"宽度: "<<wid<<endl;
cout<<"高度: "<<h<<endl;
cout<<"长方体的体积: "<<len*wid*h<<endl;
}
};
class Cylinder:public Base{
private:
double r;
public:
Cylinder(double h1,double r1):Base(h1)//
{ r=r1;
}
void disp()
{ cout<<"圆柱体:"<<endl;
cout<<"高度: "<<h<<endl;
cout<<"半径: "<<r<<endl;
cout<<"圆柱体的体积: "<<3.1415*r*r*h<<endl;
}
};
int main()
{ Base *p;
Cylinder cy(8,9);
cuboid cu(5,6,7);
p=&cy;
p->disp();
p=&cu;
p->disp();
system("pause");
return 0;
}