小编Cel*_*ery的帖子

工厂设计模式中的纯虚函数错误

学习期末考试并决定构建一个使用纯虚函数和多态性的程序。我陷入了一个非常奇怪的错误,也许我错过了一些东西。

这是 Shape 抽象类

#ifndef Shape_hpp
#define Shape_hpp

#include <stdio.h>
#include <string.h>

class Shape{
    const char* name;
public:
    Shape(const char* abc);
    virtual double getPerimeter()=0;
    virtual double getArea()=0;
};

#endif /* Shape_hpp */
Run Code Online (Sandbox Code Playgroud)

Shape .cpp 实现文件

#include "Shape.hpp"

Shape::Shape(const char *shape){
    name = shape;
}
Run Code Online (Sandbox Code Playgroud)

圆头文件

#ifndef Circle_hpp
#define Circle_hpp

#include "Shape.hpp"
#include <stdio.h>

class Circle:public Shape{
    double m_radius;
public:
    Circle(double rad);
    double getRadius();            
};

#endif /* Circle_hpp */
Run Code Online (Sandbox Code Playgroud)

圆.cpp实现文件

#include "Circle.hpp"
#include "Shape.hpp"

Circle::Circle(double rad):Shape("Circle"){
    m_radius = rad;
}

double …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism inheritance abstract-class pure-virtual

3
推荐指数
1
解决办法
980
查看次数