# C++继承与多态练习--计算图形面积

1.目的：

```/*设计一个计算图形面积的类库。

Class Shape
{
virtual void showData()=0;
virtual double reArea()=0;
virtual double reVolume()=0;
};

Rectangle（矩形）和Triangle（三角形）等类。
ThreeShape类派生Ball（球体）、Cylinder（圆柱体）、
RectangularParallelepiped（长方体）等类。

*/```

2.代码如下:

```/*
*This file contains code for C++ 6th experiment
*By LZH
*/
#include<iostream>
#include<string>
using namespace std;
const double PI = acos(-1.0);
// Definition of Shape class and related functions goes here
class Shape
{
virtual void showData() = 0;
virtual double reArea() = 0;
virtual double reVolume() = 0;
};
class TwoDimShape :virtual public Shape {
protected:
double x, y;
public:
virtual void showData() {
return ;
}
virtual double reArea() {
return 0;
}
virtual double reVolume() {
return 0;
}
};
class ThreeShape :virtual public Shape {
protected:
double x, y, z;
public:
virtual void showData() {
return;
}
virtual double reArea() {
return 0;
}
virtual double reVolume() {
return 0;
}
};
class Circle :virtual public TwoDimShape {
public:
Circle(double tp) {
x = tp;
}
Circle(Circle &asp) {
x = asp.x;
}
~Circle()
{

}
void showData() {
cout << "This is a Circle:" << endl
<< "The radiation:" << x << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return PI*x*x;
}
};
class Elipse :virtual public TwoDimShape {
public:
Elipse(double ta, double tb) {
x = ta, y = tb;
}
Elipse(Elipse &asp) {
x = asp.x, y = asp.y;
}
~Elipse() {

}
void showData() {
cout << "This is a Elipse:" << endl
<< "The long axis:" << x << endl
<< "The short axis:" << y << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return PI*x*y;
}
};
class Rectangle :virtual public TwoDimShape {
public:
Rectangle(double ta, double tb) {
x = ta, y = tb;
}
Rectangle(Rectangle &asp) {
x = asp.x, y = asp.y;
}
~Rectangle() {

}
void showData() {
cout << "This is a Rectangle:" << endl
<< "The long axis:" << x << endl
<< "The short axis:" << y << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return x*y;
}
};
class Triangle :virtual public TwoDimShape {
public:
Triangle(double ta, double tb) {
x = ta, y = tb;
}
Triangle(Triangle &asp) {
x = asp.x, y = asp.y;
}
~Triangle() {

}
void showData() {
cout << "This is a Triangle:" << endl
<< "The base length:" << x << endl
<< "The height  :" << y << endl
<< "The area:" << reArea() << endl;
}
double reArea() {
return x*y / 2.0;
}
};
class Ball :virtual public ThreeShape {
public:
Ball(double ta) {
x = ta;
}
Ball(Ball &asp) {
x = asp.x;
}
~Ball() {

}
void showData() {
cout << "This is a Ball:" << endl
<< "The radiation:" << x << endl
<< "The surface area:" << reArea() << endl;
}
double reArea() {
return PI*pow(x, 3)*4.0 / 3.0;
}
double reVolume() {
return PI*x*x;
}
};
class Cylinder :virtual public ThreeShape {
public:
/*
V=PI*r*r*h S=2*PI*r+r*h
*/
Cylinder(double ta, double tb) {
x = ta, y = tb;
}
Cylinder(Cylinder &asp) {
x = asp.x, y = asp.y;
}
~Cylinder() {

}
void showData() {
cout << "This is a Cylinder:" << endl
<< "The radiation:" << x << endl
<< "The height:" << y << endl
<< "The surface area:" << reArea() << endl;
}
double reArea() {
return 2 * PI*x + x*y;
}
double reVolume() {
return PI*x*x*y;
}
};
//RectangularParallelepiped
class cuboid :virtual public ThreeShape {
public:
cuboid(double ta, double tb, double tc) {
x = ta, y = tb, z = tc;
}
cuboid(cuboid &asp) {
x = asp.x, y = asp.y, z = asp.z;
}
void showData() {
cout << "This is a cuboid:" << endl
<< "The length:" << x << endl
<< "The width:" << y << endl
<< "The height" << z << endl
<< "The surface area:" << reArea() << endl;

}
double reArea() {
return 2 * (x*y + x*z + y*z);
}
double reVolume() {
return x*y*z;
}
};
int main(void) {
TwoDimShape a;
ThreeShape b;
TwoDimShape *p = &a;
ThreeShape *w = &b;
Circle t1(1.0);
Elipse t2(1.0, 2.0);
Rectangle t3(10.0,2.3);
Triangle t4(4.0, 5.0);
Ball t5(2.33333);
Cylinder t6(4.5, 65.0);
cuboid t7(132, 5,156);
p = &t1;
p->showData();
p = &t2;
p->showData();
p = &t3;
p->showData();
p = &t4;
p->showData();
w = &t5;
w->showData();
w = &t6;
w->showData();
w = &t7;
w->showData();
return 0;
}```

3 . 测试截图

4.关于多态性

``` 在这个例子中我用了基类指针指向基类，这个不难理解，在类型兼容规则下，
指向基类的指针可以隐式的转换成派生类的指针。
这是最常见的关于多态的用法，利用该指针指向任意一个子类对象，
就可以调用相应的虚函数，指向的子类的不同，实现的方法也就不同。```

129 篇文章36 人订阅

0 条评论

## 相关文章

### php面试题（二）

1 <?php echo count (false); \$a = count ("567") + count(null) + count(false); ec...

47480

31770

### C语言第六讲,数组

C语言第六讲,数组 一丶什么是数组 数组,就是一整块的连续内存空间. 且类型都是一样的.大小一样 比如: ? 1.1数组元素的访问 我们要访...

72230

245100

39650

25290

### 最大连续子序列和

https://blog.csdn.net/bitcarmanlee/article/details/51526010

12520

### C++ STL算法系列3---求和：accumulate

该算法在numeric头文件中定义。 假设vec是一个int型的vector对象，下面的代码： //sum the elements in vec start...

26880

14230

34580