单选题下列程序的运行结果是( )。
#include
intx=5;
intfun(inta)
{
intc:
c=X*a:
returnC;
}
voidmain()
{
intx=3,a=4;
x=x+fun(a);
cout<<"x="< }
单选题通过派生类的对象可直接访问其( )。
单选题下列程序的运行结果为( )。
#include<iostream.h>
class Location {
protected:
int X,Y;
publiC:
void SeX(int myx){X=myx;}
void SetY(int myy){Y=myy;}
void showxy( ) {cout<<"X=" <<X<<" " <<"Y"=<< Y<<
endl;} };
Class Rectangle :public Location{
private:
int H,W;
public:
void SetH(int myh){H=myh;}
void SetW(int myw) {W=myw;}
void show( ) {cout <<"X="<<X<<" "<<"Y="<<"<<
Y<<" "<<"H="<< H<<" " <<"W=" <<W
<<endl;} };
void main( )
{ Rectangle r1;
r1.SetX(3);
r1.SetY(5);
r1.SetH(4);
r1.SetW(6);
r1.showxy( );
r1. show( );}
单选题有函数模板声明和一些变量定义如下:
template<class T1,class T2,class T3>T1 sum(T2,T3);
double d1,d2;
则下列调用中,错误的是______。
单选题已知Bag是一个类,并有函数FBag定义如下: Void FBag(){ Bag *ptr, my, colleage[2], }则在调用函数FBag()后,Bag类的析构函数被调用的次数是
单选题有如下类声明: class MyBASE { int
k; public: void set(int n) {k=n;}
int get()const {return k; } }; class
MyDERIVED: protected MyBASE { Protected: int
j; public: void set(int m,int n){MyBASE::
set(m);j=n;} int get () const {return MyBASE:: get ()+j;
} };则类MyDERIVED 中保护的数据成员和成员函数的个数是______ 。
A. 4
B. 3
C. 2
D. 1
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程prog2,其中定义了Stack类和Entry类。Stack是一个基于链式存储结构的栈,Entry表示存储在栈中的数据顶。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Entry {
public:
Entry* next;
int data;
//********** found**********
Entry (Entry * n, int d): ______, data(d) {}
};
class Stack {
Entry* top;
public:
Stack() : top(0) {}
~Stack()
{
while (top!= 0)
{
Entry* tmp = top;
//********** found**********
top =______;
delete tmp;
}
}
void push (int data)
{
//********** found**********
top = new Entry(______, data);
}
int pop ( )
{
if (top = 0) return0;
//********** found**********
int result = ______;
top = top->next;
return result;
}
};
int main ( )
{
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Stack s;
int i = 0;
for (i = 0; i < 10; i++) {
cout << a[i] << ";
s.push(a[i]);
}
cout << endl;
for (i = 0; i <10; i++) {
cout << s.pop() <<'';
}
cout << endl;
return 0;
}
问答题使用VC6打开考生文件夹下的工程test16_3,此工程包含一个源程序文件test16_3.cpp,其中定义了类Cat,但类Cat的定义并不完整。请按要求完成下列操作,将类Cat的定义补充完整。 (1)定义类Cat 的保护数据成员itsAge用于表示年龄,它是int型的数据。请在注释“//**1**”之后添加适当的语句。 (2)定义类Cat的保护数据成员GetAge()的类外定义,返回成员的itsAge值,请在注释“//**2**”之后添加适当的语句。 (3)完成成员函数SetAge(int age)的类外定义,把数据成员itsAge赋值为参数age的值,请在注释“//**3**”之后添加适当的语句。 (4)完成成员函数Meow的类外定义,使其输出字节符串“meow.”到屏幕上。请在注释“**4**”后添加适当的语句。 输出结果如下: meow. kitty is a cat who is 5 years old. meow. 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件test16_3.cpp清单如下: #include<iostream.h> class Cat public: int GetAge(); void SetAge(int age); void Meow(); protected: //**1** ; int Cat::GetAge() //**2** void Cat::SetAge(int age) //**3** void Cat: :Meow() //**4** void main ( ) Cat kitty; kitty.Meow(); kitty. SetAge(5); cout<<"kitty is a cat who is "; cout<<kitty.GetAge(); cout<<" years old./n"; kitty. Meow();
问答题请使用VC6或使用[答题]菜单打开考生文件夹pmj2下的工程proj2,该工程中包含程序文件main.cpp,其中有类Mammal(“哺乳动物”)、类Elephant(“大象”)、类Mouse(“老鼠”)的定义和主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述定义。此程序的正确输出结果应为:
ELEPHANT
MOUSE
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
enum category { EMPTY, ELEPHANT, MOUSE};
char* output [] = { "EMPTY", "ELEPHANT", "MOUSE"};
class Mammal
{
public:
Mammal(char* str)
{
//**********found**********
name = new______
strcpy(name, str);
}
virtual char* WhoAmI()=0;
virtual ~Mammal() { delete[] name;
}
void Print () { cout <<WhoAmI () <<endl; }
private:
char* name;
};
class Elephant : public Mammal
{
public:
//**********found**********
Elephant(char* str): ______{}
char* WhoAmI () { return output[ELEPHANT]; }
};
class Mouse : public Mammal
{
public:
Mouse (char* str) : Mammal(str) {
//**********found**********
char* WhoAmI () {______}
};
int main ()
{
//**********found**********
Mammal * pm = new ______ ( "Huanhuan" );
pm -> Print ();
delete pm;
pm = new Mouse ("Micky");
pm -> Print ();
delete pm;
return 0;
}
问答题使用VC6打开
下的源程序文件modi2.cpp。请补充完整程序fun(int i),使程序完成以下功能:
如果i=5,则输出如下5行井号。
#
##
###
####
#####
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
#include <iostream.h>
void fun(int i)
{
}
int main()
{
fun (5);
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
i=5
i=10
i=15
i=20
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
class CMyClass
{
public:
template
void func(T x,T Y)
{
∥********error********
T i=0;
if(x>=i)
{
i=i+x;
}
else
{
i=i+y;
}
cout<<“i=”<
问答题使用VC6打开考生文件夹下的工程proj1。此工程包含一个源程序文件mainl.cpp,但该程序运行有问题。请改正主函数中的错误,使程序的输出结果是: member = 0 member = 5 member = 10 源程序文件 mainl.cpp 清单如下: //mainl.cpp #include <iostream> using namespace std; class MyClass public: MyClass(int i) member=i; void SetMember(int m) member=m; int GetMember()const return member; void Print() const cout<<"member="<<member<<end1; private: int member; ; int main() /***************found***********/ MyClass obj1; obj1. Print(); MyClass obj2(3); /***************found***********/ obj1.member = 5; /***************found***********/ MyClass. SetMember(10); obj1. Print(); obj2. Print(); return 0;
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中声明了MiniComplex是一个用于表示复数的类。请编写这个operator+运算符函数,以实现复数的求和运算。两个复数的和是指这样一个复数:其实部等于两个复数的实部之和,其虚部等于两个复数的虚部之和。例如,(23+34i)+(56+35i)等于(79+69i)。
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Minicomplex.h
#include <iostream>
using namespace std;
class MiniComplex //复数类
{
public:
friend ostream
return osObject;
}
friend istream
isObject >> complex, realPart >> ch >> complex.imagPart >> ch;
return isObject;
}
MiniComplex (double real = 0, double imag = 0);
//构造函数
MiniComplex operator + (const MiniComplex
//重载运算符+
private:
double realPart; //存储实部变量
double imagPart; //存储虚部变量
};
void writeToFile(char *);
//main.cpp
#include "MiniComplex.h"
MiniComplex::MiniComplex (double real, double imag){realPart=real;imagPart=imag;}
MiniComplex MiniComplex::operator+(const MiniComplex
MiniComplex num1 (23,34),num2 (56,35);
cout << "Initial Value of Num1 =" << num1 << "/nInitial Value of Num2 =" << num2 << endl;
cout << num1 << "+" << num2 << "=" << num1 + num2 << endl; //使用重载的加号运算符
writeToFile (" ");
return 0;
}
问答题使用VC++6.0打开
下的源程序文件2.cpp。阅读下列函数说明和代码,实现函数sort(int A[],int n),用选择排序法将数组从大到小排序。
提示:选择排序法的思想是
(1)反复从还未排好序的那部分线性表中选出关键字最小的结点。
(2)按照从线性表中选出的顺序排列结点,重新组成线性表。
(3)直到未排序的那部分为空,使得重新形成的线性表是一个有序的线性表。
补充函数sort(int A[],int n),实现选择排序。
注意:请勿改动主函数。
试题程序:
#include<iostream.h>
#define N 10
void sort(int A[N],int n)
{
}
int main()
{
int A[N]={-72,54,-6,7,18,102,0,4,-11,1};
sort(A,10);
for(int i=0;i<sizeof(A)/sizeof(int);i++)
{
cout<<A[i]<<"";
}
cout<<endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整。请按要求完成下列操作,将类Date的定义补充完成。
(1)定义私有数据成员year、month和day,分别用于表示年、月和日,它们都是int型的数据。请在注释∥*******1*******之后添加适当的语句。
(2)完成默认构造函数Date的定义,使Date对象的默认值为:year=1,month=1,day=1,请在注释∥*******2*******之后添加适当的语句。
(3)完成重载构造函数Date(int y,int m,int d)的定义,把数据成员year、month和day分别初始化为参数y、m和d的值,请在注释∥*******3*******之后添加适当的语句。
(4)完成成员函数print()的类外定义,使其以“年一月一日”的格式将Date对象的值输出到屏幕上,例如:2008-8-8。请在注释∥*******4*******之后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
#include
class Date{
public:
∥*******2*******
Date(int y,int m,int d)
{
∥*******3*******
}
void print()const;
private:
∥data member
∥*******1*******
};
void Date::print()const
{
∥*******4*******
}
int main(){
Date national_day(1949,i0,1);
national_day.print();
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的IntSet是一个用于表示正整数集合的类。IntSet的成员函数Merge的功能是求当前集合与另一个集合的并集,在Merge中可以使用成员函数IsMemberOf判断一个正整数是否在集合中。请完成成员函数Merge。在main函数中给出了一组测试数据,此时程序的输出应该是:
求并集前:
1 2 3 5 8 10
2 8 9 11 30 56 67
求并集后:
1 2 3 5 8 10
2 8 9 11 30 56 67
1 2 3 5 8 10 9 11 30 56 67
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Intset.h
#include <iostream>
using namespace std;
const int Max=100;
class IntSet
{
public:
IntSet() //构造一个空集合
{
end = -1;
}
IntSet(int a[], int size) //构造一个包含数组a中size个元素的集合
{
if (size >=Max)
end=Max-1;
else
end =size-1;
for(int i=0; i<=end; i++)
element[i]=a[i];
}
bool IsMemberOf(int a) //判断a是否为集合中的元素
{
for(int i=0; i<=end; i++)
if (element[i]==a)
return true;
return false;
}
int GetEnd() {return end;} //返回最后一个元素的下标
int GetElement(int i) {return element[i]; } //返回下标i处的元素
IntSet Merge(IntSet //求当前集合与集合set的并集
void Print() //输出集合中的所有元素
{
for(int i=0; i<=end; i++)
if((i+1)% 20==0)
cout << element[i] << endl;
else
cout <<element[i] <<' ';
cout <<endl;
}
private:
int element[Max];
int end;
};
void writeToFile(const char * );
//main.cpp
#include "IntSet.h"
IntSet IntSet::Merge(IntSet
//******** 333********
//******** 666********
return IntSet(a, size);
}
int main ()
{
int a[] = {1, 2, 3, 5, 8, 10};
int b[] ={2, 8, 9, 11, 30, 56, 67};
IntSet set1(a, 6), set2(b, 7), set3;
cout <<"求并集前:" <<endl;
set1.Print();
set2.Print();
set3.Print();
set3 =set1.Merge(set2);
cout << endl << "求并集后: " << endl;
set1.Print();
set2.Print();
set3.Print();
writeToFile("");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi l.cpp,该程序运行时有错,请改正错误,使得程序正确执行,并且输出以下语句:
TestClass1:0
TestClass2
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在
∥********error********的下面。
#include
struct TestClass1
{
TestClass1(int i=0)
{
m i=i;
}
void print()
{
cout<<“TestClassl:”<
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,此工程包含有一个源程序文件proj2.cpp。其中定义了Base1类、Base2类和Derived类。
Base1是一个抽象类,其类体中声明了纯虚函数Show。Base2类的构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中,复制功能要求通过调用strcpy函数来实现。Derived类以公有继承方式继承Base1类,以私有继承方式继承Base2类。在Derived类的构造函数的成员初始化列表中调用Base类的构造函数。
请在程序中的横线处填写适当的代码,然后删除横线,以完成Base1、Base2和Derived类的功能。此程序的正确输出结果应为:
I"m a derived class.
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
// proj2.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Base1 {
public:
// ********found******** 下列语句需要声明纯虚函数Show
______;
};
class Base2 {
protected:
char * _p;
Base2 (const char * s)
{
_p = new char[strlen (s) +i];
// ********found******** 下列语句将形参指向的字符串常量复制到该类的字符数组中
}
~Base2() {delete [] _p;}
};
// ********found******** Derived类公有继承Base1,私有继承Base2类
class Derived:______{
public:
// ********found******** 以下构造函数调用Base2类构造函数
Derived (const char * s):______
{ }
void Show()
{cout << _p << endl;}
};
int main()
{
Base1 * pb = new Derived("I"m a derived class.");
pb -> Show();
delete pb;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数factor(int*des,int&length,int n)实现的功能是:将n所有因数存放到des中,通过length引用返回因数的个数,比如fator(a,length,20)执行后,则a={1,2,4,5,10,20},length=6。注意:只能补充函数factor(),请勿改动其他部分的内容。#include<10Stream.h>#define MAXNLEN100void factor(int*des,int&length,int n){}void main(){int a[MAXNLEN],length;int n=20;factor(a,length,n);for(inti=0;i<length;i++){cout<<a[i]<<" ";}cout<<endl;return;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。该程序从键盘读入整数,并按从大到小的顺序输出输入整数中互不相等的那些整数。程序一边读入整数,一边构造一个以大到小顺序链接的链表,直至输入0时结束。然后顺序输出链表上各表元的整数值。主函数每读入一个整数,就调用函数fun(),函数fun()将还未出现在链表上的整数按从大到小的顺序插入到链表中。为了插入方便,链表在表首有一个辅助表元。注意:不能修改程序的其他部分,只能修改fun()函数。#include<iostream>class NODE{public:int data;NODE *next;};void fun(NODE *list,int x){}void main(){int.x;NODE *head,*p;/*首先建立只有辅助袁元的空链表*/head=new NODE;head->next=NULL;std::cout<<"Enter integers, end with0"<<std::endl;while(1){std::cin>>x;if(x==0)break;fun(head,x),}for(p=head->next;p!=NULL;p=p->next;std::cout<<p->data<<' ';std::cout<<Std::endl;do{p=head->next;delete head;head=p;)while(p);}