问答题请使用“答题”菜单或从VC6中打开考生文件夹proj1下的工程proj1。此工程包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个//ERRO************found************下的语句行有错,请加以改正。改正后程序的输出结果是: Name: sonny Type: dog Name: John Type: dog Name: Danny Type: cat Name: John Type: dog 注意:只能修改每个//ERROR************found************下的那一行,不要改动程序中的其他内容。 //源程序 #include <iostream> using namespace std; enum Pets_typedog,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 show() const; ; Pets::Pets(const char*name,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::operator=(const Pets &s) if(&s==this) return*this; //确保不要向自身赋值 delete[]name; name=new char[strlen(s.name)+1]; //ERROR************found************ strcpy(s.name,this->name); type=S.type; return*this: void Pets::show() const couL<<"Name:"<<name<<"Type:"; switch(type) case dog: cout<<"dog";break; case cat: cout<<"cat";break; case bird: cout<<"bird";break; case fish: cout<<"fish";break; cout<<endl; int main() Pets mypet1,mypet2("John",dog); Pets youpet("Danny",cat); mypet1.show(); mypet2.show(); youpet.show(); youpet=mypet2; youpet.show(); return 0:
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误使程序的输出结果为:Constructor called.The value is10Copy constructor called.The value is10Destructor called.Destructor called.注意:只修改注释“//EROR****found****”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostream>using namespace std;class MyClass{public://ERROR**********found**********MyClass(int i){value=i;cout<<"Constructor called."<<endl;}//ERROR**********found**********MyClass(const MyClass p){value=p.value;cout<<"Copy constructor called."<<endl:}void Print(){cout<<"The value is"<<value<<endl;}//ERROR**********found**********void~MyClass(){cout<<"Destructor called."<<endl;}private:int value;};int main(){MyClass obj1;obj1.Print();MyClass obj2(obj1);obj2.Print();return0;}
问答题cout<<"Area of this rectangle is: "<<recta.getarea()<<endl;
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char* des,char* str)的功能是去掉字符串str中相同的字母,并将处理后的结果存到des所指的字符串中。例如:
输入:Ths is great!
输出:This grea!
注意:不能修改程序的其他部分,只能补充fun()函数。
#include <iostream.h>
#define MAXLEN 1024
void convert(char* des,char* str)
{
}
void main()
{
char sour[MAXLEN];
char dest[MAXLEN];
cout
"Please input a string: "
endl;
cin.getline(sour,MAXLEN);
convert(dest,sour);
cout
dest
endl;
return;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。此工程中定义了一个人员类Person,然后派生出学生类Student和教授类Professor。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
My name is Zhang.
my name is Wang and my G.P.A.is 3.88.
My name is Li, I have 8 publications..
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//********found********”。
#include <iostream>
using namespace std;
class Person{
public:
//********** found**********
______{name =NULL;}
Person (char* s)
{
name = new char[ strlen (s) +i];
strcpy(name, s);
}
~Person()
{
if(name!=NULL) delete [] name;
}
//********** found**********
______Disp() //声明虚函数
{
cout << "My name is" << name <<"./n";
}
void setName (char* s)
{
name = new char[ strlen (s) +i];
strcpy (name, s);
}
protected:
char* name;
};
class Student : public Person{
public:
//********** found**********
Student (char * s, double g)______{}
void Disp ()
{
cout << "my name is" << name <<"andmy G.P.A. is" << gpa <<"./n";
}
private:
float gpa;
};
class Professor : public Person{
public:
void setPubls (int n) {publs =n;}
void Disp ()
{
cout << "My name is" <<name <<", Ihave" << publs <<" publications./n";
}
private:
int publs;
};
int main ()
{
//********** found**********
______;
Person x ("Zhang");
p = p->Disp();
Student y("Wang", 3.88);
p = p->Disp();
Professor z;
z. setName ("Li");
z. setPubls (8);
p = p->Disp();
return 0;
}
问答题使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示矩形的CRect类,但类CRect的定义并不完整。请按要求完成下列操作,将类CRect的定义补充完整。
(1)定义私有数据成员leftPoint、topPoint、rightPoint、bottomPoint,分别用于表示矩形左上角及右下角的点的坐标,它们都是double型的数据。请在注释1之后添加适当的语句。
(2)完成默认构造函数CRect的定义,指定默认实参为0,它们都是double型的数据。请在注释2之后添加适当的语句。
(3)定义函数体为空的析构函数。请在注释3之后添加适当的语句。
(4)在main函数中定义GReet类的实例rect2,并把re-
ct1的值赋给rect2。请在注释4之后添加适当的语句。
注意:除在指定位置添加语句之外,不要改动程序中的其他内容。
试题程序:
#include
classCRect
{
private:
//********1********
public:
//********2********
//********3********
voidSetPoints(double,double,double,double);
voidSetLeftPoint(doublem){leftPoint=m;}
voidSetRightPoint(doublem){rightPoint=m;}
voidSetTopPoint(doublem){topPoint=m;}
voidSetBottomPoint(doublem){bottomPoint=m;}
voidDisplay();
};
CReet::CRect(double1,doublet,doubler,doubleb)
{
leftPoint=1;topPoint=t;
rightPoint=r;bottomPoint=b;
}
voidCRect::Setpoints(double1,doublet,doubler,doubleb)
{
leftPoint=1;topPoint=t;
rightPoint=n;bottomPoint=b;
}
voidCRect::Display()
{
cout<<"left-toppointis("< Point<<")"<<’\n’;
cout<<"right-bottompointis("< < }
voidmain()
{
CRectrect0;
rect0.Display();
rect0.SetPoints(20,20.6,30,40);
rect0.Display();
CRectrectl(0,0,150,150);
rect1.SetTopPoint(10.5);
rect1.SetLeftPoint(10.5);
//********4********
rect2.Display( );}
问答题请使用【答题】菜单命令或直接用VC6打开
下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:
ValArray v1={1,2,3,4,5}
ValArray v2={1,2,3,4,5}
要求:
补充编制的内容写在“// ********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为boj文件,并且在本程序中调用。
//ValArray.h
#include <iostream>
using namespace std;
class ValArray {
int * v;
int size;
public:
ValArray (const int * p, int n) : size(n)
{
v = new int[size];
for (int i = 0; i < size; i ++)
v[i] = p[i];
}
ValArray (const ValArray
~ValArray() {delete [] v;}
void print (ostream
for (int i = 0; i < size -1; i ++)
out << v[i] << ",";
out << v[size-1] << "}";
}
void setArray (int i, int val)
{
v[i] = val;
}
};
void writeToFile(const char *);
//main.cpp
#include "ValArray.h"
ValArray:: ValArray (const ValArray
ValArray v1(a, 5);
cout << "ValArray v1 =";
v1.print(cout);
cout << endl;
ValArray v2(v1);
cout << "ValArray v2 =";
v2.print(cout);
cout << endl;
writeToFile(" ");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)完成类MyArrayClass的构造函数,申请数组的大小,请在注释∥********1********后添加适当的语句。
(2)完成类MyArrayClass的析构函数,释放数组,请在注释∥********2********后添加适当的语句。
(3)完成重载运算符“[]”,用来获得指定下标的数据,请在注释∥********3********后添加适当的语句。
(4)完成函数SetElement0,用来设置指定下标的数据。如果数据没有超出范围,则设置数据,并返回1,否则返回0,请在注释∥********4********后添加适当的语句。
注意:除在指定的位置添加语句外,并不要更改程序中的其他语句。
#include
template
class MyArrayClasS
{
private:
T*data ;
int length;
public:
MyArrayClass(int len)
{
length=len;
∥********1********
}
-MyArrayClasS()
{
∥********2********
}
T&operator[](int i)
{
∥********3********
}
bool SetElement(int i,T t)
{
∥********4********
if()
{
data[i]=t ;
return true ;
}
return false;
}
};
int mein()
{
MyArrayClassobj(5);
obj[3]=1;
cout<
问答题请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Initial times are 0 d:0 h:0 m:59 s After one second times are 0 d:0 h:1 m:0 s注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class Clock{public: Clock(unsigned long i=0); void set(unsigned long i=0); void print()const; void tick();//时间前进一秒 Clock operator++();private: unsigned long total_sec,seconds,minutes ,hours, days;};Clock::Clock(unsigned long i) :total sec(i),seconds(i%60), minutes((i/60)%60), hours((i/3 600)%24), days(i/8 64 00){}void Clock::set(unsigned long i){ total sec=i; seconds=i%60; minutes=(i/60)%60; hours=(i/3600)%60; days=i/864 00;}//ERROR**********found**********void Clock::print(){ tout<<days<<”d:”<<hours<<”h:” <<minutes<<”m:”<<seconds<<”s”<<endl;}void Clock::tick(){//ERROR**********found********** set(total sec++);}Clock Clock::operator++(){ tick();//ERROR**********found********** return th~s;}int main(){ Clock ck(59); tout << ”Initial timeS aEe” <<endl; ck.print(); ++ck; tout << ”After one second timesaEe”<<endl; ck.print(); return 0;}
问答题使用VC6打开
proj2下的工程proj2,其中有元素类Element和队列类Queue的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的输出结果应为:
3 8 5 0
5 0 7
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
#define MaxLength 100
class Element{ //"元素"类
public:
int n;
Element(int i=0):n(i){}
};
class Queue{ //"队列"类
Element{*element; //指向存储元素的数组的指针
int tail; //队尾元素的下标
public:
Queue():element(new Element[100]),tail(-1){}
~Queue(){delete[]element;}
void push(Element ele); //在队列尾端添加一个元素
Element pop(); //在队列首端删除一个元素,返回被删元素
Element front() const {return element[0];) //返回队首元素,但不从队列中删除该元素
//******found******
int size()const{return(______);)//返回元素个数
void show()const; //显示集合中所有元素
};
void Queue::push (Element ele){
if(tail==MaxLength-1)
return; //空间满,不做任何处理
//******found******
______;
}
Element Queue::pop(){
if(size()==0)exit(1); //队列空,不做任何处理
Element tmp=element[0];
for(int i=0;i<tail;i++)
element[i]=element[i+1];
//******found******
______;
return tmp;
}
void Queue::show()const{
//******found******
for(______)
cout<<element[i].n<<"";
cout<<endl;
}
int main(){
Queue q;
q.push(3);
q.push(8);
q.push(5);
q.push(0);
q.show();
q.pop();
q.pop();
q.push(7);
q.show();
return 0;
}
问答题简单应用
请使用"答题"菜单或使用VC6打开考生文件夹proj2下的工程proj2,其中有坐标点类Point、圆形类Circle和圆柱形类Cylinder的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
表面积:408.407
体 积:628.319
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动"//**********found**********"。
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: (1)声明类objA1,请在注释//********1********后添加适当的语句。 (2)为类objA0增加友元函数func(),请在注释//********2********后添加适当的语句。 (3)为类objA1增加友元函数func(),请在注释//********3********后添加适当的语句。 (4)函数func()返回objA1对象中的变量和objA0的静态变量的乘积,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>//********1********class objA0{private: static int m A0; //********2********};int objA0..m_A0=10;class objA1{private: int m_A1; //********3********public: objA1(int i) { m_A1=i; }};int func(objA1&obj){ //********4********}int main(){ objA1 obj 0(10); cout<<func(obj0)<<endl; return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modi.cpp,该程序运行时有错,请改正其中错误,使得程序正常运行,并使程序输出的结果为:
c
Test
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在∥********error********的下面。
#include
void main()
{
∥********error********
char C=“C”;
cout<
问答题使用VC++6.0打开
下的源程序文件3.cpp,其中定义了用于表示个人基本信息的类PInfo,但类PInfo的定义并不完整。请按要求完成下列操作,将类PInfo的定义补充完成:
(1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释1之后添加适当的语句。
(2)完成构造函数的定义,要求具有默认值,默认值为身高175,体重70,血型A。请在注释2之后添加适当的语句。
(3)完成类PInfo外成员函数SetInfo的定义。请在注释3之后添加适当的语句。
(4)在主函数main中调用成员函数SetInfo,把对象d2的3个私有数据成员分别设定为身高170,体重64,血型B。请在注释4之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
试题程序:
#include<iostream.h>
class PInfo
{
private:
int height;
int weight;
//********1********
public:
//********2********
:height(ht),weight(wt),bloodType(bt){};
PInfo(PInfo
}
int GetWeight()
{
return weight;
}
int GetBloodType()
{
return bloodType;
}
void SetInfo(int ht,int wt,char bt);
void Display();
};
//********3*******。
{
height=ht;
weight=wt;
bloodType=bt;
}
void PInfo::Display()
{
cout<<"HumanOnfo:";
cout<<height<<"cm,"<<weight<<"Kg,
BloodType"<<bloodType<<endl;
}
void main()
{
PInfo h1(169,61,"A");
PInfo h2;
PInfo h3(h1);
PInfo h4(h2);
//********4********
h1.Display();
h2.Display();
h3.Display();
h4.Display();
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中含有一个源程序文件projl.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Max number is 20 Destructor called. 注意:只能修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。//projl.cpp#include<iostream>USing namespace std;class MyClass{public://ERROR**********found************* void MyClass(int i) {value=i;cout<<”Constructorcalled.”<<endl;} int Max(int X,int Y){return x>Y?x:y;)//求两个整数的最大值//ERROR**********found********** in=Max(int x,int Y, nt Z=0)//求三个整数的最大值 { if(x>y) return X>Z?x:Z; else return y>Z?Y:z; } int GetValue()const{return value;} 一MyClass(){cout<<”Destructorcalled.”<<endl;)Frivate: int value; }; int main() { MyClass obj(10);//ERROR**********found*********** cout<<”The value is”<<value()<<endl; cout<<”Max number is”<<obj.Max(10,20)<<endl; return 0; }
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
100
37
32
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
∥********error********
void main(
{
∥********error********
int m=0 142;
∥********error********
int n=0X27;
int q=32 ;
cout<
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。函数sum(int n)用于计算在n范围内,能被7和11整除的所有整数的和(包括n在内)。 注意:不能修改程序的其他部分,只能补充sum函数。 试题程序: #include<iostream.h> double sum(int n) void main() cout<<sum(80)<<end1; cout<<sum(500)<<end1; cout<<sum(1000)<<end1; return;
问答题请使用“答题”菜单或使用VC6打开考生文件夹projl下的工程projl,此工程包含程序文件main.cpp,其中有类TimesTable(“乘法口诀表”)的定义和主函数main的定义。程序中位于每个//ERROR **********found**********下的语句行有错误,请加以改正。更正后程序的输出应该是:1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=82*8=16 3*8=24 4*8=32 5*8=加6*8=48 7*8=56 8*8=641*9=92*9=18 3*9=27 4*9=36 5*9=45 6*9=547*9=63 8*9=72 9*9=81注意:只能修改每个//ERROR **********found**********下的那一行,不要改动程序中的其他内容。#include<iostream>#include<iomanip>using namespace std;class TimesTable//乘法口诀表{public://ERROR **********found**********TimesTable(int n=9){count:n;)void Print()const;private:const int count;};//ERROR **********found**********void Print()const{int i,j;//ERROR **********found**********for(i=1;i<count;i++){for(J=1;j<=i;j++)cout<<j<<‘* ’<<i<<‘=’<<left<<setw(4)<<j*i;cout<<endl;}}int main(){TimesTable obj;obj,Print();return 0;}
问答题使用VC6打开考生文件夹下的工程MyProj13。此工程包含一个源程序文件MyMain12.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。 请按要求完成下列操作,将类的定义补充完成: ①定义类shapes的保护成员x和y,它们都是整型变量。请在注释“//* *1* *”之后添加适当的语句。 ②完成构造函数shapes(int d,int w)定义,使类shapes的保护成员x、y分别初始化为d、w,并分别具有默认值0、0。请在注释“//* *2* *”之后添加适当的语句。 ③完成类shapes的成员函数setvalue(int d,int w)的定义,shapes类的数据成员x和y分别被设置成d和w。请在注释“//* *3* *”之后添加适当的语句。 ④将类shapes的成员函数void disp()设置成纯虚函数。请在注释“//* *4* *”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件MyMain13.cpp清单如下: //MyMain13.cpp #include<iostream> using namespace std; class shapes //* * * 1 * * * public: //* * * 2 * * * void setvalue(int d,int w) //* * * 3 * * * //* * * 4 * * * ; class square : public Shapes public: void disp() cout<<x*y<<end1; ; int main() shapes*ptr; square s1; ptr=&s1; ptr->setvalue(10,5); ptr->disp(); return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: 每卖出一个瓜,则计算瓜的重量,还要计算所有卖出瓜的总重量以及总个数,同时允许退货,请按照以下的操作,把类补充完整 (1)定义类Cmelon的私有静态数据成员float型变量totalweight和int型变量totalNo,请在注释//********1********后添加适当的语句。 (2)完成类Cmelon的带一个float型变量w的构造函数,并把这个w加到totalweight中,并且totalNo自加。请在注释//********2********后添加适当的语句。 (3)在析构函数中,在totalweight中减去weight,然后totalNo自减,请在注释//********3********后添加适当的语句。 (4)完成静态成员变量的初始化为0,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>Class Cmelon{private: float weight; //********1******** Static int totaiNo;public: Cmelon(float w) { //********2******** totalweight +=w; totalNo++; } ~Cmelon() { //********3******** totalweight =-=weight;} void display() { cout;<<"Sell a melon with"<<weight<<"kg"<<end1; cout: << "Total sellnumber:"<<totaiNo<<end1; cout: << "Total sellweight:"<<totalweight<<"kg"<<end1<<end1; }};//********4********float:Cmelon::totalweight=0.0;int main (){ Cmelon melonl(1.2); melonl.display(); Cmelon melon2(2.3); melon2.display(); return 0;}