请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程包含有一个源程序文件proj2.cpp,其中定义了Stack类和ArrayStack类。 Stack是一个用于表示数据结构“栈”的类,栈中的元素是字符型数据。Stack为抽象类,它只定义了栈的用户接口,如下所示: 公有成员函数 功能 push 入栈:在栈顶位置添加一个元素 pop 退栈:取出并返回栈顶元素 ArrayStack是Stack的派生类,它实现了Stack定义的接口。ArrayStack内部使用动态分配的字符数组作为

题目

请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程包含有一个源程序文件proj2.cpp,其中定义了Stack类和ArrayStack类。 Stack是一个用于表示数据结构“栈”的类,栈中的元素是字符型数据。Stack为抽象类,它只定义了栈的用户接口,如下所示: 公有成员函数 功能 push 入栈:在栈顶位置添加一个元素 pop 退栈:取出并返回栈顶元素 ArrayStack是Stack的派生类,它实现了Stack定义的接口。ArrayStack内部使用动态分配的字符数组作为栈元素的存储空间。数据成员maxSize表示的是栈的最大容量,top用于记录栈顶的位置。成员函数push和pop分别实现具体的入栈和退栈操作。 请在程序中的横线处填写适当的代码,然后删除横线,以实现上述功能。此程序的正确输出结果应为: a,b,C C,b,a 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****料found****”。 //proj2.cpp include<iostream> using namespacc std; class Stack{ public: virtual void push(char C)=0; virtual char pop=0; };

