C++qiujieda

 

代码如下:

#include <iostream>
using namespace std;
const double PI = (float)3.14159;
class Shape
{
public:
	virtual double Biaomj() = 0;
	virtual double Tiji() = 0;
};
//球体
class Sphere:public Shape
{
private:
	double radius;  //半径
public:
	Sphere(double r):radius(r){}
	double Biaomj()
	{
		return 4 * PI * radius * radius;
	}
	double Tiji()
	{
		return 4.0/3 * PI * radius * radius * radius;
	}
	double GetRadius(){return radius;}
};

class Cube : public Shape
{
private:
	double length;
public:
	Cube(double l):length(l){}
	double Biaomj()
	{
		return 6.0 * length * length;
	}
	double Tiji()
	{
		return length * length * length;
	}
	double GetLen(){return length;}
};

int main()
{
	Sphere cir(2);
	Cube cb(4);
	cout << "半径为"<< cir.GetRadius() << "的球的表面积为:" << cir.Biaomj() << ",体积为: " << cir.Tiji() <<endl;
	cout << "长为" << cb.GetLen() << "的立方体的表面积为:" << cb.Biaomj() << ",体积为:" << cb.Tiji() << endl;
	return 0;
}

 

是不是这个意思:

#include<iostream>
using namespace std;
const double pi = 3.1415;
class shape
{
public:
	shape(int a) { cout << "继承shape" << endl; }
};

class Sphere :public shape
{
public:
	Sphere(int a,int b):shape(a),r(b)
	{
		cout << "体积:" << (4 / 3) * pi * r * r * r << endl;
		cout << "表面积:" << 4 * pi * r * r << endl;
	}
private:
	int r;
};
class Cube :public shape
{public:
	Cube(int a,int b):shape(a),wid(b)
	{
		cout << "立方体的表面积:" << a * a * 6 << endl;
		cout << "立方体的面积为:" << a * a * a << endl;
	}
private:
	int wid;
};
int main()
{
	int a,b;
	cout << "输入球体的半径:" << endl;
	cin >> a;
	Sphere s(2, a);
	cout << "输入立方体的边长:" << endl;
	cin >> b;
	Cube c(2, b);
	return 0;

}