Hi!

I am doing a project that's about inheritance and I've solved the most of it. I have some problems with removing a vehicle (car or motorcycle) because they have different characteristics.

A car has these characteristics: brand, age, regNr, price, fuel, size
A motorcycle has these characteristics: brand, age, regNr, price, storageCape

If they'd had the same characteristics there wouldnt be any problems.

Here is when I add a car:

void VehicleManager::addCar(string brand, string age, string regNr, string price, string fuel, string size)
{
	if (this->m_count == this->m_capacity)
	{
		Vehicle **temp = new Vehicle*[this->m_capacity + 5];
		for (int i=0; i<m_capacity; i++)
		{
			temp[i] = this->m_ppVehicles[i];
		}
		delete [] this->m_ppVehicles;
		m_capacity += 5;
		this->m_ppVehicles = temp;
	}
	this->m_ppVehicles[this->m_count] = new Car();
	(this->m_ppVehicles[this->m_count])->setVehicle(brand, age, regNr, price, "", fuel, size);
	this->m_count++;
}

and here is when i add a motorcycle

void VehicleManager::addMotorcycle(string brand, string age, string regNr, string price, string storageCape)
{
	if (this->m_count == this->m_capacity)
	{
		Vehicle **temp = new Vehicle*[this->m_capacity + 5];
		for (int i=0; i<m_capacity; i++)
		{
			temp[i] = this->m_ppVehicles[i];
		}
		delete [] this->m_ppVehicles;
		m_capacity += 5;
		this->m_ppVehicles = temp;
	}
	
	this->m_ppVehicles[this->m_count] = new Motorcycle();
	(this->m_ppVehicles[this->m_count])->setVehicle(brand, age, regNr, price, storageCape);
	this->m_count++;
}

I want that the program asks the user what registration number the vehicle has, and based on the registration number the program removes either a motorcycle or a car.

Grateful for answers!

Please provide the shortest compilable example code, sample input, current output, and expected output.

Dave

Okey, I've tried 2 different codes but none of them works.

Code1

void VehicleManager::removeVehicle()
{	
	string rnr;
	cout<<"Enter the registration number of the vehicle to be removed"<<endl;
	getline(cin, rnr);

	for(int i  = 0; i <this->m_count;  i++)
	{ 
		if(m_ppVehicles[i]->getRegNr() == rnr)					
		{
			string brand;
			string regNr;
			string age;
			string price;
			
			m_ppVehicles[i]->setBrand(brand);
			m_ppVehicles[i]->setRegNr(regNr);
			m_ppVehicles[i]->setAge(age);
			m_ppVehicles[i]->setPrice(price);
			delete m_ppVehicles[i]; 
		}
	}

}

Code2

void VehicleManager::removeVehicle()
{
	int pos=-1;
	string rnr;
	cout<<"Enter the registration number of the vehicle to be removed"<<endl;
	getline(cin, rnr);
	

	for( int i=0; i<this->m_count; i++)
	{
		if(m_ppVehicles[i] != 0)
		{
			if(m_ppVehicles[i]->getRegNr()==rnr)
			{
				pos=i;
				break;
			}
		}
		
	}
	if(pos!=-1)
	{	
		delete m_ppVehicles[pos];
		m_ppVehicles[pos]=0;
	}
	else
	{
		cout<<"No such vehicle exists"<<endl;
	}
}

I need to see your class for that and also which data members are used for storing what.

I need to see your class for that and also which data members are used for storing what.

Here's all my classes.

class VehicleManager
{
private:
	Vehicle **m_ppVehicles;
	int m_capacity;
	int m_count;

public:
	VehicleManager();
	VehicleManager(int capacity);
	virtual~VehicleManager();

	VehicleManager(const VehicleManager &h);
	VehicleManager& operator=(const VehicleManager &h);
	
