sorry about the lenght of code:

``````#include <iostream>

using namespace std;

class Shape
{
public:
Shape(int,int);
Shape ();
virtual  ~Shape();
virtual void print()const = 0;
virtual double calArea() = 0;

protected:
int  centreX,centreY;
};

////// CONSTRUCTORS  ////////////////////
Shape::Shape(int x, int y)
{
centreX = x;
centreY = y;
}

Shape::Shape()
{
centreX=0;
centreY=0;
}

///// DESTRUCTOR ///////////////////////

Shape::~Shape()
{ }

/////  Virtual functions ////////////////////////

void Shape::print()const
{ }

double Shape::calArea()
{ }

class Circle:public Shape
{
public:
Circle(int x,int y,int r);
~Circle ();
void print()const;
double calArea()const;

protected:
};

class Rectangle:public Shape
{
public:
Rectangle( int, int, int, int);
~Rectangle();
void print()const;
double calArea()const;

protected:
int height;
int width;
};

Circle::Circle (int x,int y, int r) : Shape(x,y)
{
}

Circle::~Circle()
{}

void Circle::print(void)const
{
cout << "Circle with radius " << radius << " at " << centreX << "," <<
centreY <<endl;
}

Rectangle::Rectangle(int x,int y,int h,int w)
:Shape(x,y)
{
height=h;
width = w;
}

double Circle::calArea()const
{
return 3*3;
}

Rectangle::~Rectangle()
{}

void Rectangle::print(void)const
{
cout <<"Rectangle is at " << centreX <<','<< centreY
<< " Height:" << height << " width:" << width <<endl;
}

double Rectangle::calArea()const
{
return 2*2;
}
int main()

{

return 0;
}``````

This is the error it generates! Really confusing!

C:\computer science programming\CPP2\excerise sheet 8\Question 3 - shape\main.cpp(45) : error C4716: 'Shape::calArea' : must return a value

You have got to be kidding! The error says that calArea needs to return a value. The definition of calArea is:

``````double Shape::calArea()
{ }``````

How is that confusing? All you need to do is return a value:

``````double Shape::calArea()
{
return 0;
}``````

Sorry, maybe it was the sheet that made it al confusing since the teacher said ...

Add a pure virtual member function, double calcArea() to the Shape class. The function calculates and returns the area of an object. In the Shape class this function has an ‘empty’ implementation, which is over-ridden in the derived classes..

So to me by adding return 0; its no longer empty

edit: By deleting the function body from the virtual function it works! But not with the destructor.... any reason for this?

>In the Shape class this function has an ‘empty’ implementation, which is over-ridden in the derived classes
In Shape calArea needs no implementation because pure virtual member functions make a class abstract. In the derived class Circle, it must have an implementation or the class inherits abstract behavior from Shape, and can't be instantiated. I agree that the wording is vague. I've found that it's better to make a clear distinction between function declarations and function definitions, then stick to that terminology.

If you define a pure virtual function then it still must follow the rules, such as returning a value.

>By deleting the function body from the virtual function it works!
Yes, because it doesn't need a definition. Only a declaration that the derived classes redefine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.