#include <iostream>
#include <string>
using namespace std;
class shapes
{
protected:
	int width;
	int height;
public:
	shapes()
	{
		width=0;
		height=0;
	}
	~shapes()
	{
	}
	void getWidth()
	{
		cout<<"What is the width"<<endl;
		cin>>width;

	}
	void getHeight()
	{
		cout<<"What is the height"<<endl;
		cin>>height;
	}
};
class rectangle:public shapes
	{
	public:
		void area()
		{
			cout<<"Your area for the rectangle is "<<(height*width)<<endl;
		}

	};
class triangle:public shapes
{
private:
	int side;
	
public:
	void getSide()
	{
		cout<<"What is the side?"<<endl;
		cin>>side;
	}
	void area()
	{
	cout<<"your area for the square is"<< (width*height*side)<<endl;
	}

};
void main()
{

	triangle *pointer=new triangle;
	pointer->getWidth();
	pointer->getHeight();
	pointer->getSide();
	pointer->area();
	
	delete pointer;
	rectangle *pointer=new rectangle;

	pointer->getHeight();
	pointer->getWidth();
	pointer->area();
	delete pointer;
	system("PAUSE");
}

I am getting an error message telling me to "see declaration of 'pointer'".
I am assuming I am not allowed to do this

delete pointer;
	rectangle *pointer=new rectangle;

But I don't understand why.
I deleted pointer, aka deallocated the memory. Then I am creating a new pointer, which just happens to have the same name. Am I not allowed to do this? I thought that was the whole point of using 'pointers'.

Any helpful advise would be greatly appreciated.

Recommended Answers

All 3 Replies

You are not allowed to change the datatype of an object -- in this case you can't reassign the data type from triangle* to rectangle*. Just use a different variable name for each one.

>>I deleted pointer, aka deallocated the memory.
delete operator only deletes the memory assigned to the pointer, not the pointer itself.

>>I thought that was the whole point of using 'pointers'.
You have a misconception of what pointers do. Its just like any other object, you can't have two or more objects with the same name.

Also to what AD said. You must always assign 0 to a pointer after you delete. So that you dont get a pointer hunging around with unknown memory.

bar *foo = new bar;
         foo->set_name("name");
          foo->get_name();
          delete foo;
       foo = 0;

To release 100% the memory and secure the pointer's behaviour.

I really don't want to confuse you but there is another solution you might be interested in. It is called polymorphism. With the use of polymorphism you would only need one type of pointer. One of the benefits of polymorphism is, that you can have the same functions in the inherited classes, working differently. Plus, you don't need to use different type of pointers to access your inherited classes.

In order to do that, you need to make your base class to be an abstract class, declare your functions as virtual in the base class. You can define your functions separately in the inherited classes later, depending on what kind of formula you want to use to calculate the properties of the actual shape. When it is done, you no longer have to use different type of pointers. One pointer of the base class could access all the functions of your objects, regardless of their class type, as long as they are inherited from the base class.

You can read about polymorphism in the following link: http://www.cplusplus.com/doc/tutorial/polymorphism/. Good luck, with whatever solution you choose. :)

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.