【发布时间】:2019-04-13 12:54:02
【问题描述】:
我在 C++ 中创建了一个链表。首先我创建了一个类Node,然后创建了另一个类List。我的程序运行完美。问题是,我不明白为什么 Node 和 List 之间没有继承,以便 List 类可以访问 Node 的公共方法。
我做了一些更改,并从Node 及其工作中派生了List。那么它如何在有继承和没有继承的情况下运行呢?
#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);
}
改编自 Asker 的评论:
任何人都可以解释一下“第二类如何能够使用第一类的
public方法,而没有继承它们?第二类是否不必从第一类派生,所以它可以指向基类(1st)的方法?
【问题讨论】:
-
Node和List之间的继承关系意味着一个是另一个的类型。这将是一个奇怪的选择,因为列表通常包含一系列节点。List不是Node和Node当然不是List -
作为未来的提示,尽量不要过度注释您的代码。注释代码是一件好事。不要评论代码是不好的。过度评论代码也很糟糕。您应该出于文档目的和重要的功能/方法以及非常复杂的代码部分进行评论。例如:
void add(int addObject)真的不需要那个注释,很明显那个方法会添加。Node * newNode = new Node();也很明显。您必须注释您的代码以帮助其他专业人士,而不是教新手。 -
如果(普通)代码需要cmets,不要写cmets而是重写代码。
标签: c++ inheritance linked-list