清使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中包含了类Pets(“宠物”) 和主函数main的定义。程序中位于每个“//ERROR****found ****”之后的一行语句有错误,请加以改 正。改正后程序的输出结果应为: Name:sonny Type:dog Name:John Type:dog Name:Danny Typc:cat Name:John Type:dog 注意:只修改每个“//ERROR ****found ****”下的那一行,不要改动程序中的其

题目

清使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中包含了类Pets(“宠物”) 和主函数main的定义。程序中位于每个“//ERROR****found ****”之后的一行语句有错误,请加以改 正。改正后程序的输出结果应为: Name:sonny Type:dog Name:John Type:dog Name:Danny Typc:cat Name:John Type:dog 注意:只修改每个“//ERROR ****found ****”下的那一行,不要改动程序中的其他内容。 include<iostream> using namespace sm; enum Pets_type{d09,cat,bird,fish}; class Pets{ private: char *name; Pets_type type; public: Pets(const char *name=”sonny”,Pets_type type=dog); Pets&operator=(const Pets&s); ~Pets; void showeonst;}; Pets::Pets(eonst char$naIne,Pets_type type) //构造函数 { This ->name=new char[strlen(name)+1]; strcpy(this一>name,name); //ERROR *********found********* type=type; }{ Pets::~Pets//析构函数,释放name所指向的字符串 { //ERROR *********found********* name=’/0‘; } Pets&Pets::0perator=(const Pets&s){ if(&s==this)//确保不要向自身赋值 return *this; delete[]name; name=new char[strlen(S.name)+1];//ERROR *********found********* strcpy(this一>nmne,name); type=S.type: return *this;} void Pets::showconst cout<<“Name:”<<name<<”Type:”: Pets mypetl,mypet2(’’John”,dog);


相似考题
更多“清使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中包含了类Pets(“宠物”) 和 ”相关问题
  • 第1题:

    请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehiele类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将Vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:

    80

    150

    100

    1

    注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****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;

    }


    正确答案:

    (1)MaxSpeed(maxspeed),Weight(weight){f;

    (2)virtual

    (3)virtua1

    (4)public bicycle,public motorcar


  • 第2题:

    请使用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

  • 第3题:

    请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl。程序中位于每个“//ERROR****found料****之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name:Smith Age:21 ID:99999 CourseNum:12 Record:970 注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。 include<iostream> using namespace std; class Studentlnfo { protected: //ERROR********************found**************** char Name; int Age; int ID: int CourseNum; float Record; public: Studentlnfo(char*name,int Age,int ID,int coumeNum,float record); //ERROR ********************found******************** void~Studentlnfo{} float AverageRecord{ return Record/CourseNum; } void showconst{ cout<<”Name:”<<Name<<”Age:”<<Age<<”ID:”<<ID <<”CourseNum:”<<CourseNum<<”Record:”<<Record<<endl; } }; //ERROR ******************found************** StudentInfo StudentInfo(char*Name,int Age,int ID,int CourseNum,float Record) { Name=name; Age=age; this一>ID=ID: CourseNum=courseNum: Record=record; } int main { Studentlnfo st(’’Smith”,21,99999,12,970); st.show; return 0; }


    正确答案:

    (1)char*Name;
    (2)~Studentlnfo{}
    (3)Studentlnf0::Studentlnfo(char*name,int age,,int ID,int eourseNum,float record)

  • 第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或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: 书名:C++语句程序设计总页数:299 已把“c++语言程序设计”翻到第50页 已把“c++语言程序设计”翻到第51页 已把“c++语言程序设计”翻到第52页 已把“c++语言程序设计”翻到第51页 已把书合上。 当前页:0 注意:只修改每个“//ERROR ***found***”下的那一行,不要改动程序中的其他内容。 include<iostream> using namespace std; class Book{ char*title; int num—pages;//页数 int cur_page;//当前打开页面的页码,0表示书未打开 public: //ERROR***********found*********** Book(const char*theTitle,int pages)num_pages(pages) { title=new char[strlen(theTitle)+1]; strepy(title,theTitle); cout<<endl<<”书名:”<<title <<”总页数:”<<num_pages; } ~Book{delete[]title;} bool isClosedconst{return cur_page==0;} //书合上时返回true,否则返回false bool isOpenconst{return!isClosed;}//书打开时返回true,否则返回false int numOfPagesconst{return num_pages;}//返回书的页数 int currentPageconst{return cur_page;}//返回打开页面的页码 //ERROR***********found*********** void openAtPage(int page_no)const{ //把书翻到指定页 cout<<endl: i“page_no<l Jj page_no>hum_pages){ tOUt<<”无法翻到第”<<cur page<<”页。”; ClOSe; } else{ cur_page=page_n0; tout<<”已把“"<<tide<<"”翻到第"<<cur page<<“页”; } } void openAtPrevPage{openAtPage(cur page—1);{//把书翻到上一页 void openAtNextPage{openAtPage(cur_page+1);}//把书翻到下一页 void close{//把书合上 tout<<endl: if(isClosed) tout<<"书是合上的。"; else{ //ERROR**********found********** num_pages=0; cout<<”已把书合上。”; } cOut<<endl: } }; int main{ Book book(”C++语言程序设计”,299); book.openAtPage(50); book.openAtNextPage; book.openAtNextPage; book.openA.tPrevPage; book.close; tout<<”当前页:”<<book.currentPage<<endl; return 0: }


    正确答案:
    (1)Book(const char*theTifle,int pages):num_pages(pages)
    (2)void openAtPage(int page—no) //把书翻到指定页
    (3)cur_page=0;