	void addCar(string brand, string age, string regNr, string price, string fuel, string size);
	void addMotorcycle(string brand, string age, string regNr, string price, string storageCape);
	void removeVehicle();
	void showAll();
	void showSpecificType(string type);
	void findRegNr();
};
class Vehicle 
{
protected:
	string m_type;
	string m_age;
	string m_regNr;
	string m_price;
	string m_brand;

public:
	Vehicle();
	Vehicle(string brand, string age, string regNr, string price);
	virtual Vehicle* create(){ return this;} 
	virtual Vehicle* copy(){ return this;}
	virtual~Vehicle();

	string getType();
	string getAge();
	string getRegNr();
	string getPrice();
	string getBrand();

	void setAge(string age);
	void setRegNr(string regNr);
	void setPrice(string price);
	void setBrand(string brand);
	
	virtual void setVehicle(string brand, string age, string regNr, string price, string storageCape = "", string fuel = "", string size = "") = 0;
	virtual void showVehicle() = 0;	
};
class Car : public Vehicle
{
private:
	string m_fuel;
	string m_size;

public:
	Car();
	Car(const Car& car);
	virtual~Car();
	Car* create() const { return new Car(); }
	Car* copy() const { return new Car(*this); }

	string getFuel();
	string getSize();

	void setVehicle(string brand, string age, string regNr, string price, string storageCape = "", string fuel = "", string size = "");
	void showVehicle();
	
};
class Motorcycle : public Vehicle
{
private:
 string m_storageCape;

public:
	Motorcycle();
	virtual~Motorcycle();
	Motorcycle(const Motorcycle& motor);
	Motorcycle* create() const { return new Motorcycle(); }
	Motorcycle* copy() const { return new Motorcycle(*this); }

	string getStorageCape();

	void setVehicle(string brand, string age, string regNr, string price, string storageCape = "", string fuel = "", string size = "");
	void showVehicle();	
};

You have 2 options:
1. Overload remove and make it static:

static void Vehicle::removeVehicle( Car thisCar);
static void Vehicle::removeVehicle( Motocycle thisMotor);

2. Define remove functions in the Car & Motorcycle classes and include it as virtual in Vehicle class

class Vehicle 
{
.
.
	Vehicle();
	Vehicle(string brand, string age, string regNr, string price);
	virtual removeThis();
.
.
};

class Car
{
.
.

	void removeThis(){delete this;};
.
.
};

class Motorcylce
{
.
.

	void removeThis(){delete this;};
.
.
};

I've tried with the second option, and made the "removeThis function" pure virtual but it didn't worked.

Here is what I've did:

class Vehicle 
{
.
.
	virtual void removeThis() = 0;
.
.
};

class Car
{
.
.

	void removeThis(){delete this;};
.
.
};

class Motorcylce
{
.
.

	void removeThis(){delete this;};
.
.
};
void VehicleManager::removeVehicle()
{
	string rnr;	
	cout<<"Enter the registration number of the vehicle to be removed"<<endl;	
	cin >> rnr;

	for(int i  = 0; i <this->m_count;  i++)
	{ 
		if(m_ppVehicles[i]->getRegNr() == rnr)					
		{	
			m_ppVehicles[i]->removeThis();
		}
	}

}


I'm sorry if I didn't get you right!

Strange :-/
What kind of error are you getting compilation or run-time?
If run-time can you put a break point and see if the delete statement in the removeThis() is the cause.

Strange :-/
What kind of error are you getting compilation or run-time?
If run-time can you put a break point and see if the delete statement in the removeThis() is the cause.

It's run-time, the program crashs and the problem is the removeThis function I think. Sorry but I'm not familiar with breakpoints. I've tried but I didn't understood a thing, but I'm pretty sure that the problem is the removeThis function. :S

I did took the pain of testing out the code, and it indeed is CORRECT.
Try the following code of mine:

class Vehicle
{
public:
   virtual void del()=0;
};

class Car : public Vehicle
{
public:
   void del(){delete this;}
};

