没见过定义纯虚函数
含有纯虚析构函数的类是抽象类
必须为这个抽象类中的纯虚析构函数提供函数体
还有一个也是这种问题
#include<iostream.h>
class Node{
friend class list;
private:
int Data;
Node *previous;
Node *next;
};
class list{
private:
Node *Head;
Node *Tail;
public:
list();
~list();
void Build_HT(int Data);
void Build_TH (int Data);
void list::Display_HT();
void list::Display_TH();
void Clear();
};
int main(void)
{
list list1;
int i;
cout<<"Add to the back of the list:"<<endl;
for(i=1;i<=20;i=i+2){
list1.Build_HT(i);
cout<<i<<" ";
}
cout<<endl;
cout<<"Add to the front of the list:"<<endl;
for(i=0;i<=20;i=i+2)
{
list1.Build_TH(i);
cout<<i<<" ";
}
cout<<endl;
list1.Display_HT();
list1.Display_TH();
return 0;
}
list::list()
{
Head=0;
Tail=0;
}
list::~list()
{
Clear();
}
void list::Build_HT(int Data)
{
Node *Buffer;
Buffer=new Node;
Buffer->Data=Data;
if(Head==0)
{
Head=Buffer;
Head->next=0;
Head->previous=0;
Tail=Head;
}
else
{
Tail->next=Buffer;
Buffer->next=0;
Tail=Buffer;
}
}
void list::Build_TH (int Data)
{
Node *NewNode;
NewNode=new Node;
NewNode->Data=Data;
if(Tail==0)
{
Tail=NewNode;
Tail->next=0;
Tail->previous=0;
Head=Tail;
}
else
{
NewNode->previous=0;
NewNode->next=Head;
Head->previous=NewNode;
Head=NewNode;
}
}
//从头到尾显示数据
void list::Display_HT()
{
Node *TEMP;
TEMP=Head;
cout<<"Display the list from Head to Tail :"<<endl;
while(TEMP!=0)
{
cout<<TEMP->Data<<" ";
TEMP=TEMP->next;
}
cout<<endl;
}
//从尾显示数据
void list::Display_TH()
{
Node *Temp;
Temp=Tail;
cout<<"Display from Tail to Head;"<<endl;
while(Temp!=0)
{
cout<<Temp->Data<<" ";
Temp=Temp->previous;
}
cout<<endl;
}
void list::Clear()
{
Node *Temp_head=Head;
if(Temp_head==0) return;
do{
Node *TEMP_NODE=Temp_head;
Temp_head=Temp_head->next;
delete TEMP_NODE;
}
while(Temp_head!=0);
}
这是个双向链表的,问题和上面的一样,都郁闷死了
#include<iostream.h>
class cshape
{
public:
cshape();
virtual int area()=0;
void setshape(int number);
~cshape();
private:
int number;
};
void cshape::setshape(int number)
{
switch(number)
{
case 1:cout<<"It is a triangle"<<endl;break;
case 2:cout<<"It is a crect"<<endl;break;
case 3:cout<<"It is a circle"<<endl;break;
};
}
class ctriangle:public cshape
{
public:
ctriangle();
float triangle(float het,float bot){
return ((float)(0.5*het*bot));
};
~ctriangle();
};
class ccrect:public cshape
{
public:
ccrect();
float crect(float size1,float size2){
return (size1*size2);
};
~ccrect();
};
class ccircle:public cshape
{
public:
ccircle();
double circle(float line){
return (3.14159*line);
};
~ccircle();
};
int main()
{
cshape *pshape;
ctriangle *a;
cout<<a->triangle(3,3)<<endl;
pshape->setshape(1);
ccrect *b;
cout<<b->crect(2,3)<<endl;
ccircle *c;
cout<<c->circle(3.5)<<endl;
cout<<a->triangle(3,3)+b->crect(2,3)+c->circle(3.5)<<endl;
return 0;
}
不好意思,第一个程序是我的一个失误,对不起啊,让大家受累了.
在上面void setshape()不能定义成虚函数,因为它有具体的内容,不用在主程序中再去确定