题目 1 图形类
用虚函数实现第九次实验中实验一(https://blog.csdn.net/Alask_/article/details/106298890)的不同图形面积的计算。
要求:
使用基类指针,通过基类指针调用确定的派生类对象中的面积计算函数,实现面积算。
[实验提示]
如果在抽象类中定义了带参数的构造函数,而且没有定义默认构造函数,则应在其类中通过构造函数的初始化表对抽象类进行初始化;
基类与派生类的转换:
1、派生类对象可以向基类对象赋值;
2、派生类对象可以替代基类对象向基类对象的引用进行赋值或初始化;
3、如果函数的参数是基类对象或基类对象的引用,相应的实参可以用子类对象
分析:只需要在Shape 中将show函数声明为虚函数。
#include <iostream>
#include<cmath>
#define PI 3.14
using namespace std;
class Shape{
public:
virtual float GetArea() =0;//纯虚函数
virtual void show()=0;
};
class Cirlce :virtual public Shape {
public:
Cirlce() {
x = 0;
y = 0;
r = 1;
}
Cirlce(float r0) {
x = 0;
y = 0;
r = r0;
}
Cirlce(float x0, float y0, float r0) {
x = x0;
y = y0;
r = r0;
}
float GetArea();
void show();
void c_set(float x0, float y0, float r0);
private:
float x;
float y;
float r;
};
float Cirlce::GetArea() {
return PI * r * r;
}
void Cirlce::show() {
cout << "圆心坐标、直径、周长、面积为:" << endl;
cout << "(" << x << "," << y << ")" << " " << 2 * r << " " << PI * r * 2 << " " << GetArea() << endl;
}
void Cirlce::c_set(float x0, float y0, float r0) {
x = x0;
y = y0;
r = r0;
cout << "修改后,";
show();
}
class Rectangle : public Shape {
public:
Rectangle() {
wide = 1;
len = 1;
}
Rectangle(float wide0) {
wide = wide0;
len = 1;
}
Rectangle(float wide0, float len0) {
wide = wide0;
len = len0;
}
float GetArea();
void show();
void r_set(float wide0, float len0);
private:
float wide;
float len;
};
float Rectangle::GetArea() {
return wide * len;
}
void Rectangle::show() {
cout << "矩形宽、长、周长、面积为:" << endl;
cout << wide << "," << len << "," << " " << 2 * (wide + len) << ", " << wide * len << endl;
}
void Rectangle::r_set(float x0, float y0) {
wide = x0;
len = y0;
cout << "修改后,";
show();
}
class Triangle : public Shape {
public:
Triangle() {
a = 1;
b = 1;
c = 1;
}
Triangle(float a0) {
a = a0;
b = 1;
}
Triangle(float a0, float b0, float c0) {
a = a0;
b = b0;
c = c0;
}
float GetArea();
void show();
void set_a(float A) { a = A; }
void set_b(float B) { b = B; }
void set_c(float C) { c = C; }
private://因为涉及到一些数据访问,直接将它设置为公有了
float a;
float b;
float c;
};
float Triangle::GetArea() {
float p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
void Triangle::show() {
cout << "三角形三边的长、周长、面积为:" << endl;
cout << a << " " << b << " " << c << "," << a + b + c << "," << GetArea() << endl;
}
void tran(float* arr, Triangle& t) {
for (int i = 0; i < 6; i++)cin >> arr[i];
float A, B, C;//三条边的长度。
A = sqrt(pow((arr[0] - arr[2]), 2) + pow((arr[1] - arr[3]), 2));
B = sqrt(pow((arr[0] - arr[4]), 2) + pow((arr[1] - arr[5]), 2));
C = sqrt(pow((arr[2] - arr[4]), 2) + pow((arr[3] - arr[5]), 2));
bool flag = true;
if (A + B <= C)flag = 0;
if (A + C <= B)flag = 0;
if (B + C <= A)flag = 0;
if (flag == true) {
t.set_a(A);
t.set_b(B);
t.set_c(C);
}
else {
cout << "输入的坐标无法构成三角形,请重新输入坐标:";
tran(arr, t);
}
}
int main() {
Shape *s;//用基类指向派生类。
//圆的部分测试:用引用来进行值的传递
cout << "请输入圆的坐标和半径:";
float x1, y1, r1;
cin >> x1 >> y1 >> r1;
Cirlce c1(x1, y1, r1);
s = &c1;
s->show();//动态多态的体现
cout << "请问你想修改圆的数据吗?输入1修改,输入0不修改。";
bool flag;
if (cin >> flag && flag) {
cout << "请输入新圆的坐标和半径:";
cin >> x1 >> y1 >> r1;
c1.c_set(x1, y1, r1);
}
//矩形部分的测试:
cout << "请输入矩形的宽和长:";
float wide1, len1;
cin >> wide1 >> len1;
Rectangle r2(wide1, len1);
s = &r2;
s->show();//动态多态的体现
cout << "请问你想修改矩形的数据吗?输入1修改,输入0不修改。";
if (cin >> flag && flag) {
cout << "请输入新宽和长:";
cin >> wide1 >> len1;
r2.r_set(wide1, len1);
}
//三角形的测试:
cout << "请输入三角形顶点的三个坐标:";
float coordinate[7];
Triangle t3;
tran(coordinate, t3);//如果可以的话将坐标转换为a,b,c三条边的长度。并将对象初始化。否则重新输入坐标
s = &t3;
s->show();//动态多态的体现
cout << "请问你想修改三角形的数据吗?输入1修改,输入0不修改。";
if (cin >> flag && flag) {
cout << "请输入新三角形的坐标:";
tran(coordinate, t3);
t3.show();
}
return 0;
}
运行结果: