
定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函
定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函数printinfo(三个函数均为纯虚函数)。从shape公有派生poi...
定义一个抽象类shape,包括公有的计算面积area函数,计算体积volume函数,输出基本信息函数printinfo(三个函数均为纯虚函数)。从shape公有派生point类,增加私有数据成员x,y坐标,以及构造函数,析构函数。从point公有派生circle类,增加私有数据成员半径r,以及构造函数,析构函数。从circle公有派生cylinder类,增加私有数据成员高度h,以及构造函数,析构函数。(在定义三个派生类的过程中,自己考虑需要重定义哪个虚函数)。在main函数中,定义shape类的指针,指向派生类的对象,输出三类对象的基本信息,面积,体积;(将shape指针改为引用再尝试)。
不要复制哦 展开
不要复制哦 展开
1个回答
2016-05-18 · 百度知道合伙人官方认证企业
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注

展开全部
参考下面代码
#include <iostream>
#define pi 3.14
using namespace std;
class Shape
{
protected:
double Area;
public:
virtual void area() = 0;
void showArea(){cout << Area << endl;}
};
class Circle : public Shape
{
double r;
public:
Circle(int r){this->r = r;}
virtual void area(){Area = pi * r * r;}
};
class Rectangle : public Shape
{
double l, w;
public:
Rectangle(int l, int w){this->l = l, this->w = w;}
virtual void area(){Area = l * w;};
};
class Triangle : public Shape
{
double a, b;
public:
Triangle(int a, int b){this->a = a, this->b = b;}
virtual void area(){Area = a * b / 2.0;}
};
void disPlay()
{
cout << "Please input a number:" << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Triangle" << endl;
}
int main()
{
int n, num = 0;
int r, l, w, a, b;
cout << "Please input the number of shape:" << endl;
cin >> n;
Shape *temp[100];
disPlay();
while (n--)
{
int elect;
cin >> elect;
switch(elect)
{
case 1:
cout << "Please input r:" << endl;
cin >> r;
temp[num++] = new Circle(r);
break;
case 2:
cout << "Please input l and w:" << endl;
cin >> l >> w;
temp[num++] = new Rectangle(l, w);
break;
case 3:
cout << "Please input a and b:" << endl;
cin >> a >> b;
temp[num++] = new Triangle(a, b);
break;
defult:
cout << "error!" << endl;
}
}
for (int i = 0; i < num; i++)
{
temp[i]->area();
temp[i]->showArea();
}
return 0;
}
#include <iostream>
#define pi 3.14
using namespace std;
class Shape
{
protected:
double Area;
public:
virtual void area() = 0;
void showArea(){cout << Area << endl;}
};
class Circle : public Shape
{
double r;
public:
Circle(int r){this->r = r;}
virtual void area(){Area = pi * r * r;}
};
class Rectangle : public Shape
{
double l, w;
public:
Rectangle(int l, int w){this->l = l, this->w = w;}
virtual void area(){Area = l * w;};
};
class Triangle : public Shape
{
double a, b;
public:
Triangle(int a, int b){this->a = a, this->b = b;}
virtual void area(){Area = a * b / 2.0;}
};
void disPlay()
{
cout << "Please input a number:" << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Triangle" << endl;
}
int main()
{
int n, num = 0;
int r, l, w, a, b;
cout << "Please input the number of shape:" << endl;
cin >> n;
Shape *temp[100];
disPlay();
while (n--)
{
int elect;
cin >> elect;
switch(elect)
{
case 1:
cout << "Please input r:" << endl;
cin >> r;
temp[num++] = new Circle(r);
break;
case 2:
cout << "Please input l and w:" << endl;
cin >> l >> w;
temp[num++] = new Rectangle(l, w);
break;
case 3:
cout << "Please input a and b:" << endl;
cin >> a >> b;
temp[num++] = new Triangle(a, b);
break;
defult:
cout << "error!" << endl;
}
}
for (int i = 0; i < num; i++)
{
temp[i]->area();
temp[i]->showArea();
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询