单选

一.单项选择题(每小题 2 分,共 20 分)

  1. 在 C++ 中,数据与操作的封装是借助于 B 达到的。

    1. 指针 (B) 类 (C) 数组 (D) 函数
  2. 下面叙述不正确的是 C 。

    1. 基类的保护成员在派生类中仍然是保护的

    2. 基类的保护成员在公有派生类中仍然是保护的

    3. 基类的保护成员在私有派生类中仍然是保护的

    4. 对基类成员的访问必须是无二义性的

  3. 以下基类中的成员函数中表示纯虚函数的是 B 。

    1. virtual void vf(int); (B) virtual void vf(int)=0;

(C) virtual void vf(int=0); (D) virtual void vf(int){ };

  1. 下面对类的析构函数的定义是 D 。

    1. X::~ X( 参数 ); (B) void X::~ X( 参数 );

(C) void X::~ X(); (D) X::~ X();

  1. 下列关于运算符重载的描述中, D 是正确的。

    1. 运算符重载可以改变操作数的个数。

    2. 运算符重载可以改变优先级。

    3. 运算符重载可以改变结合性。

    4. 运算符重载不可以改变原语法规则。

  2. 编译时的多态性通过使用 B 实现。

    1. 构造函数 (B) 虚函数 (C) 重载函数 (D) 析构函数
  3. 下面对类的构造函数的定义是 B 。

    1. void X::X( 参数 ) (B) X::X( 参数 )

(C) int X::X( 参数 ) (D) float X::X( 参数 )

  1. 已知类 A 中的一个成员函数说明如下: void Set( A & a ) 其中, A & a 的含义是

C 。

  1. 指向 A 类的指针为 a

  2. 将 a 的地址值赋给变量 Set

  3. a 是类 A 的引用,用来做函数 Set() 的形参

  4. 变量 A 和 a 是函数 Set() 的两个形参

突然发现混淆了引用和指针以及*和&的各种操作

引用是存储空间的别名,比如大黄和旺财是同一条狗的名字一样

指针是存储空间的序列,比如狗子的狗牌上的信息,可以帮助我们找到狗子

声明引用要用dog&,指针用dog*

声明只能在初始化时选定代表的对象,后续无法更改。指针则更加灵活。

指针有*和&两种操作,对&,是地址操作符,是对存储空间的名字进行操作以得到地址空间序列的方法。例如,通过狗子的名字拿到狗牌。

对*,是解引用操作符,通过地址空间的序列得到这个空间的名字。即通过狗牌得到狗子的名字。

  1. 下列函数中, D 是对文件进行写操作的。

    1. get() (B) read() (C) seekg() (D) put()
  2. 写一个 C++ 文件,要创建一个 B 流对象。

    1. ifstream (B) ofstream (C) cin (D) cout

这选择题看完,感觉八股文还是得看看,有些时候不上机看一眼确实会感到怀疑。

二.程序填空题( 30 分)

  1. ```c++

    include\

    class A
    {

    (1)--------------public: 
    void f(int i)
    {cout\<\<i\<\<'\t'; } 
    void g()
    {cout\<\<"g\n"; }
    

    };

    class B:private A

    {

    public:
    

    void h()
    {cout\<\<”h\n”;}
    (2)—————————-A::f;
    };
    void main()
    { B d;
    d.f(6);
    d.h();
    }


    输出: 6 h

    2. ```c++
    #include \<iostream.h\>
    class p_class
    {
    int num ;
    public :
    void set_num ( int val ) { num = val ; }
    void show_num ( ) ;
    } ;
    void p_class :: show_num ( )
    { cout \<\< num \<\< '\t';}
    void main ( )
    {
    p_class ob\[3\] , * p ;
    for ( int i = 0; i\<3 ; i++ )
    ob[i].set_num((i+1)*10);

    p=&ob\[1\]; _____(3) ; p -\> show_num ( ) ;

    p=&ob\[2\];______(4) ; p -\> show_num ( ) ;

    p=&ob\[0\];______(5) ; p -\> show_num ( ) ;

    }

输出: 20 30 10

  1. ```c++

    include\

    class Tdate
    {
    int month ; int day ; int year ;
    int IsLeapYear()
    { return(year%4==0&&year%100!=0)||(year%400==0);}
    
    (6)————————————————-public:
    void Set (int m, int d, int y)
    { month=m; day=d; year=y; }
    (7) ;————————void Print();
    } ;
    (8) Print()——————————————-void Tdate::Print()
    {
    cout \<\< month \<\<"/"\<\< day \<\<"/"\<\< year\<\<endl ; 
    if(IsLeapYear())cout\<\<"This year Is leapyear.\n" ; 
    else cout\<\<"This year Is not leapyear.\n";
    
    }
    void main()
    {
    Tdate a ;
    a.Set ( 2, 4, 2000 ) ;
    a.Print() ;
    
    }



    4. ```c++
    #include\<iostream.h\>
    class Time
    {
    public:
    Time(int h, int m) {hours=h; minutes=m;}
    Time12();
    \(10\)
    Time24(Time time); private: int hours, minutes;
    };
    \(11\) Time12()--------------------------------void

    {
    if(hours\>12)
    { cout\<\<hours-12\<\<":"\<\<minutes\<\<"PM\n" ; }
    else cout\<\<hours\<\<":"\<\<minutes\<\<"AM\n" ;

    }

    void Time24(Time time)

    { cout\<\<hours-12\<\<":"\<\<minutes;} -----------------------------(12)
    void main()

    { Time T1(20,30), T2(10,45);

    T1.Time12(); Time24(T1); T2.Time12(); Time24(T2);

    }

