@图灵不吃苹果 #C++图形基类Shape 派生类Rectangle、Circle、Triangle类

@图灵不吃苹果
C++图形基类和派生类的实现(带注释)

实验要求

设计一个图形基类:Shape,在此基础上派生Rectangle、Circle、Triangle 类。

1、Cirlce 类基本信息:圆心坐标、半径;
Rectangle 类基本信息:长、宽;
Triangle 类基本信息:三个顶点坐标;
其中:成员变量为
private 属性,成员函数为public 属性;
2、每个图形类有多个构造函数:缺省构造函数、带参数的构造函数;
3、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show(),修改基
本信息的函数Set(形参)。

[实验提示]
1、在*派生类的构造函数中,应注意对基类数据成员的初始化;

2、各类图形面积的计算方法不同, 因此不能在基类 Shape 中统一确定计算的方法。在

基类Shape 中实现计算面积功能的函数体GetArea()应为空,在派生类中根据同名覆盖原则

定义各自的同名函数实现具体功能

[测试数据]

Circle:

圆心(30,45) 半径:35

圆心(50,89) 半径:23

Rectangle:长:30 宽:53

长:28 宽:24

Triangle:

顶点:(34,45)、(89,45)、(54,67)

顶点:(22,34)、(67,43)、(86,64)

代码部分:

头文件部分:shape.h

#pragma once
#ifndef Shape_h
#define Shape_h
#define PI 3.141;
class shape
{
public:
 shape(){}
 ~shape(){}  //具备多态特性的类的析构函数有必要声明为virtual函数
 virtual double getarea() { return -1; }  //虚函数 系统分辨成员对象的真实性 调用了各自的重载函数
};
class circle:public shape   //继承shape类
{
public:
 circle();
 double getarea();
 void show();
 void set(int a, int b, int c);
private:
 int x1;
 int y1;
 int r;
};
class rectangle:public shape
{
public:
 rectangle();
 double getarea();
 void show();
 void set(int a, int b);
private:
 int chang;
 int kuan;
};
class triangle:public shape
{
public:
 triangle();
 double getarea();
 void show();
 void set(int a, int b, int c, int d, int e, int f);
 private:
 int x31;
 int y31;
 int x32;
 int y32;
 int x33;
 int y33;
 double l1;
 double l2;
 double l3;
};
#endif // !Shape_h
#pragma once

shape.cpp部分:

#include "shape.h"
#include <iostream>
#include <math.h>
using namespace std;
circle::circle()
{
 x1 = 0;
 y1 = 0;
 r = 0;
}
double circle::getarea()
{
 double s;
 s = PI;
 s = s * r * r;
 return s;
}
void circle::show()
{
 double c;
 c = PI;
 c = c * 2 * r;
 cout << "(" << x1 << "," << y1 << ")" << endl;//坐标;
 cout << "2R =" << 2 * r << endl;//直径;
 cout << "circum =" << c << endl;//周长;
}
void circle::set(int a, int b, int c)
{
 this->x1 = a;
 this->y1 = b;
 this->r = c;
}
rectangle::rectangle()
{
 chang = 0;
 kuan = 0;
}
double rectangle::getarea()
{
 int s;
 s = chang * kuan;
 return s;
}
void rectangle::show()
{
 cout << "chang = " << chang << endl;
 cout << "kuan = " << kuan << endl;
 cout << "rectangle circum = " << 2 * (chang + kuan) << endl;
}
void rectangle::set(int a, int b)
{
 chang = a;
 kuan = b;
}
triangle::triangle() {
 x31 = 0;
 y31 = 0;
 x32 = 0;
 y32 = 0;
 x33 = 0;
 y33 = 0;
 l1 = 0;
 l2 = 0;
 l3 = 0;
}
double triangle::getarea()
{
 double s;
 double p;
 p = (l1 + l2 + l3) / 2;
 s = p * (p - l1) * (p - l2) * (p - l3);
 s = sqrt(s);
 return s;
}
void triangle::show()
{
 cout << "location:\n(" << x31 << "," << y31 << ")" << endl;
 cout << "(" << x32 << "," << y32 << ")" << endl;
 cout << "(" << x33 << "," << y33 << ")" << endl;
 double a, b, c, c1;
 a = pow((x31 - x32), 2) + pow((y31 - y32), 2);
 a = sqrt(a);
 b = pow((x31 - x33), 2) + pow((y31 - y33), 2);
 b = sqrt(b);
 c = pow((x32 - x33), 2) + pow((y32 - y33), 2);
 c = sqrt(c);
 c1 = a + b + c;
 l1 = a;
 l2 = b;
 l3 = c;
 cout << "triangle circum = " << c1 << endl;
}
void triangle::set(int a, int b, int c, int d, int e, int f)
{
 x31 = a;
 y31 = b;
 x32 = c;
 y32 = d;
 x33 = e;
 y33 = f;
}

main.cpp部分

// C++code5.1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//circle rectangle triangle 是shape的继承类
//start系列的文件是初始化三角形 圆 正方形
//getarea是获取面积
//show是显示图形信息
//set是设置图形的基本信息
//三角形求面积用了海伦公式
//用while循环确保输入的长宽等必要信息符合要求(>0)
//用switch设置循环菜单形式 可多次进行调试
//类里面的信息用private 函数用public
#include <iostream>  //输入输出流头文件
#include "shape.h"  //导入头文件

using namespace std;
void startcircle();
void startrectangle();
void starttriangle();
int main()
{
    while (true)
    {
        int shape;
        cout << "please input shape: \n 1.circle 2.rectangle 3.triangle\n( input 0 quit)" << endl;
        cin >> shape;
        switch (shape)
        {
        case 1:startcircle(); break;
        case 2:startrectangle(); break;
        case 3:starttriangle(); break;
        default:return 0;
        }
        
    }
}
void startcircle()
{
    int a, b, r1;
    cout << "please input circle x1,y1,r;" << endl;//x1,y1为圆心坐标 r为半径
    cin >> a >> b >> r1;
    while (r1 < 0)
    {
        cout << "请输入正确的半径:(>0)" << endl;
        cin >> r1;
    }
    circle yuan;
    yuan.set(a, b, r1);
    double s;
    s = yuan.getarea();
    cout << "the circle s = " << s << endl;
    yuan.show();
}
void startrectangle()
{
    int x2, y2;
    cout << "please input rectangle x2,y2;" << endl;//x2,y2为长,宽
    cin >> x2 >> y2;
    while (x2 < 0 || y2 < 0)   //长宽都应该>0
    {
        cout << "重新输入长和宽(>0)" << endl;
        cin >> x2 >> y2;
    }
    rectangle rec;
    rec.set(x2, y2);
    double s;
    s = rec.getarea();
    cout << "the rectangle s = " << s << endl;
    rec.show();
}
void starttriangle() {
    int x31, y31, x32, y32, x33, y33;
    cout << "input 3 point location:\n1:";//输入三个顶点坐标
    cin >> x31 >> y31;
    cout << "2:";
    cin >> x32 >> y32;
    cout << "3:";
    cin >> x33 >> y33;
    triangle tri;
    tri.set(x31, y31, x32, y32, x33, y33);
    tri.show();
    double sq;
    sq = tri.getarea();
    cout << "the triangle s = " << sq << endl;
    
}
    

  • 8
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值