南京邮电大学通达学院 软嵌 C++复习题目

如果发现有错误请私信QQ:3146463054  会进行更改


一、选择题

1、以下设置默认值的函数原型声明中,错误的是__B__。

A.int add(int X=3,int y=4,int z=5);

B.int add(int x=,int y=4,int z);

C.int add(int x=,int y=4,int z=5);

D.int add(int x=,int y,int z=5);

2、下列错误的重载函数是__B__。

A.int print(int x); 和 void print(float x);

B.int disp(int x); 和 char * disp(int y);

C.int show(int x,char *s);和 int show (char *s,intx);

D.int view(int x,int y);和 int view(int x);

3、在动态空间管理方面,C++语言运算符_new_取代了C语言中的malloc申请动态内存空间,运算符_delete_取代了C语言中的free释放动态内存空间。

4、如果class类中的所有成员在定义时都没有使用关键字public、private、protected,则所有成员默认的访问属性为_B_。

A、public        B、private        C、static        D、protected

5、下列关于常对象的说法,正确的是_B_。

A、常对象的数据成员均为常数据成员

B、常对象只能调用常成员函数

C、常对象可以调用所有的成员函数

D、常对象所属的类中只能定义常成员函数

6、下面关于类的友元的说法,错误的是_D_。

A、友元函数可以访问该类的私有数据成员

B、友元的声明必须放在类的内部

C、友元成员可以是另一类的某个成员函数

D、若X类是Y类的友元,Y类就是X类的友元

7、下面有关基类与公有派生类的赋值兼容原则的说法中,正确的是_C_。

A、共有派生类对象不能赋给基类对象

B、基类对象能赋给公有派生类的引用

C、基类对象不能赋给公有派生类对象

D、公有派生类对象地址不能赋给基类指针变量

8、下列关于纯虚函数和抽象类的描述中,错误的是_C_。

A、纯虚函数是一种特殊的虚函数,它没有具体的实现代码

B、抽象类是指具有纯虚函数的类

C、一个基类中声明有纯虚函数,该基类的派生类一定不再是抽象类

D、抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出

9、假设定义如下函数模板。

template<class T>
T max(Tx,Ty)
{
        returb(x>y)?x:y;
}

并有定义 “int i;char c;” ,则错误的调用语句是_D_。

A、max(i,i);        B、max(c,c);        C、max((int)c,i);        D、max(i,c);

10、假设定义如下函数模板。

template < class T1, class T2>
void sum(T1 x,T2 y)
{
    cout << sizeof(x+y);
}

函数调用"sum('1',99.0)"的输出结果是_C_。 

A、100        B、1        C、8        D、4

11、在C++语言程序中进行文件操作时应包含标准名字空间std中的_A_文件。

A、fstream        B、iomanip        C、string        D、iostream

12、当用ifstream流类对象打开文件时,其默认打开方式是_B_。

A、ios::app        B、ios::in        C、ios::out        D、ios::ate

13、设有带默认值的函数原型声明“void f(int x,int y=4,int z=5);”,以下几种调用中错误的是_C_。

A、f(10,20,30)        B、f(10,20)        C、f()        D、f(10)

14、设有语句“void f(int a[10];int &x);int y[10],*py=y,n”,则对函数f()的正确调用语句是_B_。

A、f(py[10],n);        B、f(py,n);        C、f(*py,&n);        D、f(py,&n);

15、C++引入_D_释放所申请的动态内存空间。

A、malloc()        B、free()        C、new        D、delete

16、下面正确的引用定义是_D_。

A. int &A[4];        B. int &*p;        C. int &&q;        D. int i,&p=i;

17、以下选项中不属于类的访问权限的是_C_。

A. public        B.private        C. static        D.protected

18、可以用p.a的形式访问对象p的成员a,其中a是_A_.

A、公有数据成员        B、私有数据成员

C、公有成员函数        D、私有成员函数

19、在下面选项中,类的复制构造函数的正确声明格式是_B_。

A. A::A(&)        B. A::A(const A&)        C. A::A(A)        D. void A::A(A&a)

20、_D_不是构造函数特征。

A、构造函数的函数名与类名相同        B、构造函数可以重载

C、构造函数可以设置缺省参数           D、构造函数必须指定返回值类型

