假如增加一个Globe(圆球)类,写出完整的程序。

假如增加一个Globe(圆球)类,写出完整的程序。

#include<iostream>
using namespace std;

class Shape
{
public:
	virtual float area() const
	{
		return 0.0;
	}

	virtual float volume() const
	{
		return 0.0;
	}

	virtual void shapeName() const = 0;
};






class Point :public Shape
{
public:
	Point(float = 0, float = 0);
	void setPoint(float, float);

	float getX() const
	{
		return x;
	}

	float getY()  const
	{
		return y;
	}

	virtual void shapeName() const
	{
		cout << "Point:";
	}


	friend ostream& operator<<(ostream&, const Point&);

protected:
	float x, y;
};

Point::Point(float a, float b)
{
	x = a;
	y = b;
}

void Point::setPoint(float a, float b)
{
	x = a;
	y = b;
}

ostream& operator<<(ostream& output, const Point& p)
{
	output << "[" << p.x << "," << p.y << "]";
	return output;
}





class Circle :public Point
{
public:
	Circle(float x = 0, float y = 0, float r = 0);
	void setRadius(float);
	float getRadius() const;
	virtual float area() const;
	virtual void shapeName() const
	{
		cout << "Circle:";
	}
	friend ostream& operator<<(ostream&, const Circle&);
protected:
	float radius;
};

Circle::Circle(float a, float b, float r) :Point(a, b), radius(r)
{

}

void Circle::setRadius(float r)
{
	radius = r;
}


float Circle::getRadius() const
{
	return radius;
}

float Circle::area() const
{
	return float(3.14159 * radius * radius);
}

ostream& operator<<(ostream& output, const Circle& c)
{
	output << "[" << c.x << "," << c.y << "],r=" << c.radius;
	return output;
}


class Globe :public Point
{
public:
	Globe(float x = 0, float y = 0, float r = 0);
	void setRadius(float);//声明设定半径 
	float getRadius() const;//声明取半径的值 
	virtual float area() const;//声明球的面积 
	virtual float volume() const;//声明球的体积 
	virtual void shapeName() const
	{
		cout << "Globe:";
	}
	friend ostream& operator<<(ostream&, const Globe&);
protected:
	float radius;
};

Globe::Globe(float a, float b, float r) :Point(a, b), radius(r)
{

}

void Globe::setRadius(float r)
{
	radius = r;
}

float Globe::getRadius() const
{
	return radius;
}


float Globe::area() const
{
	return float(4 * 3.14159 * radius);
}

float Globe::volume() const
{
	return float((4.0 / 3) * 3.14159 * radius * radius * radius);
}

ostream& operator<<(ostream& output, const Globe& c)
{
	output << "[" << c.x << "," << c.y << "],r=" << c.radius;
	return output;
}







class Cylinder :public Circle
{
public:
	Cylinder(float x = 0, float y = 0, float r = 0, float h = 0);
	void setHeight(float);
	virtual float area() const;
	virtual float volume() const;
	virtual void shapeName() const
	{
		cout << "Cylinder:";
	}
	friend ostream& operator<<(ostream&, const Cylinder&);
protected:
	float height;
};

Cylinder::Cylinder(float a, float b, float r, float h) :Circle(a, b, r), height(h)
{

}

void Cylinder::setHeight(float h)
{
	height = h;
}

float Cylinder::area() const
{
	return float(2 * Circle::area() + 2 * 3.14159 * radius * height);
}

float Cylinder::volume() const
{
	return Circle::area() * height;
}

ostream& operator<<(ostream& output, const Cylinder& cy)
{
	output << "[" << cy.x << "," << cy.y << "],r=" << cy.radius << ",h=" << cy.height;
	return output;
}







int main()
{
	Point point(3.2f, 4.5f);
	Circle circle(2.4f, 1.2f, 5.6f);
	Globe globe(3.6f, 7.9f, 8.6f);
	Cylinder cylinder(3.5f, 6.4f, 5.2f, 10.5f);

	point.shapeName();
	cout << point << endl;

	circle.shapeName();
	cout << circle << endl;

	globe.shapeName();
	cout << globe << endl;

	cylinder.shapeName();
	cout << cylinder << endl << endl;

	Shape* pt;

	pt = &point;
	pt->shapeName();
	cout << "x=" << point.getX() << ",y=" << point.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n";

	pt = &circle;
	pt->shapeName();
	cout << "x=" << circle.getX() << ",y=" << circle.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n";

	pt = &globe;
	pt->shapeName();
	cout << "x=" << globe.getX() << ",y=" << globe.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n";

	pt = &cylinder;
	pt->shapeName();
	cout << "x=" << cylinder.getX() << ",y=" << cylinder.getY() << "\narea=" << pt->area() << "\nvolume=" << pt->volume() << "\n\n";

	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值