class ArrayStack:public Stack{ char*P; int maxSizc; int top; public: ArravStack(int s) { top=0; maxSize=s: //*********found********* P=______; } ~ArrayStack { //*********found********* _______; } void push(char c) } if(top==maxSize){ cerr<<”Overflow! \n”: return; } //*********found********* _______; top++: } char pop { if(top==0){ cerr<<”Underflow!、n”; return‘\0’; } Top--; //*********found********* ______; } }; void f(Stack&sRef) { char ch[]={‘a’,‘b’,‘c’}; cout<<ch[0]<<”,”<<ch[1]<<”,”<<ch[2]<<endl; sRef.push(oh[0]);sRef.push(ch[1]);sRef.push(ch[2]); cout<<sRef.poP<<”,”; cout<<sRef.poP<<”,”; cout<<sRef.poP<<endl; } int main { ArrayStack as(10); f(as): return 0: }


相似考题

2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehiele类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将Vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。include<iostream.h>class vehicle{private:int MaxSpeed;int Weight;public://*************found************vehicle(int maxspeed,int weight):——~vehicle{};int getMaxSpeed{return MaxSpeed;}int getWeight{retum Weight;}};//****************found************class bicycle:——public vehicle{private:int Height;public:bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}int getHeight{retum Height;};};//*******************found**************class motorcar:——public vehicle{private:int SeatNum;public:motorcar(int maxspeed。int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}int getSeatNum{return SeatNum;};};//*****************found***************class motorcycle:——{public:motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}};void main{motorcycle a(80,150,100);cout<<a.getMaxSpeed<<endl;cout<<a.getWeight<<endl;cout<<a.getHeight<<endl;cout<<a.getSeatNum<<endl;}

3.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类0ffice的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 办公室房间号:308 办公室长度:5.6 办公室宽度:4.8 办公室面积:26.88 办公室所属部门:会计科 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“///料料found??料”。 include<isotream> using namespaee std; class Room{ //“房间”类 int room_n0;//房间号 double length;//房间长度(m) double width;//房间宽度(n1) public: Room(int the—room—n0,double the~length,double the—width):loom—no(the—room—no),length(the— length),width(the—width){} int theRoomNoeonst{return iDonl—n0;} //返回房间号 double theLengthconst{return length;}//返回房间长度 double theWidthconst{return width;}//返回房间宽度 //**********found********** double theAreaconst{_______________}//返回房间面积(矩形面积) }; class Office:public Room{//“办公室”类 char*depart;//所属部门 public: Office(int the_room_n0,double the_length,double the_width,eonst char木the_depart) //**********found********** :_______________{ depart=new char[strlen(the depart)+1]; //**********found********** strcpy(_______________); } ~Office{delete[]depart;} const char*theDepartmentconst{return depart;}//返回所属部门 }; int main{ //**********found********** Office_______________; cout<<”办公室房间号:”<<au_office.theRoomNo<<endl <<”办公室长度:”<<an office.theLength<<endl <<”办公室宽度:”<<an 0ffice.theWidth<<endl <<”办公室面积:”<<an office.theArea<<endl <<”办公室所属部门:”<<all—office.theDepartment<<endl; return 0; }

更多“请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程包含有一个源程序文件proj2. ”相关问题
  • 第1题:

    使用VC6打开考生文件夹下的工程test29_1,此工程包含一个源程序文件test29_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:

    A:no parameters

    B:int parameter

    A:no parameters

    B:int parameter

    C:int parameter

    源程序文件test29_1.cpp清单如下;

    include <iostream.h>

    class A

    {

    /***************** found *****************/

    private:

    A(){ cout<<"A:no parameters\n";}

    A(int a) {cout<<"A: int parameter\n";}

    };

    class B:public A

    {

    public:

    B(int a){cout<<"B:int Parameter\n";}

    };

    /***************** found *****************/

    class C:public B,public A

    {

    public:

    /***************** found *****************/

    C(int a) ::B(a) {cout<<"C: int parameter\n"; )

    };

    void main ( )

    {

    B b(1);

    C c(2);

    }


    正确答案:(1)错误:private: 正确:public: (2)错误:class C:public Bpublic A 正确:class C:public B (3)错误:C(int a)::B(a){cout"C:int parameter\n";) 正确:C(int a):B(a){cout"C:int parameter\n";}
    (1)错误:private: 正确:public: (2)错误:class C:public B,public A 正确:class C:public B (3)错误:C(int a)::B(a){cout"C:int parameter\n";) 正确:C(int a):B(a){cout"C:int parameter\n";} 解析:(1)一个类的构造函数和析构函数可以由系统自动生成,也可以由用户提供,但构造函数和析构函数都必须是该类的公有成员函数,否则编译时将出现错误,不能被调用;
    (2)A已经是B的基类,C公有继承B,A也就成为了C的基类,根据程序的运行结果可知,C是要公有继承B;
    (3)派生类的构造函数,初始化基类的参数,调用基类的构造函数时,使用符号“:”,而不是“::”;

  • 第2题:

    使用VC6打开考生文件夹下的工程proj2。此工程包含一个源程序文件main2.cpp,但该程序运行有问题。请改正main函数中的错误。

    源程序文件main2.cpp清单如下:

    //main2.cpp

    include <iostream>

    using namespace std;

    class MyClass

    {

    public:

    MyClass(int m)

    {

    member=m;

    }

    ~MyClass() {}

    int GetMember()

    {

    return member;

    }

    private:

    int member;

    };

    MyClass MakeObject(int m)

    {

    MyClass *pMyClass=new MyClass(m);

    return *pMyClass;

    }

    int main ( )

    {

    int x=7;

    /************found**************/

    MyClass *myObj=MakeObject(x);

    /*************found*************/

    cout<<"My object has member"<<myObj.GetMember()<<end1;

    return 0;

    }


    正确答案:修改后的主函数为: int main() { int x=7; MyClass *myObj=&MakeObject(x); cout"My object has member"myObj->GetMember()end1; return 0; }
    修改后的主函数为: int main() { int x=7; MyClass *myObj=&MakeObject(x); cout"My object has member"myObj->GetMember()end1; return 0; } 解析:本题考核对象指针的应用。程序中出现了2个出错标识符,说明此程序有2处错误。
    第1处错误:“MyClass*myObj=MakeObject(x);”。myObj是对象指针,而函数 MakeObject( )的返回值是类MyClass的对象,所以应改为:“MyClass *myObj=&MakeObject(x);”。
    第2处错误:“cout"MyObject has member"myObj.GetMember()end1;”。对象指针使用方式有两种:“对象指针名->成员名;”和“(*对象指针名)成员名;”。显然上述语句的对象指针的使用方式是错误的。应改为“cout"My object has member"myObj->GetMember()end1;”,或者“cout"MyObject has member"(*myObj).GetMember()end1;”。

  • 第3题:

    请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件 main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为: 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 include<lostream> {tout<<——<<endl;} class CRectangle:public CPolygon{ CRe&angle(int w,int h):width(w),height(h){} int area(void){return(width *height);} class CTriangle:public CPolygon{ int length;//三角形一边长 int height;//该边上的高 public: CTriangle(int l,int h):length(1),height(h){} //*********found********* int area(void){return(——)/2;} }; int main{ CRectangle rect(4,5); CTriangle trgl(4,5); //*********found********* ______ *ppolyl,* ppoly2; ppolyl=&rect; ppoly2=&trgl; ppolyl->printarea; ppoly2->printarea; retun 0;


    正确答案:

    (1)virtual int area(void)=0;
    (2)area
    (3)length*height
    (4)CPolygon

  • 第4题:

    使用VC6打开考生文件夹下的工程RevProj3。此工程包含一个源程序文件 RevMain3.cpp。阅读文件中的程序代码,找出程序中的错误,并改正。

    源程序文件RevMain3.cpp清单如下:

    //RevMain3.cpp

    include<iostream>

    using namespace std;

    class MyClass{

    public:

    /* * * * * * * * *found * * * * * * + * * */

    void MyClass(int a){ value=a;}

    int Max(int x,int y)

    {

    if(x>y)

    return x>y?x:y;

    else

    return y>value?y:value;

    }

    /* * * * * * * * *found * * * * * * * * * */

    ~MyClass(int a)

    {

    value=a;

    }

    private:

    int value;

    };

    int main()

    {

    MyClass my(10);

    cout<<my.Max(20,30)<<end1;

    return 0;

    }


    正确答案:正确的类MyClass定义为: class MyClass { public: MyClas(int a) { value=a;} int Max(int xint y) { if (x>y) return x>y?x:y; else return y>a?y:a; } ~MyClass() {} private: value; };
    正确的类MyClass定义为: class MyClass { public: MyClas(int a) { value=a;} int Max(int x,int y) { if (x>y) return x>y?x:y; else return y>a?y:a; } ~MyClass() {} private: value; }; 解析:本题考核类的定义。程序中出现了2个出错标识符,说明此程序有2处错误。第1处错误:类的构造函数没有返回类型,而程序中将类的构造函数设为void型,显然是错误的。
    第2处错误:C++中类的析构函数中不能带有行参,所以程序中析构函数的定义是错误的。

  • 第5题:

    使用VC6打开考生文件夹下的工程test21_1,此工程包含一个源程序文件test21_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:

    The grade is 3

    源程序文件test21_1.cpp清单如下:

    include<iostream.h>

    class student

    {

    private:

    int grade;

    public:

    /**************** found*******************/

    student(int thegra):(thegra){}

    ~student(){}

    int get_grade(){return grade;}

    };

    void main()

    {

    int thegra=3;

    /**************** found*******************/

    student point=new student(thegra);

    /**************** found*******************/

    cout<<"The grade is"<<point.get_grade()<<endl;

    delete point;

    }


    正确答案:(1)错误:student(int thegra):(thegra){} 正确:student(int thegra):grade(thegra){} (2)错误:student point=new student(thegra) 正确:student*point=new student(thegra); (3)错误:cout"The grade is"point.get_grade()endl; 正确:cout"The grade is"point->get_grade()endl;
    (1)错误:student(int thegra):(thegra){} 正确:student(int thegra):grade(thegra){} (2)错误:student point=new student(thegra) 正确:student*point=new student(thegra); (3)错误:cout"The grade is"point.get_grade()endl; 正确:cout"The grade is"point->get_grade()endl; 解析:(1)主要考查考生对于构造函数使用参数列表初始化数据成员的掌握,可以使用参数列表的形式,也可以在函数内部实现,不过参数赋值的双方都必须出现,本题的错误就在于并没有把参数赋值给数据成员;
    (2)主要考查考生对于使用指针申请动态空间的方法的理解,new运算符申请的空间返回值为指针类型,指针类型的定义是在变量名前加上“*”;
    (3)主要考查考生对于对象指针调用成员函数方法的掌握,必须使用符号“->”,而对象本身使用符号“.”。