实现一个shape类_c++_Problem 2. Shap

题目

这个问题包括实现一个抽象类Shape,从这个抽象类派生出具体的子类Square、Rectangle、Triangle和Circle。为了简化,这里的三角形指的是一个直角三角形(一个有90°角的三角形)。Shape类应该具有允许访问区域、周长和角的数量的成员函数。这些函数应该命名为CalculateArea、CalculatePerimeter和NumberCorners。由于所有子类都应该具有相同的函数,但它们的计算可能不同,因此您应该考虑将其中一些类编写为虚函数。此外,这四种形状不能以类似的方式创建。因此,它们应该有不同的构造函数。(所有参数都是double类型。)

代码

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>
#include <list>
#include <iomanip>

const double PI = 3.14159;

/***********************************************************************
* Declaration/Definition of the base-class Shape *
***********************************************************************/
class Shape {
public:
    // constructors and destructor
    Shape() {};
    virtual ~Shape() {};
    // Accessors; you should decide which should be virtual or pure virtual.
    virtual double CalculateArea() const =0;
    virtual double CalculatePerimeter() const = 0;
    virtual int NumberCorners() const = 0;
private:
    // member variables;
};


/***********************************************************************
* Declaration/Definition of the child-classes *
***********************************************************************/
class Square : public Shape {
public:
    Square(double a1) :Shape(){
        a = a1;
    }
    ~Square() {
        std::cout << "A Square has been destroyed."<<std::endl;
    }
    double CalculateArea() const {
        return a * a;
    }
    double CalculatePerimeter() const{
        return 4 * a;
    }
    virtual int NumberCorners() const {
        return 4;
    }
private:
    double a;
};
class Rectangle : public Shape {
public:
    Rectangle(double a1,double a2) :Shape() {
        a = a1;
        b = a2;
    }
    ~Rectangle() {
        std::cout << "A Rectangle has been destroyed." << std::endl;
    }
    double CalculateArea() const {
        return a * b;
    }
    double CalculatePerimeter() const {
        return 2 * (a+b);
    }
    virtual int NumberCorners() const {
        return 4;
    }
private:
    double a,b;
};
class Triangle : public Shape {
public:
    Triangle(double a1, double a2) :Shape() {
        a = a1;
        b = a2;
    }
    ~Triangle() {
        std::cout << "A Triangle has been destroyed." << std::endl;
    }
    double CalculateArea() const {
        return a * b*0.5;
    }
    double CalculatePerimeter() const {
        return (a + b) + sqrt(a * a + b * b);
    }
    virtual int NumberCorners() const {
        return 3;
    }
private:
    double a, b;
};
class Circle : public Shape {
public:
    Circle(double a1) :Shape() {
        a = a1;
    }
    ~Circle() {
        std::cout << "A Circle has been destroyed." << std::endl;
    }
    double CalculateArea() const {
        return PI * a * a;
    }
    double CalculatePerimeter() const {
        return 2 * a * PI;
    }
    virtual int NumberCorners() const {
        return 0;
    }
private:
    double a;
};
/************************************************************************
* Main Function which is creating/reporting database (do not change!) *
************************************************************************/
int main() {
    //initialize an empty list of shapes
    std::list<Shape*> shapeDatabase;
    //interact with the user: ask the user to enter shapes one by one
    while (1) {
        //print instructions as to how to enter a shape
        std::cout << "Enter a type (Circle, Triangle, Square, or Rectangle) ";
        std::cout << "and one or two size parameters, ";
        std::cout << "separated with blank spaces:\n";
        double size1;
        double size2;
        //check which shape has been requested and store in the database
        std::string shapeType;
        std::cin >> shapeType;
        if (shapeType == std::string("Circle")) {
            std::cin >> size1;
            shapeDatabase.push_back(new Circle(size1));
        }
        else if (shapeType == std::string("Triangle")) {
            std::cin >> size1 >> size2;
            shapeDatabase.push_back(new Triangle(size1, size2));
        }
        else if (shapeType == std::string("Square")) {
            std::cin >> size1;
            shapeDatabase.push_back(new Square(size1));
        }
        else if (shapeType == std::string("Rectangle")) {
            std::cin >> size1 >> size2;
            shapeDatabase.push_back(new Rectangle(size1, size2));
        }
        else {
            std::cout << "Unrecognized shape!!\n";
        }
        //check if the user wants to add more shapes
        std::cout << "Do you want to add more shapes? (Enter Y or N)\n";
        std::string answer;
        std::cin >> answer;
        if (answer != std::string("Y"))
            break;
    }
    //iterate through the list and output the area, perimeter,
    //and number of corners of each entered shape
    std::list<Shape*>::iterator it = shapeDatabase.begin();
    int shapeCounter = 0;
    while (it != shapeDatabase.end()) {
        std::cout << "Properties of shape " << shapeCounter++ << ":\n";
        std::cout << "Area: " << std::setprecision(5) << (*it)->CalculateArea() << "\n";
        std::cout << "Perimeter: " << std::setprecision(5) << (*it)->CalculatePerimeter() << "\n";
        std::cout << "Corners: " << (*it)->NumberCorners() << "\n";
        std::cout << std::endl;
        it++;
    }

    it = shapeDatabase.begin();
    while (it != shapeDatabase.end()) {
        delete (*it);
        it = shapeDatabase.erase(it);
    }

    return 0;
}

运行结果

请添加图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清欢_小铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值