1、通过类实现一个双向链表。
要求: 正反遍历
动态增加子节点 Add(Node *node);
插入子节点 Insert(intnIndex,Node *node);
删除子节点 Remove(intnIndex,Node*node);
查找子节点 Find(DataType);
Node:(Head、Tail) List:(Next、Prev)
2.设计一个Point类,设计一个Rect类,要求Rect类能够提供方法判断当前Point是否在自身范围内。
Point x、y
Rect left、top、right、bottom
3、实现一个字符串类
MyString str1,str2;
str1=“hello ”; (* char类型转化为str1的class类型)
str2=“world ”;
str1+=str2 ; (内存拷贝)
cout<
屏幕输出:“hello world”
4.实现一个时间类,对其中的“分钟”进行操作。
MyTime tm1(12,30,50),tm2;
tm2.Init(20,0,0);
tm1=tm2;
tm1+=10;//20,10,0
tm1++;//20,11,0
tm2=--tm1;
cout<
5、实现一个万能数组
要求:
MyArrayarr;
arr.Add(35);
arr.Add(90);
可以增加无限个数据
arr.Get(3);
arr.GetLenght();
arr.ShowAll();
#include "lesson_10_homework.h"
#include
using namespace std;
#define size 5
class CSet {
private:
int nArray[size];
int nCount;
public:
CSet();
CSet(int nArray[],int num);
CSet(CSet &st);
~CSet() { cout<<"调用析构函数!"<nArray[i] = nArray[i];
nCount = num;
}
CSet::CSet(CSet &st) {
for(int i=0;inArray[i] = st.nArray[i];
this->nCount = st.nCount;
}
CSet::CSet() {
this->nCount = 0;
}
//-------------------------------------添加数据函数
void CSet::Add(int n) {
nArray[nCount] = n;
nCount++;
}
//-------------------------------------输出函数
void CSet::APrint() {
cout<<"{";
for(int i=0;i
6.实现双向链表的反转
Reverse,尾巴变成头,头变成尾巴,每个节点的指向都发生改变。
函数指针
链表
#include "lesson_10_homework.h"
#include
#include
using namespace std;
class Node
{
private:
int data;
Node* next;
public:
friend class List;
Node(int theValue)
{
data = theValue;
next = 0;
}
};
class List
{
public:
List()
{
first = 0;
end = 0;
}
~List()
{
Node* next;
while (first)
{
next = first->next;
delete first;
first = next;
}
}
//增加item,从屁股后面插入 ^_^
void Add(int value)
{
if (first == 0)
{
first = end = new Node(value);
}
else
{
end->next = new Node(value);
end = end->next;
}
}
//反转List,从前面插入(原有的item,不要想歪了 ^_^)
void Reverse()
{
if (first == 0)
return;
Node* newFirst = first;//临时变量,保存新的first.
end = first;//反转后,first 刚好和end 互换.
Node* nextOfFirst;//辅助变量,其值始终为first的下一个成员,即first->next.
first = first->next;
while (first)
{
nextOfFirst = first->next;
first->next = newFirst;
newFirst = first;
first = nextOfFirst;
}
first = newFirst;
end->next = 0;
}
//显示List的所有成员
void Display()
{
if (first == 0)
{
cout << "The List has no items." << endl;
return;
}
else
{
cout << "The List's items:" << endl;
}
Node* item = first;
while (item)
{
cout << item->data << " ";
item = item->next;
}
cout << endl;
}
private:
Node* first;
Node* end;
};
int main()
{
List theList;
for (int i = 0; i < 10; i++)
theList.Add(i);
//显示其初始值
cout << "List 的原始值为:" <
汇众:
1、静态函数只能被文件内部调用
2、静态函数前的static不是只存储方式,而是指对函数的作用域仅限于本文间
3、在定义函数时,如果没有加关键字“static”
或冠以关键字“extern”表示此函数是外部函数
4、静态数据成员实际上是类域中的全局变量。所以,静态数据成员
的初始化不应该被放在头文件中。且其在外部初始化时,不受权限
访问限制
5、静态数据成员被类的所有对象所共享,包括该类派生类的对象。
即派生类对象与与基类对象共享基类的静态数据成员
6、静态数据成员可以成为成员函数的可选参数,而普通数据成员则不可以
7、静态数据成员的类型可以是所属类型,普通数据成员只能
声明为所属类型的指针或引用
8、静态成员函数不可以调用类的非静态成员。
因为静态成员函数不含this指针
9、静态成员函数不可以同时声明为virtual、const、volatitle函数
10、静态成员函数的地址可用普通函数指针储存,
而普通函数成员函数地址需要用类成员函数指针存储
11、声明一个新的类型(名)