Linked List with Different Class Objects (Not using STL)

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...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)
};



And the functions for the Linked List will be:

AddNewNodeAtEnd
ShowNode / ShowNextNode
DeleteNode etc


Any ideas? Thanks
Last edited on
anyone?
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?
I think what I need to know is, how can I get the BaseClassPointer in the node, to point to different objects + their variable / functions,




basically the program I am making (user orientated), is like this:

[Menu-Driven]

1 - Add new Vehicle
2- Display next Vehicle
3- Delete Vehicle




(And if user wants to add a new vehicle)


Select Type of Vehicle

1 - Car
2 - MotorBike




And then the user fills in the details for whichever vehicle, and this is saved (as a node??) as part of the Linked List,



So we dont know before compiling / running the program, which object type will be created,


[Sorry if im not explaining this too clearly, c++ is new for me :-P]

Thanks again
anyone? it involves run-time polymorphism but im not too sure how to implement this
Topic archived. No new replies allowed.