Hey all! I'm new to the forum and a little wet around the programming ears so to speak.
Anyway I have a question regarding Linked Lists.
I know that typically a linked list looks like this:
struct Node
{
dataType varName; //data type and variable name
Node *next; //points to next node in list
};
Now I know I can use standard datatypes such as char, int, string and so on so forth quite easily in a linked list, but what I want to do is use my own data type. In this case I have created a simple class structure similar to:
class Book
{
protected:
string _title;
int _isbn;
public:
Book(string title, int isbn);
~Book();
void setTitle(string title);
string getTitle();
void setIsbn(int isbn);
int getIsbn();
}
class Magazine : Book
{
protected:
string _magazineType;
public:
Magazine(string title, int isbn, string magType);
~Magazine();
void setType(string type);
string getType();
}
Don't take that code too literally, I'm going off of the top of my head. What I'm trying to ask however is in my linked list, how do I get it so that I can create a node that contains a data member of type Book that includes all 'Book' derived classes?
Additionally when adding book objects to the list, what would the syntax be for doing so?
I realise that with a linked list I'm using pointers and and to create a new node is something like:
Node *newNode;
Node *Node_ptr;
newNode = new Node;
Of course if I do this with my custom data class I'm going to get a 'no valid constructor' compile error. What syntax would I need use to pass arguments to the Book constructor? normarily I'd simply do:
Book book1("A Brave New World", 124321);
I don't know how to set a new object using pointers and the like.
I'm a little confused by this and any help is really appreciated!