设计一个图形类Shape,其中包含两个纯虚函数:输出函数disp()和求面积函数area() 5
设计一个图形类Shape,其中包含两个纯虚函数:输出函数disp()和求面积函数area(),然后派生出二维图形类TwoDShape和三维图形类ThreeDShape,均...
设计一个图形类Shape,其中包含两个纯虚函数:输出函数disp()和求面积函数area(),然后派生出二维图形类TwoDShape和三维图形类ThreeDShape,均包含各自的虚函数求周长函数perimeter()和求体积函数volume(),之后由TwoDShape派生出圆类Circle和矩形类Rectangle,由ThreeDShape派生出柱体类Cone和正方体类Box,最后在主函数中通过抽象基类实现统一接口。
展开
1个回答
展开全部
#include "stdafx.h"
#include<iostream>
using namespace std;
const double PI = 3.14;
class Shape
{
public:
virtual void disp() = 0;
virtual double area() = 0;
};
class TwoDShape:public Shape
{
public:
virtual double perimeter(){ return 0; }
};
class ThreeDShape:public Shape
{
public:
virtual double volume(){ return 0; }
};
class Circle: public TwoDShape
{
private:
double radius;
public:
Circle(double r = 0):radius(r){}
double perimeter() { return 2 * PI * radius; }
double area(){return PI*radius*radius;}
void disp(){cout << "Circle : radius = " << radius << endl;}
};
class Rectangle: public TwoDShape
{
private:
double length;
double width;
public:
Rectangle(double l=0, double w = 0):length(l), width(w){}
double perimeter(){return 2 * (length + width);}
double area() {return length * width;}
void disp(){cout << "Rectangle : length = " << length << ", width = "<< width << endl;}
};
class Cone: public ThreeDShape
{
private:
double radius;
double high;
public:
Cone(double r=0, double h = 0):radius(r), high(h){}
double volume(){return PI*radius*radius*high;}
double area() {return 2*PI*radius*(2+high);}
void disp(){cout << "Cone : radius = " << radius << ", high = "<< high << endl;}
};
class Box: public ThreeDShape
{
private:
double length;
public:
Box(double l=0):length(l){}
double volume(){return length*length*length;}
double area() {return 6*length*length;}
void disp(){cout << "Box : length = " << length << endl;}
};
void main(int argc, char *argv[])
{
Circle circle(5);
Rectangle rect(4,6);
Cone cone(3, 2);
Box box(7);
Shape* ptr = &circle;
ptr->disp();
cout << ptr->area() << endl;
ptr = ▭
ptr->disp();
cout << ptr->area() << endl;
ptr = &cone;
ptr->disp();
cout << ptr->area() << endl;
ptr = &box;
ptr->disp();
cout << ptr->area() << endl;
}
#include<iostream>
using namespace std;
const double PI = 3.14;
class Shape
{
public:
virtual void disp() = 0;
virtual double area() = 0;
};
class TwoDShape:public Shape
{
public:
virtual double perimeter(){ return 0; }
};
class ThreeDShape:public Shape
{
public:
virtual double volume(){ return 0; }
};
class Circle: public TwoDShape
{
private:
double radius;
public:
Circle(double r = 0):radius(r){}
double perimeter() { return 2 * PI * radius; }
double area(){return PI*radius*radius;}
void disp(){cout << "Circle : radius = " << radius << endl;}
};
class Rectangle: public TwoDShape
{
private:
double length;
double width;
public:
Rectangle(double l=0, double w = 0):length(l), width(w){}
double perimeter(){return 2 * (length + width);}
double area() {return length * width;}
void disp(){cout << "Rectangle : length = " << length << ", width = "<< width << endl;}
};
class Cone: public ThreeDShape
{
private:
double radius;
double high;
public:
Cone(double r=0, double h = 0):radius(r), high(h){}
double volume(){return PI*radius*radius*high;}
double area() {return 2*PI*radius*(2+high);}
void disp(){cout << "Cone : radius = " << radius << ", high = "<< high << endl;}
};
class Box: public ThreeDShape
{
private:
double length;
public:
Box(double l=0):length(l){}
double volume(){return length*length*length;}
double area() {return 6*length*length;}
void disp(){cout << "Box : length = " << length << endl;}
};
void main(int argc, char *argv[])
{
Circle circle(5);
Rectangle rect(4,6);
Cone cone(3, 2);
Box box(7);
Shape* ptr = &circle;
ptr->disp();
cout << ptr->area() << endl;
ptr = ▭
ptr->disp();
cout << ptr->area() << endl;
ptr = &cone;
ptr->disp();
cout << ptr->area() << endl;
ptr = &box;
ptr->disp();
cout << ptr->area() << endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
--
2022-12-05 广告
2022-12-05 广告
图形化编程简单理解为用积木块形式编程,scratch和python也是其中的一种,属于入门级编程,以其简单生动的画面获得无数学生的喜爱,深圳市创客火科技有限公司是一家做教育无人机的公司,旗下有编程无人机,积木无人机及室内外编队,每款飞机含有...
点击进入详情页
本回答由--提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询