继承了父类的函数显示未定义
Shape.h
#ifndef SHAPE_H #define SHAPE_H class Shape { public: Shape(); ~Shape(); virtual double calcArea(); protected: private: }; #endif // SHAPE_H
Shape.cpp
#include "Shape.h" #include<iostream> using namespace std; Shape::Shape() { cout<<"Shape()"<<endl; } Shape::~Shape() { cout<<"~Shape()"<<endl; } virtual double Shape::calcArea() { cout<<"Shape::calcArea()"<<endl; return 0; }
Circle.h
#ifndef CIRCLE_H #define CIRCLE_H #include "Shape.h" class Circle : public Shape { public: Circle(double r); ~Circle(); protected: double m_dR; private: }; #endif // CIRCLE_H
Circle.cpp
#include "Circle.h" #include<iostream> using namespace std; Circle::Circle(double r) { cout<<"Circle"<<endl; m_dR = r; } Circle::~Circle() { cout<<"~Circle()"<<endl; } double Circle::calcArea() { cout<<"Circle::calcArea()"<<endl; return 3.14*m_dR*m_dR; }
Rect.h
#ifndef RECT_H #define RECT_H #include "Shape.h" class Rect : public Shape { public: Rect(double width,double height); ~Rect(); protected: double m_dWidth; double m_dHeight; private: }; #endif // RECT_H
Rect.cpp
#include "Rect.h" #include<iostream> using namespace std; Rect::Rect(double width,double height) { cout<<"Rect()"<<endl; m_dWidth = width; m_dHeight = height; } Rect::~Rect() { cout<<"~Rect()"<<endl; } double Rect::calcArea() { cout<<"Rect::calcArea()"<<endl; return m_dHeight*m_dWidth; }
demo.cpp
#include<iostream> using namespace std; #include "Rect.h" #include "Circle.h" int main() { Shape *shape1 = new Circle(4.3); Shape *shape2 = new Rect(2.0,3.0); shape1->calcArea(); shape2->calcArea(); delete shape1; shape1 = NULL; delete shape2; shape2 = NULL; system("pause"); return 0; }
error:
D:\code\C++\C++远征之多态\计算面积\src\Circle.cpp|16|error: no 'double Circle::calcArea()' member function declared in class 'Circle'|
求大神指导!!