21、_D_是析构函数的特征。

A、析构函数可以有一个或多个参数        B、析构函数名与类名完全相同

C、析构函数的实现只能在类体内           D、一个类中只能定义一个析构函数

22、已知print()函数是一个类的常成员函数,无返回值,下列正确的原型声明为_A_。

A. viod print()const        B.const void print()

C.void const print()        D.void print(cionst)

23、下列关于常成员函数的描述中,正确的是_B_。

A、常成员函数只能访问类的常成员变量

B、常成员函数对数据成员只访问不修改

C、常成员函数只能被常对象所调用

D、常成员函数可以调用类的任何成员函数

24、在多重继承中,如果多个基类都有非私类有属性的同名成员,在派生类引用该同名成员时为了消除二义性,通常可以在该同名成员前增加_D_加以区分。

A、基类名,        B、基类名;        C、基类名.        D、基类名::

25、设类B是基类A的派生类,并有“A aa,*pa=&aa;        B bb,*pb=&bb;”语句,则正确的语句是_C_。

A. pb = pa;        B.bb = aa;        C. aa = bb;        D. *pb = *pa;

26、_C_是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生类的必须定义自己的现实版本,

A、虚析构函数        B、虚构造函数        C、纯虚数函数        D、静态成员函数

27、如果一个类至少有一个纯虚数,那么就称该类为_A_。

A、抽象类        B、虚基类        C、派生类        D、以上都不对

28、假定要对类AB定义加号运算符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为_B_。

A. Ab   operatpr+(AB   & a,AB   & b);

B. AB   operator+(AB   &a);

C. operator+(AB   &a);

D. AB   operator+();

29、假定要对类AB定义加号运算符重载为友元函数,实现两个AB类对象的加法,并返回相加结果,则该函数的声明语句为_A_。

A. friend AB   operator+(const AB   & a,const AB   & b);

B. friend operator+(AB   a);

C. friend AB   opreator+(AB   &a);

D. friend AB   &operator+();

二、读程序,写结果

1、

#include <iostream>
using namespace std;
int i=0;
int main ()
{
    int i=5;
    {
        int i=7;
        cout << "::i=" << ::i << endl ;
        cout << "i=” << i << endl;
        ::i = 1;
        cout << "::i=" << ::i << endl ;
    }
    cout<< "i=" << i << endl;
    cout<< "::i=" << ::i << endl;
    i += ::i;
    ::i = 100;
    cout<< "i=" << i <<endl;
    cout << "::i=" << ::i << endl;
    return 0 ;
}

输出结果

::i=0
i=7
::i=1
i= 5
::i=1
i=6
::i=100

2、

#include<iostream>
using namespace std;
class base
{
public:
    base ()
    {
        cout<<“构造base对象"<<endli
    }
    virtual void f()
    {
    cout << "调用base::f()"<< endl;
    }
};
class derived: public base
public:
    derived ()
    {
    cout << "构造derived对象" << end1;
    }
    void f()
    {
    cout << "调用derived::f()" << end1;
    }
};
int main()
{    base *p ;
    derived d ;
    d.f ();
    P=&d;
    P -> f() ;
    return 0;
}

输出结果

构造base对象
构造derived对象
调用derived::f()
调用derived::f()

3、

#include <iostream>
using namespace std;
intmain ( )
{
    intn=20,m=30 ;
    int &X =n ;
    x = m ;
    cout<< n <<" " ;
    x = x + 10 ;
    cout << n <<" "<< m <<" " ;
    n += m ;
    cout << x << endl ;
    return 0;
}

输出结果:

30 40 30 70

4、

