Issue with Pure Virtual Function

I'm attempting to write a basic program that demonstrates the use of pure virtual functions in classes. For some reason though, after I've defined my virtual base class and my derived class with the overwriten pure virtual function, I cannot use my derived class without the following error coming up,

"Object of abstract type "derived class" is not allowed: Pure Virtual Function "Base Class::Virtual Function" has no overrider."


Contents of my Virtual Abstract Base Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef BASICSHAPE_H
#define BASICSHAPE_H 

class BasicShape{
protected:

	// Instance Member Variable(s)
	double area;

	// Static Member Variable(s)

	// Array Constructor(s)

public:

	// Default Constructor
	BasicShape(); 

	// Additional Constructor(s)

	// Copy Constructor(s)

	// Mutator(s)
	virtual void calcArea() const = 0; // Pure virtual function

	// Accessor(s)
	double getArea(); 

	// Operator Overloader(s)

	// Friend(s)

	// Destructor(s)
	virtual ~BasicShape(); 

};
#endif


Contents of BasicShape.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "BasicShape.h"

BasicShape::BasicShape(){

}

double BasicShape::getArea(){

	return area; 
}

BasicShape::~BasicShape(){

}


Contents of Derived Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "BasicShape.h"

#ifndef CIRCLE_H
#define CIRCLE_H

class Circle : public BasicShape{
private:

	// Instance Member Variable(s)
	long centerX;
	long centerY;
	double radius; 

	// Static Member Variable(s)

	// Array Constructor(s)

public:

	// Default Constructor
	Circle(long, long, double); 

	// Additional Constructor(s)

	// Copy Constructor(s)

	// Mutator(s)
	void calcArea(double); 

	// Accessor(s)
	long getCenterX(); 
	long getCenterY(); 

	// Operator Overloader(s)

	// Friend(s)

	// Virtual Function(s)

	// Destructor(s)
	 ~Circle(); 
};

#endif 


Contents of Circle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "Circle.h"

Circle::Circle(long nX, long nY, double nRadius){

	centerX = nX;
	centerY = nY; 
	radius = nRadius;
	Circle::calcArea(radius); 
}

long Circle::getCenterX(){

	return centerX; 
}

long Circle::getCenterY(){

	return centerY; 
}

void Circle::calcArea(double nRadius){

	radius = nRadius; 
	area = (3.14159 * radius * radius);
}


Circle::~Circle(){
}


Contents of Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <iostream>
#include "Circle.h"
#include "Rectangle.h"

int main(){ 

   // Error described above thrown here, at underlined object names. 
   Circle c(1, 1, 50); 
   Rectangle s(5, 5); 

.
.
.


Last edited on
You did not actually override the pure virtual function calcArea() causing the derived class to be of abstract type also

Look @ this pure virtual function
virtual void calcArea() const = 0; // Pure virtual function
and this :
void calcArea(double); // doesn't match w/ the function above

I think calcArea() should be non-constant because you will modify the member area_
( or declare area_ as mutable )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>

struct Shape
{
    Shape() : area_( 0.0 ) { }
    
    virtual void calcArea() = 0; // no parameters, non const
    
    double getArea() { return area_; }
    
protected :
    double area_;
};

struct Rectangle : public Shape
{
    Rectangle( double width, double height )
        : width_( width ), height_( height )
    { }
    
    void calcArea() { area_ = this->width_ * this->height_; } // override the pvf
    
private :
    double width_;
    double height_;
};

int main ()
{
    Rectangle r( 100.11, 101.12 );
    r.calcArea();
    std::cout << "Area of rectangle is: " << std::setprecision(2) << std::fixed << r.getArea();
}
Last edited on
Hey, I'm doing the same homework right now! xD You in Hardnett's class? I see you made area protected instead of private, even though it says to make it private in the book. I'm considering doing the same thing because I can't figure out how to make this work otherwise.
If you want to make the area private instead of protected you can simply use the public interface you already have for the Shape class, i.e. using GetArea().
Nvrmind's response worked. The protected/private issue mentioned by theperson was an issue as well.

PS. theperson, ;)
Topic archived. No new replies allowed.