×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

who can help me check my program about abstract type in c++. i used the vc++ to test it.

本文发表在 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
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / who can help me check my program about abstract type in c++. i used the vc++ to test it.
    本文发表在 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
    • if(top=maxsize) throw Overflow(); => top==maxsize AND if(top=0) throw Underflow(); => top==0