#includeくiostream>
using namespace std;
void swap (int m , int &n)
{
    int temp ;
    temp = m ;
    m = n ;
    n = temp ;
}
int main (
{
    int x = 10,y = 20 ;
    cout << "x=" << x << ",y=" << y << endl;
    swap ( X,y);
    cout << "x=" << x << ",y=" << y << endl ;
    return 0 ;
}

输出结果:

x=10,y=20
x=10,y=10

5、

#include<iostream>
using namespace std;
class STAT
{
    int y
public:
    STAT(int r = 0)
    {
        y=r;
        cout << "constructing" << endl ;
    }
    STAT ( const STAT&A )
    {
        y=A.y;
        cout << " copy constructing" << endl ;
    }
    ~STAT ( )
    {
        Cout << "destructing" << endl ;
    }    
    int get_y()
    {
        return y ;
    }
};
int sqr (STAT O)
{
return O.get_y()*O.get_ y() ;
}
int main ( )
{
    STATob(5);
    cout << ob.get_y() << endl ;
    cout << sqr (ob) << endl ;
    return 0;
}

输出结果

constructing
5
copy constructing
destructing
25
destructing

 6、

#include <iostream>
usingnamespace std;
classA
{
public:
    A(int ia,int ib):pi( 3.14)
    {
        ra = ia;
        rb = ib;
    }
    void print ()
    {
        cout<< ra * rb << endl;
    }
    void print () cons t
    {
        cout<<pira* rb << endl;
    }
private :
    int ra,rb;
    const double pi;
};

int main ( )
{
    A obj1(2, 3) ;
    const A obj2 (4, 5) ;
    obj1.print () ;
    obj2.print() ;
    return 0;
}

输出结果

6
62.8

7、

#include < iostream>
using namespacestd;
class A
public:
    int n;
    A(int i)
    {
        n=i;
        cout << "ninAis:" << n <<endl;
    }
};
class B: virtual public A
{
public :
B(int i): A(i)
    {
        n = i * i;
        cout << "n in B is:" << n << endl ;
    }
};
class c:virtual public A
{
public :
    c(int i = 0): A(i)
    {
        n = 2 * i;
        cout << "n in C is:" << n << endl;
    }
};
class D: public B, public c
{
public:
    D(int x): с(x), B(x), A(x)
    {
        n = 3 * х;
        cout << "n in D is:" << n << endl;
    }
};
int main ()
{

    D d(20) ;
    cout << d.A::n << '\t' << d.B::n <<'\t' << d.C::n<< '\t' << d.n << endl;
    return 0;
}

输出结果

n in A is:20
n in B is:400
n in C is:40
n in D is:60
60        60         60        60

8、

#include <iostream>
using namespace std;
class A
{
public :
    int x;
    A( int a= 0)
    {
        x= a;
    }
};
class B: public A
{
public :
    B( int b=0) :A(b)
    {
    }
    void show ( )
    {
        cout << X << endl;
    }
};
class c: virtual public A
{
public:
    C(int c=0 ): A(c)
    {
    }
    void show ()
    {
    cout<< x<< endl ;
    }
};
class D: public B, public с
{
public:
    D(int d =0) : B(d+1) , C(d+2)
    {
    }
};
int main ()
{
    Dobj(5);
    obj.B::show() ;
    obj.c::show() ;
    return 0;
}

输出结果

6
0

三、填程序

一、完善Student类的定义,使程序正确执行,输出结果如下

age:18,name:zhangzhan
age:19,name:lili
age:17,name:wangwan

程序代码如下

#include<iostream>
#include<string>
using namespace std;
class Student
{
    int age;
    string name;
public:
    Student ()
    {
        age = 0;
        nName=" ";
    }
    Student ( int m,string n )
    {
        age = m;
        _1_
    }
    void SetName (int a, string n )
    {
        age =a ;
        name = n
    }
    int Getage ()
    {
        return age;
    }
    _2_Getname ()
    {
        return name;
    }
};
int main ()
{
    Student S[3], *p=S;     //如果类定义中没有无参构造函数,则此语句出错
    S[0]. SetName (18,"zhangzhan") ;
    S[1] . SetName ( ]"lili") ;
    S[2] . SetName ( 17"wangwan") ;
    for( ; _3_ ;p++)
    {
        cout << "age:" <<_4_<<",";
        cout << "name:" <<_5_<< end1 ;
    }
    return 0;
}


答案:

  1. name=n
  2. string
  3. p<S+3
  4. p->Getage()
  5. p->Getname()

二、

#include<iostream>
using namespace std;
class Point
{
private:
    double X,Y;
public:
    Point(double ,double );
    Point(Point &);
};
Point::Point(    ①    )
{
    X=a;
    Y=b;
}
Point::Point(    ②    )
{
    X=      ③     ; 
    Y=p.Y;
}