class Manager
{
public:
   A **ptr;
   void add()
	{
           B *a=new B;
	   ptr[0]=a;
	}
};

int main()
{
   Manager *Obj=new Manager;
   cout<<"\nAdding";
   Obj->add();
   getch();
   cout<<"\nDeleting";
   Obj->ptr[0]->del();
   getch();

return 1;
}

Ran out successfully for me. Since you are performing the operations inside the wrapper class (VehicleManager) you don't really require the instance of that class(*Obj in my case).

I understand what you are doing and I really appreciate that you took the time to help me but I'm still having problems. The program crashs when I want to show all vehicles, or only the cars or only the motorcycles. It's not working for me. The removeThis function deletes but is not working properly.

The program crashs when I want to show all vehicles, or only the cars or only the motorcycles.

Do you know if only had you mentioned this earlier, life would've bee much simpler.
By this I get that the memory is either not being allocated at all of you're class structure is completely out of bounds.

A good rule of the thumb is to first start with small things then make them bigger.

Try changing your void VehicleManager::addCar(...)

Vehicle **temp = new Vehicle*[this->m_capacity + 5];
		for (int i=0; i<m_capacity; i++)
		{
			temp[i] = this->m_ppVehicles[i];
		}
		delete [] this->m_ppVehicles;
		m_capacity += 5;
		this->m_ppVehicles = temp;

to :

Vehicle **temp;
		for (int i=0; i<m_capacity; i++)
		{
			temp[i] = new Vehicle;
			temp[i] = m_ppVehicles[i];
			delete m_ppVehicles[i];
		}
		temp[i] = new Vehicle;
		temp[i]->addYourStuff(arg1,arg2,arg3,arg4...);
		m_ppVehicles = temp;
		m_capacity ++;

Hehe I'm sorry for that, but I discovered it later.

What you wrote on the last post will not work. I have a doubblepointer and in that case the variable "temp" is used without being initialized.

Lol, I never thought I would have so much problems with a remove-function.

I have a doubblepointer

Well I too used a double pointer.

variable "temp" is used without being initialized.

You can't really use a pointer without initializing it or pointing it to an address.

Okey, but this don't work for me

Vehicle **temp;		
	for (int i=0; i<m_capacity; i++)		
	{			
		temp[i] = new Car;	
		temp[i] = m_ppVehicles[i];		
		delete m_ppVehicles[i];
	}	
	
	temp[i] = new Car;	
	temp[i]->setVehicle(brand, age, regNr, price, fuel, size);
	m_ppVehicles = temp;		
	m_capacity ++;

You didn't get me correctly.

Vehicle **temp;		
	for (int i=0; i<m_capacity; i++)		
	{			
[B]		temp[i] = new Car;[/B]	
		temp[i] = m_ppVehicles[i];		
		delete m_ppVehicles[i];
	}	
	
[B]	temp[i] = new Car;	[/B]
	temp[i]->setVehicle(brand, age, regNr, price, fuel, size);
	m_ppVehicles = temp;		
	m_capacity ++;

The highlighted one's should be Vehicle
The red one is where you decided to add a Motorcycle or Car. Overload addThis() for better purpose.

I've tried that what you wrote but I can't make it working. Maybe it's to much to ask but can I send you my project so you can take a look at it? It would maybe be easier for you to and I would appreciate it. If not, I understand and I'm really gratefull for helping me this far.

Ok. I sure can use it, than vaguely taking shots in the void.

Sorry to keep you I had to leave for somewhere. As for your Project, addFunction is:

void VehicleManager::addCar(string brand, string age, string regNr, string price, string fuel, string size)
{

	if (this->m_count == this->m_capacity)
	{
		int i;
		Vehicle **temp;
		for (i=0; i<m_count; i++)
		{
			temp[i] = new Car;
			temp[i] = m_ppVehicles[i];
			delete m_ppVehicles[i];
		}
		temp[i] = new Car;
		temp[i]->setVehicle(brand, age, regNr, price, fuel, size);
		m_ppVehicles = temp;
		m_capacity ++;
		m_count++;
	}
	else
	{
		m_ppVehicles[m_count] = new Car();
		m_ppVehicles[m_count++]->setVehicle(brand, age, regNr, price, "", fuel, size);
	}
}

