类间函数调用的问题
下面是我建的一个工程,其中tablehead类跟list类几乎一样,就只是将模板类中的Elem换成了Column,另外加了一个find函数,能不能不用再把list中的函数再原样写到tablehead中,而采用类间函数调用的形式?我不会这编这种类型的东西,哪位高手可以示范一下List类:
#include<iostream>
using namespace std;
template <class Elem>
class Link
{ public:
Elem element;
Link*next;
Link(const Elem& elemval,Link*nextval=NULL)
{ element=elemval;
next=nextval;
}
Link (Link*nextval=NULL)
{ next=nextval;}
};
template<class Elem>
class List:public List<Elem>
{ private:
Link<Elem>*head;
Link<Elem>*fence;
Link<Elem>*tail;
int leftcnt;
int rightcnt;
void init()
{ fence=tail=head=new Link<Elem>;
leftcnt=rightcnt=0;
}
void removall()
{ while(head!=NULL)
{ fence=head;
head=head->next;
delete fence;
}
}
public:
List() { init();}
~List() { removall();}
void clear() { removall();init();}
bool insert (const Elem&);
bool append (const Elem&);
bool remove (Elem&);
void setStart();
void setEnd();
void prev();
void next();
int leftLength()const;
int rightLength()const;
bool setPos(int pos);
bool getValue(Elem& it)const;
void print()const;
};
template<class Elem>
bool List<Elem>::insert(const Elem& item) //在表内增加结点
{ fence->next=new Link<Elem>(item,fence->next);
if(tail==fence) tail=fence->next;
rightcnt++;
return true;
}
template<class Elem>
bool List<Elem>::append(const Elem& item) //在表末尾增加结点
{ tail=tail->next=new Link<Elem>(item,NULL);
rightcnt++;
return true;
}
template<class Elem>
bool List<Elem>::remove(Elem& it) //删除一个结点
{ if(fence->next==NULL) return false; //表为空
it=fence->next->element;
Link<Elem>*ltemp=fence->next;
fence->next=ltemp->next;
if(tail==ltemp) tail=fence; //如果要删除尾结点
delete ltemp;
rightcnt--;
return true;
}
template<class Elem>
void List<Elem>::setStart() //指针指向链表头
{ fence=head; rightcnt+=leftcnt; leftcnt=0;}
template<class Elem>
void List<Elem>::setEnd() //指针指向链表尾
{ fence=tail; leftcnt+=rightcnt; rightcnt=0;}
template<class Elem>
void List<Elem>::prev() //结点向前移一位
{ Link<Elem>*temp=head;
if(fence==head) return; //如果fence指向head,则没前驱,不返回任何值
while (temp->next!=fence) temp=temp->next; //结点一直往后移,直到temp->next=fence
fence=temp;
leftcnt--;
rightcnt++;
}
template<class Elem>
void List<Elem>::next() //结点向后移一位
{ if(fence!=tail)
{ fence=fence->next; rightcnt--; leftcnt++;}
}
template<class Elem> //fence 左边的长度
int List<Elem>::leftLength()const
{ return leftcnt;}
template<class Elem> //fence 右边的长度
int List<Elem>::rightLength()const
{ return rightcnt;}
template<class Elem>
bool List<Elem>::setPos(int pos) //当前的位置
{ if((pos<0)||(pos>rightcnt+leftcnt))return false;
fence=head;
for(int i=0; i<pos; i++) fence=fence->next;
return true;
}
template<class Elem>
bool List<Elem>::getValue(Elem&it) const //当前结点的值
{ if(rightLength()==0) return false;
it=fence->next->element;
return true;
}
template<class Elem>
void List<Elem>::print()const //打印结点
{ Link<Elem>*temp=head;
cout<<"<";
while (temp!=fence)
{ cout<<temp->next->element>>"";
temp=temp->next;
}
cout<<"|";
while (temp->next!=NULL)
{ cout<<temp->next->element<<"";
temp=temp->next;
}
cout<<">\n";
}
Column类:
#include<iostream>
#include<string>
using namespace std;
class Column
{
private:
string Listname; //表名
string Listtype; //表类型
int Listlength; //表长度
public:
Column (string lname,string ltype,int llength); //构造函数
string getListname(){return Listname;}
string getListtype(){return Listtype;}
int getListlength(){return Listlength;}
void print();
};
Column::Column(string lname,string ltype,int llength) //构造函数
{
Listname=lname;
Listtype=ltype;
Listlength=llength;
}
void Column::print()
{
cout<<"Name:"<<Listname<<endl;
cout<<"Type:"<<Listtype<<endl;
cout<<"Length:"<<Listlength<<endl;
}
int main ()
{ string lname,ltype;
int llength;
cout<<"Please input the listname:";
cin>>lname;
cout<<"Please input the listtype:";
cin>>ltype;
cout<<"Please input the listlength:";
cin>>llength;
Column c1(lname,ltype,llength);
c1.print(); //调用print函数
system("pause");
return 0;
}
Tablehead类:
#include"column.h"
#include"list.h"
#include<iostream>
using namespace std;
class tablehead
{ private:
int colnum;
List<column>*tableheadlist;
public:
tablehead();
~tablehead();
void clear();
bool insert(const column&);
bool append(const column&);
bool remove(column&);
void setStart();
void setEnd();
void prev();
void next();
int leftLength()const;
int rightLength()const;
bool setPos(int pos);
bool getValue(column&it)const
bool find();
void print()const;
};