Hi!

I am making a project about doublepointers and inheritance. Have some small issues that should not be difficult to resolve, but I just cant get a grip of it.

The task is to add two types of vehicles that have some of the same characteristics but not all. I'm using a dynamic allocated array that is declared as "Vehicle ** vehicles" to store all vehicles. This is a pointer that points to another pointer and this together with inheritance immediately becomes complicated (for me).

- My questions now is how I'm going to just show all the cars and not all vehicles.?

- It should also be possible that, from a vehicle, find out if it contains a certain character combinations in number and * is used to "fill" the positions of the registration number as we do not know the contents of (for example to search for all vehicles with registration begins at A and the last digits is 19 out of character sequence A *** 19.

I have 5 cpp-files and 4 h-files but I only show some of it, and if that's not enough for you to help me, then I can post more of my code and maybe all if that's necessary.


This is when I add a Car.

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

This is when i show all the Vehicles

void VehicleManager::showAll()
{
    for(int i=0; i<this->count; i++)
    {
        cout <<this->vehicles[i]->getBrand() <<'\t' <<this->vehicles[i]->getRegNr()<< endl;
    }
}

you are makin the object of derived class (car) using base class(vehicle) pointer.. thats correct..

but there should be a pure virtual funtion showType or show in base class which should be overridden in every derived class.

now when u call the show function it will autometically call ur derived class function and will display only what u require.

you are makin the object of derived class (car) using base class(vehicle) pointer.. thats correct..

but there should be a pure virtual funtion showType or show in base class which should be overridden in every derived class.

now when u call the show function it will autometically call ur derived class function and will display only what u require.

Okey, I understand what you mean, but if I do that I'll get an abstract class, and you can't instantiate an abstract class (Vehicle.h).

do you need the instantiation of base class in your case.

use the pointer of base class to instantiate derived one..

and i think vehicle should be an abstract class...

//it should be like
vehicle *vech = new Car();

and if you still want to instantiate vehicle dont make the functions pure virtual insteed keep it virtual only
this way ur vehicle class wont be abstract..

do you need the instantiation of base class in your case.

use the pointer of base class to instantiate derived one..

and i think vehicle should be an abstract class...

//it should be like
vehicle *vech = new Car();

and if you still want to instantiate vehicle dont make the functions pure virtual insteed keep it virtual only
this way ur vehicle class wont be abstract..

I think i need the instanttiation of base class and if I make the functions only virtual I'll get LINK-errors.

try it at least i think you wont get link errors

give ur virtual function a definition

if it dosent find its derived class definition then it will autometically call ur base definition..

try it at least i think you wont get link errors

give ur virtual function a definition

if it dosent find its derived class definition then it will autometically call ur base definition..

Okey, I've mad the functions virtual, but I dont know how to move on. What should i write for code to execute? How and from where to call it?

Maybe I should show my whole program?

Okey, I've mad the functions virtual, but I dont know how to move on. What should i write for code to execute? How and from where to call it?

Maybe I should show my whole program?

sorry for the delay i was offline..

first u should know how virtual function works...

class vehicle
{

virtual void showType(); // only virtual in this case u can instanciate vehicle
{
           // you can have definition here
}
//or
virtual void showType() = 0;// this is pure virtual u cant instanciate vehicle now , and every derived class form vehicle has to override this function. there cannot be any definition here now. its the contract that derived class will use me as they want 
};

class Car : public vehicle
{
       void showType()
       {
            // u  can have definition here.. if showType() is virtual in base class this function will override base class definition. 
       }
           //if u do not write this function here and showType is only virtual in base class then base class definition will be called.
};

if you still have prob then show me ur code ill help

I wanna thank kunal kislay for helping me to show only one type of vehicles and many other things.

If nobody else can help me with the "registration number" I mark it as solved.

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.