And for Motorcycle:

void VehicleManager::addMotorcycle(string brand, string age, string regNr, string price, string storageCape)
{
if (this->m_count == this->m_capacity)
	{
		int i;
		Vehicle **temp;
		for (i=0; i<m_count; i++)
		{
			temp[i] = new Motorcycle;
			temp[i] = m_ppVehicles[i];
			delete m_ppVehicles[i];
		}
		temp[i] = new Motorcycle;
		m_ppVehicles[m_count++]->setVehicle(brand, age, regNr, price, storageCape);
		m_ppVehicles = temp;
		m_capacity ++;
		m_count++;
	}
	else
	{
		m_ppVehicles[m_count] = new Motorcycle();
		m_ppVehicles[m_count++]->setVehicle(brand, age, regNr, price, storageCape);
	}
}

I'm still having problems with this the removefunction:

void VehicleManager::removeVehicle()
{
	string rnr;	
	cout<<"Enter the registration number of the vehicle to be removed"<<endl;	
	getline(cin, rnr);

	for(int i  = 0; i <this->m_count;  i++)
	{ 
		if(m_ppVehicles[i]->getRegNr() == rnr)					
		{	
			this->m_ppVehicles[i]->removeThis
		}
	}
}

I've implemented your code, the addfunctions for the car and motorcycle but the program still crashs when i show a vehicle after deleting one. :S

I've implemented your code, the addfunctions for the car and motorcycle but the program still crashs when i show a vehicle after deleting one.

I noticed that strangely, I breakpointed and before going through the function reg.addVehicle() all values of the main list are intact.
During deletion the values are successfully deleted but once the function returns the values are spoiled.

Also upon returning the while loop automatically closes using case 0's return statement, so I made it case 7: instead of 0.

I'm busy with something otherwise would've loved to help your further.
The Add() & Delete() functions are working fine.

I've implemented your code, the addfunctions for the car and motorcycle but the program still crashs when i show a vehicle after deleting one.

I noticed that strangely, I breakpointed and before going through the function reg.addVehicle() all values of the main list are intact.
During deletion the values are successfully deleted but once the function returns the values are spoiled.

Also upon returning the while loop automatically closes using case 0's return statement, so I made it case 7: instead of 0.

I'm busy with something otherwise would've loved to help your further.
The Add() & Delete() functions are working fine.

Okey, I understand and I really appreciate that you helped me this far. =)

this might be a little late in the game but you could always use a vector and have the vector defined as vector<vehicle*> . with that you could add a car or a motorcycle to that vector. then when you want to remove a car or motorcycle you could do this

void VehicleManager::removeVehicle()
{
	string rnr;	
	cout<<"Enter the registration number of the vehicle to be removed"<<endl;	
	cin >> rnr;

	for(int i  = 0; i <this->m_count;  i++)
	{ 
		if(m_ppVehicles[i]->getRegNr() == rnr)					
		{	
			m_ppVehicles.erase(i);
		}
	}
}

this might be a little late in the game but you could always use a vector and have the vector defined as vector<vehicle*> . with that you could add a car or a motorcycle to that vector. then when you want to remove a car or motorcycle you could do this

void VehicleManager::removeVehicle()
{
	string rnr;	
	cout<<"Enter the registration number of the vehicle to be removed"<<endl;	
	cin >> rnr;

	for(int i  = 0; i <this->m_count;  i++)
	{ 
		if(m_ppVehicles[i]->getRegNr() == rnr)					
		{	
			m_ppVehicles.erase(i);
		}
	}
}

Yes, a vector would probably do the work but the thing is that I can't handle vectors. I have never used it. :S

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.