# 图形类设计——c++圆、矩形、三角形类的设计

1、Cirlce 类基本信息：圆心坐标、半径；
Rectangle 类基本信息：长、宽；
Triangle 类基本信息：三个顶点坐标；

2、每个图形类有多个构造函数：缺省构造函数、带参数的构造函数；
3、每个图形类有计算图形的面积 GetArea()，显示图形的基本信息函数 Show()，修改基

#include <iostream>
#include<cmath>
#define PI 3.14
using namespace std;
class Cirlce {
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 c_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::c_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 << "修改后，";
c_show();
}

class Rectangle {
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 r_show();
void r_set(float wide0, float len0);
private:
float wide;
float len;
};
float Rectangle::GetArea() {
return wide * len;
}
void Rectangle::r_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 << "修改后，";
r_show();
}

class Triangle {
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 t_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::t_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() {
//圆的部分测试：用引用来进行值的传递
cout << "请输入圆的坐标和半径：";
float x1, y1, r1;
cin >> x1 >> y1 >> r1;
Cirlce c1(x1, y1, r1);
c1.c_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);
r2.r_show();
cout << "请问你想修改矩形的数据吗？输入1修改，输入0不修改。";
if (cin >> flag && flag) {
cout << "请输入新宽和长：";
cin >> wide1 >> len1;
r2.r_set(wide1, len1);
}

//三角形的测试：
cout << "请输入三角形顶点的三个坐标：";
float coordinate[7];
Triangle t1;
tran(coordinate, t1);//如果可以的话将坐标转换为a,b,c三条边的长度。并将对象初始化。否则重新输入坐标
t1.t_show();
cout << "请问你想修改三角形的数据吗？输入1修改，输入0不修改。";
if (cin >> flag && flag) {
cout << "请输入新三角形的坐标：";
tran(coordinate, t1);
t1.t_show();
}
return 0;
}



03-30 1750

05-23 247

05-25 328

05-26 7663

05-19 825

10-06 4万+

04-28 331

05-25

12-18

10-08

05-28 246

11-27 4182

04-26 1083

03-22 2万+

10-17 100

03-08 868

12-26 1万+

02-22 6380

10-26 1万+

#### 定义一个三角形类Ctriangle，求三角形面积和周长。

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试