输出: 8:30PM

20:30

10:45AM

10:45

  1. ```c++

    include\

    class A
    { public:
    A(const char *s)
    {cout\<\<s\<\<endl;}
    ~A(){}
    };
    class B: (13) A————————————-virtual public
    {

    public:
    

    B(const char *s1, const char *s2):A(s1) {cout\<\<s2\<\<endl;}
    };
    class C: (14)———————————————-virtual public A
    {

    public:
    

    C(const char *s1, const char *s2):A(s1) { cout\<\<s2\<\<endl;}
    };

    class D:public B, public C
    {

    public:
    

    D(const char *s1,const char *s2,const char *s3,const char *s4);

    (15)———————————————:A(s1),B(s1,s2),C(s1,s3)

    { cout\<\<s4\<\<endl;}

    };

    void main()

    {

    D *ptr=new D("class A","class B","class C","class D"); 
    delete ptr;
    

    }




    输出: class A

    class B class C class D

    三.读程序写运行结果 (24 分)

    1.

    ```c++
    #include \<iostream.h\>
    class desk
    {
    public:
    desk(){ length=3; width=4; high=5; }

    desk(int l,int w,int h)
    { length=l;width=w;high=h;}
    int volume(){ return length\*width\*high;}
    int price(){ return volume()\*2;}
    private:
    int length,width,high;

    };
    class luxury_desk:public desk
    {
    public:
    luxury_desk() { m_price=2.5;}
    float price() { return volume()*m_price;}
    private:
    float m_price;

    };
    void main()
    { desk da,da1(1,2,3);
    luxury_desk db;
    cout\<\<da.volume()\<\<" "\<\<da.price()\<\<" ";
    cout\<\<db.volume()\<\<" "\<\<db.price()\<\<" ";
    cout\<\<da1.volume()\<\<" "\<\<da1.price()\<\<" ";

    }
60 120
60 150
6 12

2.

#include \<iostream.h\> 
class B
{
int b;
public:
B(int i) {b=i;}
virtual void virfun() {cout\<\< "B::b: "\<\<b\<\<" , ";}
};

class D: public B
{
public:
D(int i,int j): B(i) {d=j;}
void virfun() {B::virfun(); cout\<\<"D::d: "\<\<d\<\<endl;}
private: int d;
};
void fun(B *objp) { objp-\>virfun(); }
void main()
{
D \*pd=new D(3,5) ;
fun(pd);
}

B::b 3
D::d 5

3.

#include \<iostream.h\>
enum BoolConst { False=0 , True=1 }; //定义枚举类型class Boolean
{
public:
Boolean(BoolConst x = False)
{ logic = x; }
void print() const
{ logic? cout\<\<" TRUE " : cout\<\<" FALSE "; }
friend Boolean operator +(const Boolean & obj1, const Boolean & obj2);
friend Boolean operator \*(const Boolean & obj1, const Boolean & obj2);
protected: BoolConst logic;
};
Boolean operator + ( const Boolean & obj1, const Boolean & obj2 )
{
return (obj1.logic \|\| obj2.logic) ? Boolean(True) : Boolean(False);
}
Boolean operator \* ( const Boolean & obj1, const Boolean & obj2 )

{
return (obj1.logic && obj2.logic) ? Boolean(True) : Boolean(False);
}
void main()

{
Boolean a(False), b(True), c, d ;
c = a \* b; d = a + b;
a.print();
b.print();
c.print();
d.print();
cout\<\<endl;

}
FTFT

4.写出 data.txt 中的结果和屏幕显示的结果。

#include \<fstream.h\>
int filerror(const fstream &, char \*); void main()
{
int a=10; double x=50.5;
char str\[10\], fname\[20\]= "d:\\data.txt";
fstream iofile(fname,ios::out);
if(!iofile) return;
iofile\<\<"string\n"\<\<a+10\<\<" "\<\<x\<\<endl;
iofile.close();
iofile.open(fname, ios::in);
if(!iofile) return;
iofile\>\>str\>\>a\>\>x;
cout\<\<"x="\<\<x\<\<", a= "\<\<a\<\<"\nstring="\<\<str\<\<"!"\<\<endl;
}
string 20 50.5
x=50.5,a=20.string!

四.改错题。找出以下程序的错误,并注释错误原因。(10 分)

1.

class Time
{
public:
Time(int day){ iMonth=5; iDay=day; iYear=2000; }
Timt(int month, int day=7 )
{ iMonth=month; iDay=day; iYear=2000; }
Private:
int iMonth; int iDay; int iYear;
};
void main()
{
Time ----------input-----------------(9,1,2005);
----------------input.iMonth=1;---------------
}

2.

 Template\<class T\>

Type func ( T a , T b )

{ return ( a \> b ? a : b ); } \#include\<iostream.h\> void main()

{ int a=3 ; double x=3.5 ;
--------------------cout\<\<func(a,x)\<\<endl;-------------------------

}

五.完成程序。(共 16 分)

  1. 一个类的头文件如下所示:
test.h 
#include\<iostream.h\> class test
{ private: int num;
public:
test(int); void show();
};
test::test(int n)
{ num = n; }
void test::show()
{ cout\<\<num\<\<endl;}

编写一个主程序,产生对象 TTT,令 TTT 数据成员的值为 5,并使用 show()函数输出这个对象的数据成员。

  1. 根据下面的主程序,补上所缺的类说明文件的最小形式。

#include\ #include ”base.h” void main()

{ base stry;

stry.init(6); //对数据成员赋值cout\<\<stry.Getnum(); //输出数据成员的值

}

没写