14.利用虚函数实现多态性来求正方体、球体和圆柱体的表面积和体积。

14.利用虚函数实现多态性来求正方体、球体和圆柱体的表面积和体积。
具体要求如下,
从正方体、球体和圆柱体的各种运算中抽象出一个公共基类container为抽象类,
在其中定义求表面积和体积的纯虚函数(该抽象类本身是没有表面积和体积可言的)。
在抽象类中定义一个公共的数据成员radius,此数据可作为球的半径、正方体的边长、圆柱体底面圆半径。
由此抽象类派生出要描述的3个类,即cube、snhere和cvlinder,在这3个类中都具有求表面积和体积的重定义版本。

#include<iostream>
using namespace std;
#define pi 3.14
class container
{
protected:
	double radius;
public:
	container(double a) :radius(a) {}
	virtual double getbiaomianji() = 0;
	virtual double gettiji() = 0;
	virtual void show1() = 0;//display->表面积
	virtual void show2() = 0;//display->体积
};
class cube:public container
{
public:
	cube(double c) :container(c) {}
	double getbiaomianji()
	{
		return this->radius * this->radius * pi*4;
	}
	double gettiji()
	{
		return this->radius * this->radius * pi * 4 / 3;
	}
	void show1()
	{
		cout << "球的表面积为:\t" << this->getbiaomianji() << endl;
	}
	void show2()
	{
		cout << "球的体积为:\t" << this->gettiji() << endl;
	}
};
class snhere:public container
{
public:
	snhere(double s) :container(s) {}
	double getbiaomianji()
	{
		return this->radius * this->radius *6;
	}
	double gettiji()
	{
		return this->radius * this->radius *this->radius;
	}
	void show1()
	{
		cout << "正方体的表面积为:\t" << this->getbiaomianji() << endl;
	}
	void show2()
	{
		cout << "正方体的体积为:\t" << this->gettiji() << endl;
	}
};
class cvlinder:public container
{
protected:
	double height;
public:
	cvlinder(double a, double b) :container(a), height(b) {}
	double getbiaomianji()
	{
		return this->radius * this->radius *pi*2 + this->radius* 2 * pi * height;
	}
	double gettiji()
	{
		return this->radius * this->radius * pi*height;
	}
	void show1()
	{
		cout << "圆柱的表面积为:\t" << this->getbiaomianji() << endl;
	}
	void show2()
	{
		cout << "圆柱的体积为:\t" << this->gettiji() << endl;
	}
};
int main()
{
	double a, b;
	cin >> a;
	cube cu(a);
	container& co = cu;
	co.show1(); co.show2();
	cout << "----" << endl;

	cin >> a;
	snhere sn(a);
	container* con = &sn;
	con->show1(); con->show2();
	cout << "----" << endl;

	cin >> a >> b;
	cvlinder cv(a, b);
	cv.show1(); cv.show2();
	system("pause");
}

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用函数球体圆柱体体积表面积的C++代码示例: ``` #include <iostream> #include <cmath> using namespace std; class Shape { public: virtual double getVolume() = 0; virtual double getSurfaceArea() = 0; }; class Sphere : public Shape { private: double radius; public: Sphere(double r) { radius = r; } double getVolume() { return (4.0/3.0) * M_PI * pow(radius, 3); } double getSurfaceArea() { return 4 * M_PI * pow(radius, 2); } }; class Cylinder : public Shape { private: double radius; double height; public: Cylinder(double r, double h) { radius = r; height = h; } double getVolume() { return M_PI * pow(radius, 2) * height; } double getSurfaceArea() { return 2 * M_PI * radius * (radius + height); } }; int main() { Shape* shape; Sphere sphere(2.0); Cylinder cylinder(2.0, 5.0); shape = &sphere; cout << "Sphere Volume: " << shape->getVolume() << endl; cout << "Sphere Surface Area: " << shape->getSurfaceArea() << endl; shape = &cylinder; cout << "Cylinder Volume: " << shape->getVolume() << endl; cout << "Cylinder Surface Area: " << shape->getSurfaceArea() << endl; return 0; } ``` 在上面的代码中,我们定义了一个抽象基类`Shape`,包含两个纯函数`getVolume`和`getSurfaceArea`。然后,我们定义了两个派生类`Sphere`和`Cylinder`,分别表示球体圆柱体。这两个派生类继承了`Shape`类,并实现了`getVolume`和`getSurfaceArea`函数。 在`main`函数中,我们首先创建了一个`Sphere`对象和一个`Cylinder`对象,并将它们的地址分别赋给一个`Shape`指针变量`shape`。然后,我们通过`shape`指针调用了`getVolume`和`getSurfaceArea`函数,分别输出了球体圆柱体体积表面积。 需要注意的是,我们在`Shape`类中将`getVolume`和`getSurfaceArea`函数声明为纯函数,这意味着我们不能直接创建`Shape`对象,而只能通过派生类来创建对象。这种技术称为抽象类,它可以帮助我们实现,即在运行时根据对象的实际类型来调用相应的函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值