题目
这个问题包括实现一个抽象类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;
}