Hi Everybody
I wrote this program to experiment with dynamic binding. One class inherits from another which inherits from another. The user creates a new object of their choice at run time and 2 functions within that object should be called but it doesn't seem to be working out that way. Maybe the problem is with the pointer?
Can anyone help?
Thanks for your time!

#include <iostream>
using namespace std;

class shape
{
public:
	shape(){cout << "Shape Constructor\n" ;}
	~shape(){cout << "Shape Destructor\n" ;}
	int calcArea();
	void speak();
	void setArea(int);
	void nameOfShape();
protected:
	int itsArea;
};

int shape::calcArea(){return itsArea;}
void shape::speak() {cout << "How can a shape speak?\n";}
void shape::setArea(int Area){itsArea=Area;}
void namOfShape(){cout << "shape\n";}

class rectangle : public shape
{
public:
	rectangle(){cout << "Rectangle Constructor\n" ;}
	~rectangle(){cout << "Rectangle Destructor\n" ;}
	int calcArea();
	void nameOfShape();
};

void namOfShape(){cout << "rectangle\n";}
int rectangle::calcArea()
{	int Area, width, length; 
	cout << "What's the width?\n";
			cin >> width;
	cout << "What's the length?\n";
			cin >> length;
			Area = width * length;
			return Area;
}


class square:public rectangle
{
public:
	square(){cout << "Square Constructor\n" ;}
	~square(){cout << "Square Destructor\n" ;}
	int calcArea();
	void nameOfShape();

};
void namOfShape(){cout << "square\n";}
int calcArea()
	{int c, side;
	cout << "How long is the side of this square?\n";
		cin >> side;
	c = side * side;
	return c;}

int callCalcArea(shape*);
void callNameShape(shape*);

int main ()
{
	
	shape* ptr=0;
	int choice;
	while(1)
	{
		bool fQuit = false;
	cout << "(0)Quit (1)Shape (2)Rectangle (3)Square:\n";
	cin >> choice;
	
	switch (choice)
	{
	case 0: fQuit = true;
	break;
	case 1: ptr = new shape;	
	break;
	case 2: ptr = new rectangle;
	break;
	case 3: ptr = new square;
	default: break;
	}
	
	if (fQuit == true)
		break;
	
	cout << "The area of the new " << callNameShape(ptr) << "is " << callCalcArea(ptr) << ".\n";
	}
		return 0;
}

int callCalcArea(shape *pShape1)
{pShape1->calcArea();}
void callNameShape(shape *pShape2)
{pShape2->nameOfShape();}

Recommended Answers

All 5 Replies

Hi Thanks
Virtual functions are one of the issues. In think there are more. I have added virtual functions for the overloaded function calcArea() in the shape and rectangle classes and also for the destructors, but the same problem.
Any more ideas?

#include <iostream>
using namespace std;

class shape
{
public:
	shape(){cout << "Shape Constructor\n" ;}
	virtual ~shape(){cout << "Shape Destructor\n" ;}
	virtual int calcArea();
	void speak();
	void setArea(int);
	virtual void nameOfShape();
protected:
	int itsArea;
};

int shape::calcArea(){return (0);}
void shape::speak() {cout << "How can a shape speak?\n";}
void shape::setArea(int Area){itsArea=Area;}
void namOfShape(){cout << "shape\n";}

class rectangle : public shape
{
public:
	rectangle(){cout << "Rectangle Constructor\n" ;}
	virtual ~rectangle(){cout << "Rectangle Destructor\n" ;}
	int virtual calcArea();
	virtual void nameOfShape();
};

void namOfShape(){cout << "rectangle\n";}
int rectangle::calcArea()
{	int Area, width, length; 
	cout << "What's the width?\n";
			cin >> width;
	cout << "What's the length?\n";
			cin >> length;
			Area = width * length;
			return Area;
}


class square:public rectangle
{
public:
	square(){cout << "Square Constructor\n" ;}
	~square(){cout << "Square Destructor\n" ;}
	int calcArea();
	void nameOfShape();

};
void namOfShape(){cout << "square\n";}
int calcArea()
	{int c, side;
	cout << "How long is the side of this square?\n";
		cin >> side;
	c = side * side;
	return c;}

int callCalcArea(shape*);
void callNameShape(shape*);

int main ()
{
	
	shape* ptr=0;
	int choice;
	while(1)
	{
		bool fQuit = false;
	cout << "(0)Quit (1)Shape (2)Rectangle (3)Square:\n";
	cin >> choice;
	
	switch (choice)
	{
	case 0: fQuit = true;
	break;
	case 1: ptr = new shape;	
	break;
	case 2: ptr = new rectangle;
	break;
	case 3: ptr = new square;
	default: break;
	}
	
	if (fQuit == true)
		break;
	
	cout << "The area of the new " << callNameShape(ptr) << "is " << callCalcArea(ptr) << ".\n";
	}
		return 0;
}

int callCalcArea(shape *pShape1)
{pShape1->calcArea();}
void callNameShape(shape *pShape2)
{pShape2->nameOfShape();}

Did I do the (code) right?

Did I do the (code) right?

As you can see: No.
Perhaps you should actually click the link provided to you by Fbody..

You haven't posted any sort of information regarding the specifics of your errors, so I'm making some wild guesses here.

Line 27, you wrote int virtual instead of virtual int, I don't think that is technically correct.

Also, I see several missing scope resolutions (Lines 20, 31, 52, 53, maybe more)

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.