本文发表在 rolia.net 枫下论坛the program is:
the base class is defined as follows:
class baseclass
{
public:
class Overflow{};
class Underflow{};
class Bad_size{};
baseclass();
virtual ~baseclass();
virtual void push(char c)=0;
virtual char pop()=0;
};
baseclass::baseclass()
{
}
baseclass::~baseclass()
{
}
the derived class is defined as follows:
class derivedclass:public baseclass
{
char *p;
int top;
int maxsize;
public:
derivedclass(int s);
virtual ~derivedclass();
void push(char c);
char pop();
};
derivedclass::derivedclass(int s)
{
top=0;
if (s<0||s>100) throw Bad_size();
maxsize=s;
p=new char[s];
}
derivedclass::~derivedclass()
{
}
void derivedclass::push(char c)
{
if(top=maxsize) throw Overflow();
p[top]=c;
top++;
}
char derivedclass::pop()
{
if(top=0) throw Underflow();
top--;
return p[top];
}
the function to be used :
void f(baseclass& s_ref)
{
s_ref.push('c');
if(s_ref.pop()!='c')
cout<<"bad pop\n"<<endl;
cout<<s_ref.pop()<<endl;
}
int main(int argc, char* argv[])
{
derivedclass as(300);
list_stack ls;
f(ls);
return 0;
}
note: i have passed the link and getten a .exe file, but while running, the program produce an error and ask to terminate the program.更多精彩文章及讨论,请光临枫下论坛 rolia.net
the base class is defined as follows:
class baseclass
{
public:
class Overflow{};
class Underflow{};
class Bad_size{};
baseclass();
virtual ~baseclass();
virtual void push(char c)=0;
virtual char pop()=0;
};
baseclass::baseclass()
{
}
baseclass::~baseclass()
{
}
the derived class is defined as follows:
class derivedclass:public baseclass
{
char *p;
int top;
int maxsize;
public:
derivedclass(int s);
virtual ~derivedclass();
void push(char c);
char pop();
};
derivedclass::derivedclass(int s)
{
top=0;
if (s<0||s>100) throw Bad_size();
maxsize=s;
p=new char[s];
}
derivedclass::~derivedclass()
{
}
void derivedclass::push(char c)
{
if(top=maxsize) throw Overflow();
p[top]=c;
top++;
}
char derivedclass::pop()
{
if(top=0) throw Underflow();
top--;
return p[top];
}
the function to be used :
void f(baseclass& s_ref)
{
s_ref.push('c');
if(s_ref.pop()!='c')
cout<<"bad pop\n"<<endl;
cout<<s_ref.pop()<<endl;
}
int main(int argc, char* argv[])
{
derivedclass as(300);
list_stack ls;
f(ls);
return 0;
}
note: i have passed the link and getten a .exe file, but while running, the program produce an error and ask to terminate the program.更多精彩文章及讨论,请光临枫下论坛 rolia.net