计算机二级

第1481题

已知类CC和函数test的定义如下:

class CC {
public :
    CC( ) { cout << '1'; }
    ~CC( ) { cout << '0'; }
};
void test ( CC c ) { cout << '2'; }

且有如下main函数:

int main(){
    CC c;
    test(c);
    return 0;
}

运行这个main函数的输出是( )。

第1482题

已知类Test的定义如下:

class Test{
private :
    static int num ;
public :
    void print ( int num ) {
        cout << ++ num ;
        cout << ++ this -> num;
    }
};
int Test::num=0;

且有如下程序段:

int main(){
    Test t1,t2;
    t1.print(0);
    t2.print(1);
    return 0;
}

运行这个程序段的输出是( )。

第1483题

已知类MyClass的定义如下:

class MyClass {
private :
    int num ;
public :
    MyClass ( int d ) : num ( d ) { }
    MyClass ( const MyClass& original ) : num ( original . num ) { }
    friend void print ( MyClass c ) { cout << c.num }
} ;

且有如下程序段:

MyClass c(5) ;
MyClass *p1;
p1=&c;
print(*p1) ;
print(c) ;

运行这个程序段的过程中,对类MyClass的复制构造函数的调用次数是 ( )。

第1484题

有如下输出语句:

cout<<setw(5)<<fixed<<setprecigion(3)<<3.1415926;

此语句的输出结果是( )。

第1485题

已知类模板Test定义如下:

template<typename T1,typename T2>
class Test{
public:
    void foo(T2 t);
};

则以下针对foo函数的类外定义中语法正确的是( )。

第1486题

已知类CC以成员函数方式重载了二元运算符*,c1和c2是CC类的 两个对象,则下列对该运算符的调用中错误的是( )。

第1487题

已知类Base和类Derived的定义如下:

class Base {
    int n ;
public :
    Base ( int d ) : n( d ) { }
} ;
class Derived : public Base {
public :
    ______________
} ;

其中横线处应为类Derived的构造函数的定义。在下列构造函数的定义 中,正确的是( )。

第1488题

已知类MyClass的定义如下:

# include<iostream>
using namespace std;
class MyClass {
private :
    static int static_data;
    const int const_data;
public:
    MyClass ( int d ) : const_data ( d ) { }
    static void foo1 ( ) {
        cout << const_data ; //①
    }
    void foo2 ( ) const {
        cout << static_data ; //②
    }
};
int MyClass :: static_data = 0 ;

下列说法中正确的是( )。

第1489题

已知函数foo定义如下:

void foo ( int a,int&b,int*c) {
    a++; b++; (*c) ++;
}

且有如下程序段:

int a=1,b=2,c=3;
foo (a,b,&c) ;
cout<<a<<b<<c;

运行这个程序段的输出是( )。

第1490题

有如下程序段:

int*a=new int[2];
a[0]=0;
a[1]=1;
int**b=&a:
cout<<**b;

运行这个程序段将输出( )。

第1491题

有如下程序段:

int i = 2;
do{
    i *= i;
    cout<<'#';
    if(i>1000)break;
}while(1) ;

运行这个程序段时,输出字符‘#’的个数是( )。

第1492题

若变量a的定义为“int a=8;”,则下列逻辑表达式中其值为false的 是( )。

第1493题

下列不属于C++关键字的是( )。

第1494题

下列不属于C++预定义的流对象的是( )。

第1495题

下列关于函数模板的说法,正确的是( )。

第1496题

下列关于构造函数的说法中,正确的是( )。

第1497题

下列关于函数的说法中,正确的是( )。

第1498题

请使用“答题”菜单或使用VC6打开考生文件夹proj1下的工程文件 proj1。此工程包含程序文件 main.cpp,其中有类Door和主函数main的 定义。程序中位于每个“//ERROR*********found*********”下的语句行 有错误,请加以更正。更正后程序的输出应该是:

 打开503号门...门是锁着的,打不开。打开503号门的 锁...锁开了。 

 打开503号门...门打开了。 

 打开503号门...门是开着的,无须再开门。 锁上503号门...先关门...门锁上了。

 注意:只能修改每个“//ERROR********found*********”下的那一 行,不要改动程序中的其他内容。

