c++题 设计一个基类Shape,派生出圆形Circle类,再分别派生出圆球类Ball,
c++题设计一个基类Shape,派生出圆形Circle类,再分别派生出圆球类Ball,圆锥类Taper圆柱类Cylinder使用相关编程题方法求出圆的面积,其他几何图形的...
c++题 设计一个基类Shape,派生出圆形Circle类,再分别派生出圆球类Ball,圆锥类Taper 圆柱类Cylinder使用相关编程题方法求出圆的面积,其他几何图形的表面积和体积
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
1个回答
展开全部
#include<iostream>
#define PI 3.14159
using namespace std;
//shape类
class Shape{
public:
virtual float area() const { return 0.0;}//虚函数
virtual float volume() const {return 0.0;}//虚函数
virtual void shapName() const =0;//纯虚函数
} ;
//Circle类
class Circle : public Shape{
public:
Circle(float radius = 0 ){ this->radius = radius;;}
void setRadius( float radius ) const{ this->radius = radius;}
float getRadius( ) const { return radius; }
virtual float area() const { return PI * radius * radius ;}
virtual void shapName() const { cout<<"Circle类"<<endl;}
private:
float radius;
};
//Ball类
class Ball: public Circle{
public:
Ball( float radius = 0):Circle( radius ){}
virtual float area() const{ return Circle::area() * 4.0 ;}
virtual float volume() const{ return Circle::area() * Circle::getRadius() * 4.0 / 3.0; }
virtual void shapName() const {cout<<"Ball类"<<endl;}
};
//Taper类
class Taper:public Circle{
public:
Taper( float radius = 0, float height = 0 ):Circle( radius ){ this->height = height;}
virtual float area() const{ return Circle::area() ;}//这个面积就自己去找了把
virtual float volume() const{ return Circle::area() * Circle::getRadius(); }//同上
virtual void shapName() const {cout<<"Taper类"<<endl;}
private:
float height;
};
//Cylinder类
class Cylinder:public Circle{
public:
Cylinder( float radius = 0, float height = 0 ):Circle( radius ){ this->height = height;}
virtual float area() const{ return Circle::area() ;}//这个面积就自己去找了把
virtual float volume() const{ return Circle::area() * Circle::getRadius(); }//同上
virtual void shapName() const {cout<<"Cylinder类"<<endl;}
private:
float height;
};
#define PI 3.14159
using namespace std;
//shape类
class Shape{
public:
virtual float area() const { return 0.0;}//虚函数
virtual float volume() const {return 0.0;}//虚函数
virtual void shapName() const =0;//纯虚函数
} ;
//Circle类
class Circle : public Shape{
public:
Circle(float radius = 0 ){ this->radius = radius;;}
void setRadius( float radius ) const{ this->radius = radius;}
float getRadius( ) const { return radius; }
virtual float area() const { return PI * radius * radius ;}
virtual void shapName() const { cout<<"Circle类"<<endl;}
private:
float radius;
};
//Ball类
class Ball: public Circle{
public:
Ball( float radius = 0):Circle( radius ){}
virtual float area() const{ return Circle::area() * 4.0 ;}
virtual float volume() const{ return Circle::area() * Circle::getRadius() * 4.0 / 3.0; }
virtual void shapName() const {cout<<"Ball类"<<endl;}
};
//Taper类
class Taper:public Circle{
public:
Taper( float radius = 0, float height = 0 ):Circle( radius ){ this->height = height;}
virtual float area() const{ return Circle::area() ;}//这个面积就自己去找了把
virtual float volume() const{ return Circle::area() * Circle::getRadius(); }//同上
virtual void shapName() const {cout<<"Taper类"<<endl;}
private:
float height;
};
//Cylinder类
class Cylinder:public Circle{
public:
Cylinder( float radius = 0, float height = 0 ):Circle( radius ){ this->height = height;}
virtual float area() const{ return Circle::area() ;}//这个面积就自己去找了把
virtual float volume() const{ return Circle::area() * Circle::getRadius(); }//同上
virtual void shapName() const {cout<<"Cylinder类"<<endl;}
private:
float height;
};
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询