I am trying to add a object to my linked list, the problem is I am not sure how also the object its self is a base class with sub classes. Vehicle and Car,Van respectively.
The idea of the program is a vehicle rental system where the user can add a vehicle to the system and set certain variables about the vehicle such as: reg number engine size etc but also if the vehicle is a van or a car you the set further variables depending on the whether its a car or van.
For example if I want to add a Ford Van, I would enter the details such as engine size,model etc then i would specify that its a van allowing me to add further details contained within the van subclass.
If any of you can help me with being able to add an object to a linked list such that it meets the criteria I would be very thankful.
My code to insert a new node.
void linkedList::InsertNode(int regNo,double engine,string model,vehicle_type make)
{
Vehicle<int> vehicle(int,double,string,vehicle_type);
//create new node
nodePtr n=new node;
//make next point to null
n->next=NULL;
n->data=vehicle(regNo,engine,model,make);
//if we have a list set current pointer
if(head!=NULL)
{
current=head;
//checks if end
while(current->next!=NULL)
{
current=current->next;
}
//connect last node to new node
current->next=n;
}
else
{
//new node is front of list if empty
head=n;
}
}
Linkedlist.h
#pragma once
#include <string>
using namespace std;
class linkedList
{
template<class regNo>;
friend class Vehicle<int>;
typedef enum{ Car,Van} vehicle_type;
private:
typedef struct node
{
int data;
node* next;
}*nodePtr;
nodePtr head;
nodePtr current;
nodePtr temp;
public:
linkedList();
void InsertNode(int regNo ,double engine,string model,vehicle_type make);
void SearchNode(int);
void PrintList();
void DeleteNode(int delData);
~linkedList();
};
vehicle.h
#pragma once
template<class regNo=int>
class Vehicle
{
public:
typedef enum{ Car,Van} vehicle_type;
protected:
vehicle_type make;
string model;
int regNo;
double engineSize;
bool rented;
public:
Vehicle(vehicle_type make):model(""),regNo(reg),engineSize(engine),make(make),rented(false){};
char getMakeModel();
int getRegNo();
int getEngineSize();
bool getRented();
bool setRented(bool rented);
~Vehicle();
void listVehicle();
void addVehicle();
void removeVehicle(int delData);
void bookVehicle();
void displayDetails(int regNo);
void listCarsNotRented();
void listFiveDoor();
void listFordVansRented();
};
For clarification the linked listed will contain a list of many objects that holds information about a vehicle, what I need to do is add a vehicle(object) to the list.
The error occurs when trying to set the data within the node to the object I want.
First of all, I don't know how this will work:
Vehicle<int> vehicle(int,double,string,vehicle_type);
I think you meant to type this:
Vehicle<int> vehicle(regNo,engine,model,vehicle_type);
If you don't have an assignment or something for linked lists that you must implement linked lists yourself, I would recommend you to use Boost Linked lists instead of implementing your own: Link
For the actual problem itself:
If you want to find out what type of object it is you're adding to the linked list, simply override methods for them to support sub-classes. So instead of adding to linked list by using primitives (int, double etc.) take a Vehicle object or Van object in order to add to the list. I don't know the code but it is something like this:
void linkedList::InsertNode(Van *pVan)
{
Van newVan(*pVan); // You need a copy constructor for this
// Set Van specific attributes like this:
newVan->setEngineSize (123);
//create new node
nodePtr n = new node;
//make next point to null
n->next=NULL;
n->data= newVan; // You need a copy constructor for this
//if we have a list set current pointer
if(head!=NULL)
{
current=head;
//checks if end
while(current->next!=NULL)
{
current=current->next;
}
//connect last node to new node
current->next=n;
} else
{
//new node is front of list if empty
head=n;
}
}
void linkedList::InsertNode(Car *pCar)
{
Car newCar(*pCar); // You need a copy constructor for this
// Set Car specific attributes like this:
newCar->setEngineSize (123);
//create new node
nodePtr n = new node;
//make next point to null
n->next=NULL;
n->data= newCar; // You need a copy constructor for this
//if we have a list set current pointer
if(head!=NULL)
{
current=head;
//checks if end
while(current->next!=NULL)
{
current=current->next;
}
//connect last node to new node
current->next=n;
} else
{
//new node is front of list if empty
head=n;
}
}
So override the methods to support different classes. When you compile the code, it will automatically call the right method for you. For example if you have a Van and call insertNode function that you pass a reference of your new Van object, it won't call void linkedList::InsertNode(Vehicle *pVehicle)
but will call void linkedList::InsertNode(Van *pVan)
My recommendations: - Make a "tail" pointer that points to the end of the list, so that you won't go through a while loop each time you add an object. - While implementing any additional containers, use "template"s for the data types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.