# include<iostream>
using namespace std ;
class Door{
    int num;        // 门号
    bool closed;    // true 表示门关着
    bool locked;    // true 表示门锁着
public:
    // ERROR ********* found *********
    Door(int n) : num(n), closed(true), lock(true) { }
    bool isClosed( ) const { return closed; }       // 门关着时返回 true,否则返回 false
    bool isOpened( ) const { return ! closed; }     // 门开着时返回 true,否则返回 false
    bool isLocked( ) const{ return locked; }        // 门锁着时返回 true,否则返回 false
    bool isUnlocked( ) const { return ! locked; }   // 门未锁时返回 true,否则返回 false
    // ERROR ********* found *********
    void open( ) const {        // 开门
        cout << endl << "打开" << num << "号门...";
        if ( ! closed )
            cout << "门是开着的,无须再开门。";
        else if (locked)
            cout << "门是锁着的,打不开。";
        else {
            closed = false;
            cout << "门打开了。";
        }
    }
    void close ( ) {        // 关门
        cout << endl << "关上" << num << "号门...";
        if(closed)
            cout << "门是关着的,无须再关门。";
        else{
            closed = true;
            cout << "门关上了。";
        }
    }
    void lock( ) const {    // 锁门
        cout << endl << "锁上" << num << "号门...";
        if(locked)
            cout << "门是锁着的,无须再锁门。";
        else{
            // ERROR ********* found *********
            if(closed){
                cout<<"先关门...";
            closed = true;
            }
            locked = true;
            cout << "门锁上了。";
        }
    }
    void unlock( ) {    // 开锁
        cout<< endl << "打开" << num << "号门的锁...";
        if ( ! locked )
            cout << "门没有上锁,无须再开锁。";
        else{
            locked = false;
            cout << "锁开了。";
        }
    }
};
int main( ) {
    Door door(503);
    door.open( );
    door.unlock( );
    door.open( );
    door.open( );
    door.lock( );
    return 0;
}
第1499题

请使用“答题”菜单或使用VC6打开考生文件夹proj1下的工程文件 proj1,程序中位于每个“//ERROR*********found*********”下的语句行 有错误,请加以更正。更正后程序的输出应该是:

 Name:Smith Age:21 ID:99999 CourseNum:12 Record:970 

注意:只能修改每个“//ERROR*********found*********”下的那一 行,不要改动程序中的其他内容。

源程序如下:

# include <iostream>
using namespace std;
class StudentInfo
{
protected:
    // ERROR ********* found *********
    char Name[30] ;
    int Age;
    int ID;
    int CourseNum;
    float Record;
public:
    // ERROR ********* found *********
    void StudentInfo (char * name , int age, int ID , int courseNum , float record );
    ~StudentInfo( ) { delete [ ] Name ; }
    float AverageRecord( ) {
        return Record/CourseNum;
    }
    void show( ) const;
};
StudentInfo :: StudentInfo ( char * name, int age, int ID, int courseNum, float record)
{
    Name = strdup(name);
    Age = age;
    this -> ID = ID;
    CourseNum = courseNum;
    Record = record;
}
// ERROR ********* found *********
void StudentInfo::show( )
{
    cout << "Name: " << Name << " " << "Age: " << Age << " " << " ID: " << ID
         << "    CourseNum: " <<CourseNum<< "    Record: " <<Record<< endl;
}
int main()
{
    StudentInfo st("Smith",21,99999,12,970);
    st.show();
    return 0;
}
第1500题

请使用“答题”菜单或使用VC6菜单打开考生文件夹proj2下的工程 proj2,其中包含抽象类Shape的声明,以及在此基础上派生出的类 Rectangle和Circle的声明,两者都有计算对象面积的函数GetArea()和 计算对象周长的函数GetPerim()。

The area of the Circle is 78.5

The perimeter of the Circle is 31.4

The area of the Rectangle is 24

The perimeter of the Rectangle is 20

注意:只能在横线处填写适当的代码,不要改动程序中的其他内 容,也不能删除或移动“//**********found**********”。 

源程序如下:

# include <iostream>
using namespace std;
class Shape
{
public:
    Shape(){}
    ~Shape(){}
    //**********found**********
    __________ float GetArea() = 0
    //**********found**********
    __________ float GetPerim () = 0
};
class Circle : public Shape
{
public:
    Circle(float radius) : itsRadius(radius){ }
    ~Circle(){ }
    float GetArea() { return 3.14 * itsRadius * itsRadius; }
    float GetPerim () { return 6.28 * itsRadius; }
private:
    float itsRadius;
};
class Rectangle : public Shape
{
public:
    //**********found**********
    Rectangle(float len, float width): __________ {};
    ~Rectangle(){};
    virtual float GetArea() { return itsLength * itsWidth; }
    float GetPerim () { return 2 * itsLength + 2 * itsWidth; }
    virtual float GetLength() { return itsLength; }
    virtual float GetWidth() { return itsWidth; }
private:
    float itsWidth;
    float itsLength;
};
int main() {
    //**********found**********
    __________
    sp = new Circle(5);
    cout << "The area of the Circle is " << sp -> GetArea () << endl;
    cout << "The perimeter of the Circle is " << sp -> GetPerim () << endl;
    delete sp;
    sp = new Rectangle(4, 6);
    cout << "The area of the Rectangle is " << sp -> GetArea() << endl;
    cout << "The perimeter of the Rectangle is " << sp -> GetPerim () << endl;
    delete sp;
    return 0;
}