1.C++常见的连接错误:头文件中声明了,源文件中没有定义。
2.类对象做成员变量的构造和析构顺序:(1)类内的对象按照定义的顺序构造,之后是外层类对象的构造;(2)析构时,外层类对象先析构,内层类对象后定义的先析构。
// Test516.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "CManager.h"
#include <iostream>
using namespace std;
int main()
{
CManager manager;
manager.Menu();
return 0;
}
#include "stdafx.h"
#include "CManager.h"
#include <iostream>
using namespace std;
CManager::CManager()
{
}
CManager::~CManager()
{
RemoveAll();
}
void CManager::Input()
{
cout << "请输入你要插入的数据:";
int data = 0;
cin >> data;
cout << "请输入要插入的链表(1或2)" << endl;
int i = 0;
cin >> i;
if (i == 1)
{
m_list1.Add(data);
cout << "链表1总共有" << m_list1.GetCount() << "条数据" << endl;
}
if (i == 2)
{
m_list2.Add(data);
cout << "链表2总共有" << m_list2.GetCount() << "条数据" << endl;
}
}
void CManager::Output()
{
cout << "请选择要打印的链表(1或2)" << endl;
int i = 0;
cin >> i;
if (i == 1)
{
m_list1.Output();
}
if (i == 2)
{
m_list2.Output();
}
}
int CManager::Menu()
{
cout << "请选择:" << endl;
cout << "1.插入数据" << endl;
cout << "2.打印数据" << endl;
cout << "3.输出节点数目" << endl;
cout << "4.退出" << endl;
int i = 0;
cin >> i;
switch (i)
{
case 1:
Input();
Menu();
break;
case 2:
Output();
Menu();
break;
case 3:
DisplayCount();
Menu();
break;
case 4:
system("pause");
break;
default:
break;
}
return i;
}
void CManager::RemoveAll()
{
m_list1.RemoveAll();
m_list2.RemoveAll();
}
void CManager::DisplayCount()
{
cout << "获取链表数目,请输入(1或2)" << endl;
int i = 0;
cin >> i;
if (i == 1)
{
cout << "链表1节点数目为: " << m_list1.GetCount() << endl;
}
if (i == 2)
{
cout << "链表2节点数目为: " << m_list2.GetCount() << endl;
}
}
#include "stdafx.h"
#include "CMyList.h"
#include <iostream>
using namespace std;
CMyList::CMyList()
{
m_pHead = NULL;
count = 0;
}
CMyList::~CMyList()
{
RemoveAll();
}
void CMyList::Output()
{
SNode* p = m_pHead;
while (p)
{
cout << p->data << endl;
p = p->pNext;
}
}
void CMyList::Add(DATA data)
{
SNode* node = new SNode;
node->data = data;
node->pNext = this->m_pHead;
m_pHead = node;
}
int CMyList::GetCount()
{
count = 0;
SNode* p = m_pHead;
while (p)
{
++count;
p = p->pNext;
}
return count;
}
void CMyList::RemoveAll()
{
SNode* p = m_pHead, *p1;
while (p)
{
p1 = p;
p = p->pNext;
delete p1;
}
m_pHead = NULL;
}