#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.1415926;
class CStereoShape{
public:
virtual double GetArea() = 0;
virtual double GetVolume() = 0;
};
class CCube : public CStereoShape{
private:
double height, width, length;
public:
CCube(double l, double w, double h){
length = l; width = w; height = h;
};
~CCube(){};
double GetArea(){
return 2*(length*width+width*height+height*length);
}
double GetVolume(){
return length*width*height;
}
};
class CSphere : public CStereoShape{
private:
double radius;
public:
CSphere(double r){
radius = r;
};
~CSphere(){};
double GetArea(){
return 4.0 * PI * pow(radius, 2);
}
double GetVolume(){
return 4.0 / 3 * PI * pow(radius, 3);
}
};
int main(void){
CCube * a_cube = new CCube(4, 5, 6);
CSphere * c_sphere = new CSphere(7);
CStereoShape * ptr = nullptr;
ptr = a_cube;
cout << "The area of a_cube : " << ptr->GetArea() << endl;
cout << "The volume of a_cube : " << ptr->GetVolume() << endl;
//ptr = nullptr;
delete ptr;
ptr = c_sphere;
cout << "The area of a_cube : " << ptr->GetArea() << endl;
cout << "The volume of a_cube : " << ptr->GetVolume() << endl;
//delete c_sphere;
delete ptr;
return 0;
}
无感..
以上.