i have a class dog which i want it to inherit one of the three kinds of dogs either rescue or boom or drug
tried
class dog() public boom , public drug public rescue

but it creates all three kinds
how can i have a dog class which will be able to inherit the three of them but create only on choice ?
thank you in advance .

05-25-2004

ventolin

You are inheriting the methods and variables from the Base class, and thus inherit all methods declared in those classes into the class dog.

Let me guess you have this:

Class Rescue

Class Boom

Class Drug

Class Dog : public Rescue

A better way to do this is create a Base class called Dog. Let the Boom, Drug, and Rescue classes be derived from this so they inherit generic function/variables from Dog. So you have something like this:

Code:

class Dog {
//some methods/variables
};

Code:

class Rescue : public Dog
{
};

class Boom : public Dog
{
};

class Drug : public Dog
{
};

Then implement the necessary function in the derived classes.

05-25-2004

kernel

did not understand

hi
thanks for the quick reply
i didn't understand the diffrence

class dog () : public boom , public rescue , public drug
{
//
}

is what i have now , i want to be able to inherit one of the other
classes according to my prefences , the minute a dog is created he constructs all three other classes
how can i avoid this ?

it should hold the dogs in the pound either drug...
but every dog holds one kind

05-25-2004

jlou

It sounds like ventolin's code was what you want.

A Rescue is a dog (I'm assuming it's a rescue dog - consider changing the name to RescueDog to reflect that). A Drug is a dog, but a Drug is not a Rescue. Is that right? If so, then alter your code to match ventolin's:

On the other hand, if you want your dog to contain a Rescue, Boom, and Drug, then you'd want a different setup. Try running the program above and understanding why it works if it's what you want.

05-26-2004

kernel

thanks

now i understand i tried it the other way around ...
class dog : public ....

anyways i've ran into a diffrent problem now ;),
i have in the main the dogarr**
now the max it can hold it 10 , now after deleteing let's say number 3 when i have 8 in the arr
how can i push the remaining 5 back ... ?

05-26-2004

kernel

solve it but ...

ok i've added a string type and a gettype{return type}
so i'll know what to copy
but is there a more elegant way of solving it without adding a variable ?
i mean can i know what type of dog is it without the added variable ?

thanks in advance

05-26-2004

elad

>>now the max it can hold it 10 , now after deleteing let's say number 3 when i have 8 in the arr
how can i push the remaining 5 back ... ?

To shift elements in array to left by one you can do something like this.
for( i = elementToErase; i < #ofElementInArray; ++i)
array[i] = array[i+1];

05-26-2004

kernel

i'm not that much of a novice ;)

That is quite elementary Jbro , but my problem with psushing back is knowing whcih class to associate the
dogarr[i] = new TYPE for the [i] = [i+1]
that's why I added the string type , nut is there a more elgant way to do it like sizeof() for int and double ...

05-26-2004

elad

download jlou's code. Change the code after the for loop in main to look like this:

Code:

delete dogarray[i];

dogarray[1] = dogarray[2];

for(i = 0; i < 2; ++i)
dogarray[i]->print();

delete dogarray[0];
delete dogarray[1];
delete [] dogarray;

return 0;

The point I'm trying to make is that you don't need to know what whether dogarray[i] points to a drug or a rescue or a boom to get the code to work. You can delete them, move them, etc. without knowing. That's part of the power (and mystique) of polymorphism/pointers/OOP.

Now, if you have some reason to know whether dogarray[i] is pointing to a rescue or a boom or a drug I believe you can use RTTI (Runtime Type Identification).

The reference I have on RTTI indicates that you can mimic RTTI using an enumerated type in each of the classes and then test that type at runtime--which sounds similar to your use of a string variable and then testing that string at runtime. My reference indicates that having to use RTTI may be an indication of poor design and suggests looking into use of virtual functions, templates, or other types of inheritance to see if there is some other mechanism to do what you want. Furthermore my reference indicates that you need to cast down from the base class pointer to the derived class using the concept of dynamic_cast.

If this all sounds like it may be applicable to your case, then I suggest looking in your references for RTTI and dynamic_cast.