答案:

  1. double a,double b
  2. point &p
  3. P.X 

四、编程题

 设计并实现一个时间类time,用来表示24小时制的时间。它含有如下成员。

(1)私有int数据成员:hour、minute和second,分别表示时、分、秒。

(2)构造函数:对hour、minute和second完成初始化,默认都为0。

(3)display()函数:该函数无返回值,其功能是输出对象表示的时间,即数据成员hour、minute和second的值。

(4)用成员函数重载前置“++”运算符,实现时间增加一秒的功能;友元函数重载等于运算符“==”,判断两个时间是否相等。

main()函数代码如下:

int main()
{
    time t1( 18, 59, 59 ),t2(19,0,0);
    t1.display();
    t2.display();
    ++t1;
    t1,display();
    if (t1==t2)
        cout<<"the same time.\n";
    else
        cout<<"the different time.\n";
    return 0;
}

全部代码:

#include <iostream>
using namespace std;
class time
{
private:	
    int hour, minute,second;
public:
    time(int h=0, int m=0, int s=0) ;
    void  display ( )
    {	
	cout<<hour<<":"<<minute<<":"<<second<<endl;
    }
    time operator++( );
    friend bool operator ==(const time &t1,const time &t2);
};

time::time(int h, int m, int s):hour(h),minute(m),second(s)
{	}
time time::operator++( )
{	
    second++;
    if ( second == 60 )
    {	
second = 0;
	    minute++;
	    if ( minute == 60 )
	    {   
minute = 0;
	        hour++;
	        if ( hour == 24 )
		    hour = 0;
	     }
     }
     return *this;
}
bool operator ==(const time &t1,const time &t2)
{
     return t1.hour==t2.hour && t1.minute == t2.minute && 
t1.second == t2.second ;
}
int main( )
{
     time t1( 18, 59, 59 ),t2(19,0,0);
     t1.display( );
     t2.display( );
     ++t1;
     t1.display( );
     if (t1==t2)
	     cout<<"the same time.\n";
     else
	     cout<<"the different time.\n";	
     return 0; 
}

五、编写程序

大学里有这样几类人员:学生、教师、职员等。

Person: 抽象类,有数据成员name,和纯虚函数Print;

Staff:派生类,新增工作部门;

Student:派生类,新增专业;

Teacher:为Staff派生类,新增教授的课程; 

给出这几类人员的类描述,在基类中定义一个纯虚函数用于显示人员信息。   

main()函数代码如下:

int main()
{
    Student stu("Mike","software Engineering");
    Staff sta("Jason","Management");
    Teacher t("Tim","Computer","C++");
    stu.Print();
    sta.Print();
    t.Print();
    return 0;
}

 全部代码:

#include<iostream>
using namespace std;
class Person
{
public:
    Person(const string n);
    virtual void Print()=0;
protected:
    string name;
};
class Student: virtual public Person
{
public:
    Student(const string n, const string m);
    void Print();
protected:
    string major;
};
class Staff: virtual public Person
{
public:
    Staff(const string n,const string d);
    void Print() ;
protected:
    string dept;
};
class Teacher:public Staff
{
public:
    Teacher(const string n,const string d,const string l);
    void Print();
protected:
    string lesson;
}

Person::Person(const string n)
{
        name = n;
}
    Student::Student(const string n, const string m): Person(n)
{
        major = m;
}
void Student::Print()
{
        cout<<"Name:"<<name<<endl;

        cout<<"Major:"<<major<<endl;
}
Staff::Staff(const string n,const string d):Person(n)
{
   dept=d;
}
void Staff::Print()
{
    cout<<"Name:"<<name<<endl;
    cout<<"Department:"<<dept<<endl;
}
Teacher::Teacher(const string n,const string d,const string l)
:Person(n),Staff(n,d)
{
    lesson=l;
}
void Teacher::Print()
{
    Staff::Print();
    cout<<"Lesson:"<<lesson<<endl;
}


int main()
{
    Student stu("Mike","software Engineering");
    Staff sta("Jason","Management");
    Teacher t("Tim","Computer","C++");
    stu.Print();
    sta.Print();
    t.Print();
    return 0;
}

祝愿考的这个博客里都有 


  • 11
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值