#include#include #include "Shape.h" #include "Circle.h" #include "Rectangle.h" #include "RoundRectangle.h" int main() { Shape *ptrShape[6]; //Shape shape1,shape2("red");抽象类不能被实例化 Circle circle1, circle2("blue", 2); Rectangle rect1, rect2("black", 5, 4); RoundRectangle round1, round2("green", 5, 4, 2); ptrShape[0] = &circle1; ptrShape[1] = &circle2; ptrShape[2] = &rect1; ptrShape[3] = &rect2; ptrShape[4] = &round1; ptrShape[5] = &round2; for (int i = 0; i < 6; i++) cout << "Area = " << ptrShape[i]->getArea() << endl; cout << "程序结束" << endl; return 0; }
//
// Created by 63400 on 2021/10/15.
//
#ifndef UNTITLED_SHAPE_H
#define UNTITLED_SHAPE_H
using namespace std;
class Shape {
private:
string color;
public:
Shape();
Shape(string c);
virtual ~Shape();
//在所有类的析构函数前面加上virtual关键词
//或者只在父类的析构函数前面加virtual
string getColor();
void setColor(string c);
virtual double getArea() = 0; //Shape类变为抽象类,不能被实例化
void display();
};
#endif //UNTITLED_SHAPE_H
只需要在.h文件的函数前加virtual即可,cpp文件不需要加。
D:Clionuntitledcmake-build-debuguntitled.exe Shape default constructor called. Circle default Constructor called. Shape constructor called. Circle constructor called. Shape default constructor called. Rectangle default constructor called. Shape constructor called. Rectangle constructor called. Shape default constructor called. Rectangle default constructor called. RoundRectangle default Constructor called. Shape constructor called. Rectangle constructor called. RoundRectangle constructor called. Area = 3.14159 Area = 12.5664 Area = 1 Area = 20 Area = 0.141593 Area = 16.5664 程序结束 RoundRectangle destructor called. Rectangle destructor called. Shape default constructor called. RoundRectangle destructor called. Rectangle destructor called. Shape default constructor called. Rectangle destructor called. Shape default constructor called. Rectangle destructor called. Shape default constructor called. Circle destructor called. Shape default constructor called. Circle destructor called. Shape default constructor called. Process finished with exit code 0作业2:
#include#include #include "Shape.h" #include "Circle.h" #include "Rectangle.h" #include "RoundRectangle.h" double sumArea(Shape *shapes[], int n) { double sum = 0; for (int i = 0; i < n; ++i) { sum += shapes[i]->getArea(); } return sum; } int main() { const int N = 4; Shape *shapes[N] = { new Circle("red", 5), new Circle("green", 5), new Rectangle("blue", 5, 3), new RoundRectangle("blue", 5, 3, 1), }; double totalArea = sumArea(shapes, N); cout << "total area = " << totalArea << endl; for (int i = 0; i < N; ++i) { delete shapes[i]; } return 0; }
Shape.h
//
// Created by 63400 on 2021/10/15.
//
#ifndef UNTITLED_SHAPE_H
#define UNTITLED_SHAPE_H
using namespace std;
class Shape {
private:
string color;
public:
Shape();
Shape(string c);
virtual ~Shape();
//在所有析构函数前面加上virtual关键词
string getColor();
void setColor(string c);
virtual double getArea() = 0;
void display();
};
#endif //UNTITLED_SHAPE_H
结果:
D:Clionuntitledcmake-build-debuguntitled.exe Shape constructor called. Circle constructor called. Shape constructor called. Circle constructor called. Shape constructor called. Rectangle constructor called. Shape constructor called. Rectangle constructor called. RoundRectangle constructor called. total area = 186.221 Circle destructor called. Shape default constructor called. Circle destructor called. Shape default constructor called. Rectangle destructor called. Shape default constructor called. RoundRectangle destructor called. Rectangle destructor called. Shape default constructor called. Process finished with exit code 0



