问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。类Person完成对人的姓名和年龄的绑定。函数fun()获得年龄比较大的姓名。然后输出这个姓名到屏幕。 其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)完成类的构造函数, 请在注释//********1********后添加适当的语句。 (2)完成构造函数对姓名的赋值,请在注释//********2********后添加适当的语句。 (3)定义类的友元函数fun(),请在注释//********3********后添加适当的语句。 (4)补充函数fun()的年龄比较,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>Class Person{public://********1******** { int i; for(i=0;sUserrName[i]!=0;i++1 { m UserName[i]=suserrName[i]; )//********2******** m old=nold; }private: char m_UserName[32]; int m_Old;//********3********};void fun(char*s,Person&personl,Person&person2){//********4******** if( ) { for(int i=0;personl.m UserName[i]!=0;i++) { s[i]=personl.mUserName[i]; s[i+1]=0; } } else { for(int i=0;person2.mUserName[i]!=0;i++) ( s[i] =person2.mUserName[i]; s[i+1]=0; } }}Void main(){ char s[32]; Person p1("abc",20); Person p2("def",30); fun(s,p1,p2); cout<<s<<endl; return;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程含有一个源程序文件pmj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The value is 10 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostream>using namespace std;class MyClass{ int value;public://ERROR**********found********** void MyClass(int val):value(val) {} int GetValue()const{return value;} void SetValue(int val); }; //ERROR**********found********** inline void SetValue(int val){value =val;} int main() { MyClass obj(0); obj.SetValue(10); //ERROR**********found*********** 下列语句功能是输出obj的成员value的值 cout<<”The value is”<<obj.value<<endl; return 0; }
问答题请使用VG6或使用【答题】菜单打开考生文件夹pmjl下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: There is a white dog named Hoho. There is a black dog named HaIla. There is a motley dog named Hihi. 注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;//狗的颜色:黑、白、黄、褐、花、其他enum DOGCOLOR{BLACK,WHTTE,YELLOW,BROWN,PTEBALD,OTHER);class Dog {//狗类 DOGCOLOR colot; char name[2 O]; static int count;public: Dog(char name[],DOGCOLOR color){ strcpy(this一>name,name);//ERROR**********found********** strcpy(this一>color,color); } DOGCOLOR getColor()const{return color;)//ERROR**********found********** COnSt char*get:Name()const{return*name;) const char* getColorString()const{ switch(colot){ CaSe BLACK:return”blaCk”; case WHITE:return”white”; CaSe YELLOW:return”yellow”; case BROWN:return”brown”; CaSe PIEBALD:return”piebald”; } return”motley”; } Void show()const{ cout<<”There is a”<<getColorString()<<”dog named”<<name<<’.’<<endl; } }; int main(){ //ERROR**********found********** Dog dog1(”Hobo”,WHITE),dog2(”Haha”,BLACK);dog3(”Hihi”,OTHER); dog1.show(); dog2.show(); dog3.show(); return 0; }
问答题使用VC6打开考生文件夹下的工程RevProj13。此工程包含一个源程序文件RevMain13.cpp,但该程序中类的定义有错误。请改正程序中的错误,使它能得到正确结果。 注意,不要改动主函数,不得删行或增行,也不得更改程序的结构。 源程序文件RevMain13.cpp中的程序清单如下; //RevMain13.cpp #include<iostream> using namespace std; class MyClass public: MyClass(int a=0,b=1); Print(); private: int x; int y; ; MyClass::MyClass(int a=0,int b=1) x=a; y=b; void MyClass::Print() cout<<"x="<<x<<end1; cout<<"y= "<<y<<end1; int main() MyClass obj(1,2) obj.Print(); return 0;
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.epp。函数char*GetNum(char*src,char*buf)从src开始扫描下一个数字字符序列,并将其作为一个字符串取出放人字符串空间buf中。函数返回扫描的终止位置,如果返回NULL表示没有扫描到数字字符序列。 运行程序时,如果输入的一行字符序列是 ABC012XYZ378MN274WS则输出为: Digit string 1 is 012 Digit string 2 is 378 Digit string 3 is 274 注意:只在横线处编写适当代码,不要删除或移动“//****found****”.//proj2.cpp#include<iostream>using namespace std;char*GetNum(char*see,char*bur){ while(*src!=’\0’) { if(isdigit(*src))break; SrC++; } if(*src==’\0’)//*******found********_______;while(*src!=’\0’&& isdigit(*src)){//******** found********_______;bur++;src++; } *bur=’\0’; return srC;}int main(){char str[1 00],digits[20];cin.getline(str,1 00);char*P=str;int i=1;while((p=GetNum(p,digits))!=NULL){ tout<<”Digit string”<<i<<”is”<<digits<<endl;//********found********_________;}return 0;}
问答题改错题
使用VC6打开考生文件夹下的工程test16_1,此工程包含一个源程序文件test16_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果为:
Which act(a or b)?b
act1
act2
Bact3
Bact4
Bend
源程序文件test16_1.cpp清单如下:
#include
class A
{
public:
virtual void act1(){coutact1();
delete a;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个sort函数。程序的正确输出应为: 排序前:7,1,3,11,6,9,12,10,8,4,5,2 排序后:1,2,3,4,5,6,7,8,9,10,11,12 要求: 补充编制的内容写在“//**********333**********”与“**********666**********”两行之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序调用。//DataList.h#include<iostream>using namespace std;class DataList{//数据表类 int len; double*d;public: DataList(int len,double data[]=NULL); 一DataList(){delete[]d;) int length()const{return len;)//数据表长度(即数据元素的个数) double getElement(int i)const{re-turn d[i];} void sort();//数据表排序 void show()const;//显示数据表 }; void writeToFile (char *, const DataList&); //main.cpp #include”DataList.h’’ DataList::DataList(int len,double data[]):len(len){ d=new double[1en]; for(int i=0;i<len;i++) d[i]=(data==NULL?0.0:data [i]); } void DataList::sort(){//数据表排序 //********333******** //********666******** } void DataList::show()const{ //显示数据表 for(int i=0;i<len一1;i++)cout<<d[i]<<","; cout<<d[len一1]<<endl; } int main(){ double S[]={7,1,3,11,6,9,12,10,8,4,5,2}; DataList list(12,s); COUt<<"排序前:"; list.show(); list.sort(); cout<<endl<<"排序后:"; list.show(); writeToFile(””,list); return 0; }
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp。函数char * GetNum(char * src,char * buf)从src开始扫描下一个数字字符序列,并将其作为一个字符串取出放入字符串空间buf中。函数返回扫描的终止位置,如果返回NULL表示没有扫描到数字字符序列。
运行程序时,如果输入的一行字符序列是
ABC012XYZ378MN274WS
则输出为:
Digit string 1 is 012
Digit string 2 is 378
Digit string 3 is 274
注意:只在横线处编写适当代码,不要删除或移动“// ****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
char * GetNum(char * src, char * buf)
{
while(* src!="/0")
{
if(isdigit(* src)) break;
src ++;
}
if(* src=="/0")
// *******found*******
______;
while (* src!="/0"
buf ++;
src ++;
}
*buf = "/0";
return src;
}
int main()
{
char str[100], digits[20];
cin.getline(str,100);
char * p=str;
int i=1;
while ((p = GetNum (p, digits))!=NULL)
{
cout << "Digit string" << i << "is" << digits << endl;
// *******found*******
______;
}
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义的MyString类是一个用于表示字符串的类。假设字符串由英文单词组成,单词与单词之间使用一个空格作为分隔符。成员函数wordCount的功能是计算组成字符串的单词的个数。
例如,字符串“dog”由1个单词组成;字符串“the quickbrown fox jumps over the lazy dog”由9个单词组成。请编写成员函数wordCount。在main函数中给出了一组测试数据,此时程序应显示:
读取输入文件...
STR1=1
STR2=9
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//mystring.h
#include <iostream>
#include <string.h>
using namespace std;
class MyString {
public:
MyString(const char* s)
{
str = new char[strlen(s)+1];
strcpy(str, s);
}
~MyString() { delete [] str;}
int wordCount() const;
private:
char * str;
};
void writeToFile(char * , int);
//main.cpp
#include <fstream>
#include "mystring.h"
int MyString::wordCount() const
{
//******** 333********
//******** 666********
}
int main()
{
char inname[128], pathname[80];
strcpy(pathname, "");
sprintf (inname, "in. dat", pathname);
cout << "读取输入文件... /n/n";
ifstream infile (inname);
if (infile.fail ()) {
cerr << "打开输入文件失败!";
exit(1);
}
char buf[4096];
infile.getline(buf, 4096);
MyString str1 ("dog"), str2 ("the quick brown fox jumps over the lazy dog"), str3 (buf);
str1.wordCount ();
cout << "STRI = " << str1.wordCount () << endl;
cout << "STR2 = " << str2.wordCount() << endl << endl;
writeToFile(pathname,str3.wordCount());
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使该程序的输出结果为: 20,15 15,20 注意:错误的语句在//*****error******的下面,修改该语句即可。#include<iostream.h>//*****error******void Exchangel(int m,int n){ int t=m; m=n; n=t;}//*****error******void Exchange2(int m,int n){ int t=*m; *m =*n; *n =t;}void main(){ int b=20; int a=15; Exchange1(a,b); cout<<a<<','<<b<<end1; //*****error****** Exchange2(a,b); cout<<a<<','<<b<<end1;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程包含一个源程序文件proj1.cpp。文件中将表示数组元素个数的常量Size定义为4,并用int类型对类模板进行了实例化。文件中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: 1 2 3 4 注意:模板参数名用T。只修改注释“//ERROR ********found********”的下一行语句,不要改动程序中的其他内容。 //proj1.cpp #include <iostream> using namespace std; //将数组元素个数 Size定义为4 //ERROR ********found******** const int Size; template <typename T> class MyClass public: MyClass(T*p) for(int i=0;i<Size;i++) array[i]=p[i]; void Print(); private: T array[Size]; ; template <typename T> void MyClass::Print() for (int i=0;i<Size;i++) cout<<array[i]<<'/t'; int main() int intArray[Size]=1,2,3,4; //ERROR ********found******** MyClass <double> obj(intArray); obj.Print(); cout<<endl; return 0;
问答题使用VC6打开考生文件夹下的工程test15_1,此工程包含一个源程序文件test15_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下: My object has member 7 源程序文件test15_1.cpp清单如下: #include<iostream.h> class MyClass public: MyClass(int mem)member=mem; ~MyClass() int GetAge()const return member; private: int member; ; /*****+********+** found *************/ void main() int mem=7; /*************** found ***************/ MyClass myObj=MakeObject(mem); cout<<"My object has member"<<myObj->GetAge()<<endl; /***************** found ****************/ delete; MyClass *MakeObject(int mem) MyClass *pMyClass=new MyClass(mem); return pMyClass;
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。完成函数ToUpper(char木des,char*str),该函数实现把str字符串中小写字符转换成大写字符,并存发在des中。
例如:str=“aBcdrFGHijK”:
则:des=“ABCDEFGHIJK”;
注意:不能修改程序的其他部分,只能补充ToUpper 0函数。
#include
#define MAXLEN 1024
void ToUpper(char*des,char*
str)
{
}
void main()
{
char dest[MAXLEN],
char*str=”aBcdrFGHij K”;
ToUpper(dest,str);
cout<
问答题使用VC6打开考生文件夹下的工程test33_3。此工程包含一个test33_3.cpp,其中定义了表示时间的类Time,但Time类定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义类Time的私有数据成员hours、minutes和seconds,它们都是int型的数据,分别表示时间的小时、分和秒。请在注释“//**1**”之后添加适当的语句。 (2)完成类Time缺省构造函数的定义,该函数将数据成员hours、minutes和seconds的值初始化为0,请在注释“//** 2**”之后添加适当的语句。 (3)完成类Time带参构造函数的定义,该函数将数据成员hours、minutes和seconds的值分别初始化为参数h、m和s的值,请在注释“//**3**”之后添加适当的语句。 (4)完成类Time中运算符“+”的重载,注意小时、分和秒在相加时的进位问题。请在注释“//**4**”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 程序输出结果如下: 8:17:11 源程序文件test33_3.cpp清单如下: #include <iostream.h> class Time //** 1 ** public: Time ( ) //** 2 ** seconds=0; Time(int h, int m, int s) //** 3 ** minutes=m; seconds=s; Time operator +(Time void gettime(); ; Time Time::operator +(Time m=time.minutes+minutes+s/60; h=time.hours+hours+m/60; Time result(h,m%60,s%60); return result; void Time::gettime() cout<<hours<<":"<<minutes<<":"<<seconds<<end1; void main( ) Time t1(3,20,15),t2(4,56,56),t3; t3=t1+t2; t3.gettime();
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分的程序。实现函数sort(intA[],int n),用冒泡法将数组排序。 提示:所谓冒泡法,就是每次把相邻的两个数交换,较大的数交换到后面。这样下标从0到n-1的数与其后面相邻的数交换,可以把最大的数交换到数组的末端。进行n次下标从0到n-1的交换,则数组则会变成有序的,而且是由大到小的顺序。 注意:不能修改程序的其他部分,并且不能删除其他的部分,也不能修改程序的结构。#include<iostream.h>#define N 10void sort(int A[N],int n){}int main(){ int A[N]={5,7,4,6,i0,13,78,一4,9,2 0); Sort(A,10); for(int i=0;i<sizeof(A)/Sizeof(int),i++) { cout<<A[i]<<‘’; } cout<<end1; return 0;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出mot。rcar类和bicycle类。然后以mottorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream.h>class vehicle{private:int MaxSpeed;int Weight;public://**********found**********vehicle(intmaxspeed,intweight):_______~vehicle(){};int getMaxSpeed(){return MaxSpeed;}int getWeight(){return Weight;}};//**********found**********class bicycle:______________public vehicle{private:int Height;public:bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}int getHeight(){return 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**********classmotorcycle:______________{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,10);cout<<a.getMaxSpeed()<<endl;cout<<a.getWeight()<<endl;cout<<a.getHeight()<<endl;cout<<a.getSeatNum()<<endl;}
问答题使用VC++6.0打开
下的源程序文件1.cpp,该程序运行时有错,请改正程序中的错误,使程序输出的结果为
150
200
注意:错误的语句在//******error******的下面,修改该语句即可。
提示:定义Num1为一个整型数据位长的变量。
试题程序:
#include<iostream.h>
//******error******
struct
{
//******error******
int Num1:1;
int Num2;
}
MyStruct;
void main()
{
MyStruct mstr;
mstr.Num1=150;
mstr.Num2=15;
//******error******
int * ptr=
cout<<*ptr<<endl;
*ptr=200;
cout<<*ptr<<endl;
}
问答题改错题
使用VC6打开考生文件夹下的工程kt10_1,此工程包含一个源程序文件kt10_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果为:
classBase
classD1
classD2
classD3
finBase
源程序文件kt10_1.cpp清单如下:
#include
classBase
{ public:
Base(){cout<<"classBase"<
问答题请使用【答题】菜单命令或直接用VO6打开考生文件夹下的工程prog3,其中声明了ViArray类,该类在内部维护一个动态分配的整型数组。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>us ing 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 void writeTOFile(const char*); //main.cpp #include”ValArray.h” ValArray::ValArray(const ValArray v2.print(cout); cout<<endl; writeToFile(””); return 0;}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
Leaf Node
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class Component {
public:
//声明纯虚函数print()
// **********found**********
______
};
class Composite:public Component {
public:
// **********found**********
void setChild(______)
{
m_child = child;
}
virtual void print() const
{
m_child -> print();
}
private:
Component * m_child;
};
class Leaf:public Component {
public:
virtual void print() const
{
// **********found**********
______
}
};
int main()
{
Leaf node;
Composite comp;
comp.setChild(
Component * p =
p -> print();
return 0;
}