I created a linked list in c++. First I created a class Node
then another class called List
. My program runs perfectly. The thing is, I don't understand why there is no inheritance between Node
and List
so that List class can access the public methods of Node
.
I made some changes and derived List
from Node
and its working. So how could it run with inheritance and without inheritance?
#include <iostream>
#include <stdlib.h>
using namespace std;
/* The Node class */
class Node
{
private:
int object;
Node * nextNode;
public:
//mutator and Accessor for Node values
void set(int object)
{
this->object = object;
}
int get()
{
return object;
}
//mutator and Accessor for Node Address
void setNext(Node * nextNode)
{
this->nextNode = nextNode;
}
Node * getNext()
{
return nextNode;
}
};
/* The List class */
class List : public Node
{
private:
int size; // List Size ( number of nodes )
Node * headNode; // address of starting node of list
Node * currentNode; // address of Current node of list
Node * lastCurrentNode; // address of previous node of list
public:
/* Constructor */
List()
{
headNode = new Node(); // creating new node and store its address in headNode as its start of list
headNode->setNext(NULL); // the headNode is not connecting to any other node
currentNode = NULL; // as theres only head node so currentNode is empty
lastCurrentNode = NULL; // Previous Node is also empty because there's only headNode
size = 0; // Lisrs Size = 0 because theres no Value/data/object inside the list
}
/* add() class method */
add (int addObject)
{
Node * newNode = new Node(); // creating/adding new node and store its address in newNode Pointer
newNode->set(addObject); // Add Value/data/object in the node just created
if( currentNode != NULL ) // at first time when Current Node pointer is not pointing to any node in the list
{
newNode->setNext(currentNode->getNext()); // get adddress of node where current node will go and store that in the nextNode Pointer which is now called by our new node pointer so the addres that currentNode had now os taken and given to the new node
currentNode->setNext( newNode ); // address of new node we just created is now stored in current node
lastCurrentNode = currentNode; // move Lastcurrent node to the current node position
currentNode = newNode; // move currentNode pointer to the newNode we created;
}
// if current node is not pointing to any node (first time)
else
{
newNode->setNext(NULL); // new node we created will not point to any other next node because there's no one
headNode->setNext(newNode); // head node now is pointing to the new node we created
lastCurrentNode = headNode; // lastCurrent node is now position to headNode so we can go back
currentNode = newNode; // current node is now position to the new node we created
}
size ++; // as there's new new in the list increase its size to +1
}
/* get() class method */
get()
{
if (currentNode != NULL)
return currentNode->get(); // if current node is not null give the value where the current node is
}
/* next() class method */
next()
{
if (currentNode == NULL)
{
return false;
}
lastCurrentNode = currentNode; // move lastCurent node tot he position of current node
currentNode = currentNode->getNext(); // move current node to the next node
if (currentNode == NULL || size == 0)
{
return false;
}
else
{
return true;
}
}
friend void traverse(List list);
friend List addNodes();
};
/* Friend function to traverse linked list */
void traverse(List list) // friend function will get the object of List class
{
Node* savedCurrentNode = list.currentNode; // create new node pointer and assign it the address of current node
list.currentNode = list.headNode; // move current node to the headNode ( starting of the list)
for(int i = 1; list.next(); i++) // while we dnt reached to the end of list or not get false from next function
{
cout << "\n Element " << i << " = " << list.get(); // traverse every node and display its value
}
list.currentNode = savedCurrentNode; // after traversing the whole nodes in the list move the current node to the position where it was befor e
}
/* Friend function to add Nodes into the list */
List addNodes()
{
List list;
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
cout << "\n List size = " << list.size <<'\n';
return list;
}
int main()
{
List list = addNodes();
traverse(list);
}
Adapted from comment by Asker:
Can anybody just explain "How can 2nd class be able to use the
public
methods of 1st class while there's no inheritance b/w them? Does the 2nd class not have to be derived from 1st class so it can be able to point to the methods of base class (1st)?