I'm having an issue creating a concrete class using polymorphism. I've declared a pure virtual area() function in two different classes and one pure virtual volume() function in one of these classes. In their derivative classes, I have overridden these functions but my compiler is telling me that the derivative classes are abstract and can not be instantiated. Any ideas why? I'm sure it's something simple that I'm overlooking. Thanks for your help, Adam.
#ifndef THREEDIM_H
#define THREEDIM_H
#include "Shape.h"
class ThreeDimensionalShape : public Shape
{
public:
// Write the constructor for 3-dimensional shape
ThreeDimensionalShape( double = 0.0, double = 0.0 );
// declare a pure virtual member function area
virtualdouble area( double ) const = 0;
// declare a pure virtual member function volume
virtualdouble volume( double ) const = 0;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef SQUARE_H
#define SQUARE_H
#include "TwoDimensionalShape.h"
// write statement for square public inheritance from 2-dimensional class
class Square : public TwoDimensionalShape
{
public:
Square( double = 0.0, double = 0.0, double = 0.0 );
double getSideLength() const;
virtualdouble area() const;
virtualvoid print() const;
private:
double sideLength;
};
#endif
A derived class must give some implementation (can be virtual) of all pure virtual functions of the base class. In your case, class Shape has a pure virtual function print() which is not implemented in the derived classes TwoDinensionalShape and ThreeDimensionalShape. To me, this raises a red flag.
Your TwoDimensionalShape::area() function takes a double as an argument. But the one in Square does not. This means that it is not implementing the pure virtual function. Functions with different parameters are considered different functions in C++. So you can not implement a pure virtual function with a different function of the same name. It MUST have the same parameter specifications.
Thanks for the reply, but a derived class must only give some implementation if it is to be concrete. The TwoDimensionalShape and ThreeDimensionalShape classes are abstract, not concrete.
The derived class MUST implement EXACTLY what the abstract parent class says it should implement. If the parent class specifies a pure virtual function then the derived class MUST implement it if it wants to be able to be instantiated.
Galik, Thank you very much. I knew it was something simple. I've corrected the pure virtual functions and my Cube classes and Square classes can now be instantiated.
Now, I'm getting some different errors.
1 2 3 4
Square.obj : error LNK2028: unresolved token (0A000297) "public: __thiscall TwoDimensionalShape::TwoDimensionalShape(double,double)" (??0TwoDimensionalShape@@$$FQAE@NN@Z) referenced in function "public: __thiscall Square::Square(double,double,double)" (??0Square@@$$FQAE@NNN@Z)
1>Cube.obj : error LNK2028: unresolved token (0A000279) "public: __thiscall ThreeDimensionalShape::ThreeDimensionalShape(double,double)" (??0ThreeDimensionalShape@@$$FQAE@NN@Z) referenced in function "public: __thiscall Cube::Cube(double,double,double)" (??0Cube@@$$FQAE@NNN@Z)
1>Square.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoDimensionalShape::TwoDimensionalShape(double,double)" (??0TwoDimensionalShape@@$$FQAE@NN@Z) referenced in function "public: __thiscall Square::Square(double,double,double)" (??0Square@@$$FQAE@NNN@Z)
1>Cube.obj : error LNK2019: unresolved external symbol "public: __thiscall ThreeDimensionalShape::ThreeDimensionalShape(double,double)" (??0ThreeDimensionalShape@@$$FQAE@NN@Z) referenced in function "public: __thiscall Cube::Cube(double,double,double)" (??0Cube@@$$FQAE@NNN@Z)