Hi, Im having trouble with making a Linked List, using different Class Objects (Inherited objects),
here are the classes which I have simplified (I know the functions should be defined outside the class :-P )
I understand the basics of the linked list, but implementing one which uses different objects I cant seem to do, and theres not much help from Google either on this...
class Vehicle
{
protected:
int RegID;
string Name;
};
class Car : public Vehicle
{
protected:
int NumWheels;
float EngineSize;
public:
void getNumWheels() { cout<<NumWheels<<" ";}
void getEngineSize() { cout<<EngineSize<<" ";}
};
class MotorBike : public Vehicle
{
protected:
int NumWheels;
float EngineSize;
string TypeOfBike;
public:
void getNumWheels () {cout<<NumWheels<<" ";}
void getEngineSize () {cout<<EngineSize<<" ";}
void getTypeOfBike () {cout<<TypeOfBike<<" ";}
};
So the Linked List will consist of Objects of either Car or MotorBike, (depending on what the user selects)
Here is my Node class so far, I think I have to have a Pointer to the base class (Vehicle) here, and use Virtual Functions I think,
1 2 3 4 5 6
class Node //Create a Node for each part of the Linked list
{
private:
Vehicle *ptr1; //Pointer of base class
Node *next; //Pointer for next node (in list)
};
Sorry...what's the exact question? This funky word 'polymorphism' comes to mind in this case. If your list stores types of the base class, you can add any instance of a type that inherits from it (as long as that type can be instantiated, of course). I have no idea if that answers your question though. Do you want to see code?