整理之c++笔试

来源:互联网 发布:网络推广工作基本做法 编辑:程序博客网 时间:2024/05/16 07:21

一、选择题(50分,每小题2分)
下列各题A)、B)、C)、D)四个选项中,只有一个选项是正确的。
(1)下列有关内联函数的叙述中,正确的是 ( D )
A)内联函数在调用时发生控制转移 B)使用内联函数有利于代码重用
C)必须通过关键字inline来定义      D)是否最后内联由编译器决定


(2)下列情况中,哪一种情况不会调用拷贝构造函数 ( B )
A)用派生类的对象去初始化基类对象时
B)将类的一个对象赋值给该类的另一个对象时
C)函数的形参是类的对象,调用函数进行形参和实参结合时。
D)函数的返回值是类的对象,函数执行返回调用者时。


derived d(6,7,8);
base c=d;
(其中base是基类,derived是派生类。)
这里的d 先转换成 base类,然后调用base的拷贝构造函数,因为你的base 没有定义拷贝构造函数,就调用默认的,字节拷贝。

会把派生类对象剪裁成派生类对象。就像将一个int值赋给short,也会发生剪裁一样。

派生类赋给基类是可以滴,反过来则不行。


(3)以下哪一关键字可用于重载函数的区分( C )
A)extern B)static C)const D)virtual


(4)下列有关数组的叙述中,正确的是( B )
A)C++中数组的存储方式为列优先存储
B)数组名可以作为实参赋值给指针类型的形参
C)数组下标索引从1开始,至数组长度n结束
D)数组指针的语法形式为:类型名 *数组名[下标表达式];


(5)下列有关继承和派生的叙述中,正确的是( C )
A)派生类不能访问通过私有继承的基类的保护成员
B)多继承的虚基类不能够实例化
C)如果基类没有默认构造函数,派生类就应当声明带形参的构造函数
D)基类的析构函数和虚函数都不能够被继承,需要在派生类中重新实现

默认构造函数default constructor 又叫缺省构造函数。


(6)实现运行时多态的机制是( A )
A)虚函数 B)重载函数 C)静态函数 D)模版函数


(7)下列字符串中,正确的C++标识符是( D )
A)enum B)2b C)foo-9 D)_32


(8)若有下面的函数调用:
fun(a+b, 3, max(n-1, b));
其中实参的个数是( A )
A)3 B)4 C)5 D)6


(9)以下哪个关键字对应的属性破坏了程序的封装性( B )
A)const B)friend C)public D)protected


(10)以下哪个符号(或组合)是作用域限定符( C )
A)-> B). C):: D)[]


(11)下列关于this指针的说法正确的是( B )
A)this指针存在于每个函数之中
B)在类的非静态函数中this指针指向调用该函数的对象
C)this指针是指向虚函数表的指针
D)this指针是指向类的函数成员的指针


(12)在下列关于C++函数的叙述中,正确的是( C )
A)每个函数至少要有一个参数 B)每个函数都必须返回一个值
C)函数在被调用之前必须先声明 D)函数不能自己调用自己


(13)下列运算符中,不能重载的是 ( C )
A)&& B)!= C). D)->


(14)对于类的常成员函数的描述正确的是( A )
A)常成员函数不修改类的数据成员
B)常成员函数可以对类的数据成员进行修改
C)常成员函数只能由常对象调用
D)常成员函数不能访问类的数据成员


(15)使用如setw()的操作符对数据进行格式输出时,应包含的头文件是( D )
A)iostream B)fstream C)stdio D)iomanip


(16)若有以下类定义
class MyClass {
public:
MyClass() { cout << 1; }
};
则执行语句MyClass a,b[2],*p[2];后,程序的输出结果是( B )
A)11 B)111 C)1111 D)11111

指针数组 p 并没有实例化,所以不会调用构造函数。


(17)下面程序的输出结果是( B )
#include <iostream>
using namespace std;
int i = 0;
int fun(int n)
{
static int a = 2;
a++;
return a+n;
}
void main()
{
int k = 5;
{
int i = 2;
k += fun(i);
}
k += fun(i);
cout << k;
}
A)13 B)14 C)15 D)16


(18)下面程序的输出结果是( A )
#include <iostream >
using namespace std;
void swap1( int &v1, int &v2)
{
int tmp = v2;v2 = v1;v1 = tmp;
}
void swap1( int *v1, int *v2)
{
int tmp= *v2;*v2 = *v1;*v1 = tmp;
}
void main()
{
int i = 10, j = 20; swap1(i,j); swap1(&i,&j);
cout<<i<<”,”<<j<<endl;
}
A)10,20 B)20,10 C)10,10 D)20,20


(19)下面的程序段的运行结果为( D )
char str[] = “job”, *p = str;
cout << *(p+2) << endl;
A)98 B)无输出结果 C)字符’b’的地址 D)字符’b’


(20)下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class A
{
public:
A (int i) { x = i; }
void dispa () { cout << x << “,”; }
private :
int x ;
};
class B : public A
{
public:
B(int i) : A(i+10) { x = i; }
void dispb() { dispa(); cout << x << endl; }
private :
int x ;
};
void main()
{
B b(2);
b.dispb();
}
A)10,2 B)12,10 C)12,2 D)2,2


(21)下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) { cout << i; }
~Base () { }
};
class Base1: virtual public Base
{
public:
Base1(int i, int j=0) : Base(j) { cout << i; }
~Base1() {}
};
class Base2: virtual public Base
{
public:
Base2(int i, int j=0) : Base(j) { cout << i; }
~Base2() {}
};
class Derived : public Base2, public Base1
{
public:
Derived(int a, int b, int c, int d) : mem1(a),mem2(b),Base1(c),Base2(d),Base(a)
{ cout << b; }
private:
Base2 mem2;
Base1 mem1;
};
void main() { Derived objD (1, 2, 3, 4); }
A)134122 B)123412 C)14302012 D)143212


首先搞清楚Base1(int i, int j=0) : Base(j),还有Base2(int i, int j=0) : Base(j) ,还有Derived(int a, int b, int c, int d) :mem1(a), mem2(b), Base1(c), Base2(d), Base(a) 都是成员初始化列表,而且由题中的继承可知,当创建Derived对象时,会调用三次Base,分别是1、0、0。

首先要清楚的是成员列表初始化时,是按照类中定义的顺序来进行初始化的,这是规则。因此题中首先是初始化Base2对象,这首先要先调用基类Base,由Base(a),知输出1,然后是调用Base2(d),输出4,然后是Base1(c),输出3,然后初始化men2(b),因为有默认形参,先调用Base(0)(0值是有Base1(int i, int j=0) : Base(j)得到的,j默认为0),输出0,然后输出2,最后men1(a)的过程也一样。输出0、1,最后输出2

Base(a)->Base2(d)->Base1(c)->men2(b)->men1(a)


(1)、在构造函数Derived中先调用虚基类Base的构造函数,即Base(a),将a的值1传给i,(Base(int i) { cout << i;)输出i值为1。
(2)、然后根据声明顺序(class Derived : public Base2, public Base1 )。Base2在前,接着调用Base2(d),将d的值传给i,(Base2(int i, int j=0) : Base(j) { cout << i; })输出i值为4。由于Base为虚基类,只有最远端派生类构造函数Derived才能调用虚基类的构造函数,该派生类的其他基类对虚基类构造函数的调用被忽略,所以系统不会做 : Base(j)。(因为虚基类无论被从多少路径派生多少次,其派生类对象中永远只有一个内嵌的虚基类对象,这是无法使用派生路径上的各个构造函数,只能由当前派生类的构造函数来负责初始化虚基类对象。)

(3)、再调用Base1(c), 将c的值传给i,(Base1(int i, int j=0) : Base(j) { cout << i; }),输出i值为3,同理系统不做: Base(j)。
(1)、然后根据对象成员声明顺序(Base2 mem2; Base1 mem1;),先做mem2(b), 即调用构造函数Base2(int i, int j=0) : Base(j) { cout << i; },在Base2构造函数中需要先调用Base构造函数,把j=0传值给i,(Base(int i) { cout << i;)输出i值为0,再将b=2的值传给构造函数Base2中的i,输出i值为2。
(1)、接着做mem1(a),同mem2(b),调用Base1构造函数时先调用Base构造函数,输出i值为0,再将,a=1的值传给构造函数Base1中的i,输出i值为1。
(1)、最后做构造函数Derived中的{ cout << b; },输出b值为2


首先,任何虚拟基类的构造函数按照它们被继承的顺序构造;
其次,任何非虚拟基类的构造函数按照它们被继承的顺序构造;
最后,任何成员对象的构造函数按照它们声明的顺序调用。



(22)以下程序对一维坐标点类Point进行运算符重载,输出结果是( A )
#include <iostream>
using namespace std;
class Point {
public:
Point (int val) { x = val; }
Point operator ++() { x++; return *this; }//rong:前置++,若返回值不是引用类型,则结果也对,只是返回值不能作为左值被赋值了。
Point operator ++(int) { Point old = *this; ++(*this); return old; }//rong:void operator ++()是前缀增量运算符的重载操作,而void operator ++(int)是后缀增量运算符的重载操作,这里的int绝不会被使用,该参数其实是虚设的,只是为了区别前缀和后缀的应用。
Point operator +(Point a) { x += a.x; return *this; }
int GetX() const { return x; }
private:
int x;
};
int main()
{
Point a(10);
cout << (++a).GetX();
cout << a++.GetX();
}
A)1111 B)1011 C)1112 D)1010


(23)下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f() { cout << “f0+”; }
void g() { cout << “g0+”; }
};
class Derived : public Base
{
public:
void f() { cout << “f+”; }
void g() { cout << “g+”; }
};
void main() { Derived d; Base *p = &d; p->f(); p->g(); }
A)f+g+ B)f0+g+ C)f+g0+ D)f0+g0+

f()是虚函数,运行的时候根据指针指向的对象动态绑定,指针指向子类对象,所以 f+。
g()不是虚函数,编译的时候静态绑定,父类对象指针,就调用父类对应函数,所以g0+


(24)下面程序的输出结果是( C )
#include <iostream>
using namespace std;
int countp=0;
class Point
{
int X,Y;
public:
Point(int x=0,int y=0) { X=x; Y=y;}
Point(Point &p){X=p.X;Y=p.Y;countp++;}
friend Point myfun(Point p1 ,Point p2 ,const Point &p3);
};

Point myfun(Point p1,Point p2,const Point &p3) //rong:调拷贝构造函数
{
Point tmp(p1.X+p2.X+p3.X,p1.Y+p2.Y+p3.Y);
return tmp;
}
void main()
{
Point pp0,pp1(1,2),pp2(1);
myfun(pp0,pp1,pp2);
std::cout<<countp<<endl;
}
A)0 B)4 C)3 D)6


(25)下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class Sample
{
    friend long fun(Sample s)
    {
    if (s.x < 2) return 1;
    return s.x *fun(Sample(s.x-1));
    }

   public:
    Sample (long a) { x = a; }

   private:
    long x;
};

void main()
{
int sum = 0;
for (int i=0; i<6; i++)
{
sum += fun(Sample(i));
}
cout << sum;
}
A)120 B)16 C)154 D)34

友元不是成员函数,但是它可以访问类中的私有成员。友元的作用在于提高程序的运行效率(即减少了类型检查和安全性检查等都需要的时间开销),但是,它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。



二、填空题(20分,每空2分)
(1)以下程序是用来计算小于16的整数的阶乘,请补充完整。
#include<iostream>
using namespace std;
const int ArSize =16;
void main()
{
double fac[ArSize];
fac[1] =fac[0] =1.0;
for(int i=2;i<ArSize;i++)
fac = i*fac[i-1] ;
}


(2)下面是个Cat类的声明与使用,请补充完整。
#include <iostream>
using namespace std;
class Cat
{
static int count;
public:
Cat() { count++; cout << “Now cat number is” <<count << endl; }
~Cat() { count–; cout << ” Now cat number is ” << count << endl; }
};
int Cat::count =0;
int main()
{
Cat a, b, c;
return 0;
}


(3)将下面的MyPoint类定义补充完整,使得程序的输出结果是(10,10)(5,5)
#include <iostream>
class MyPoint
{
public:
MyPoint(int xx=5, int yy=5)
{ X = xx;
Y = yy;
std::cout<<”(“<<X<<”,”<<Y<<”) “;}
private:
int X, Y;
} ;
void main()
{
MyPoint a(10,10),b;
}


(4)已知文件之间具有以下的包含关系(用#include指令):point.cpp 包含 point.h,point.cpp 包含 line.h,line.h包含 point.h。那么如下的point.h文件缺少什么语句,请补充完整。
// Point类的声明,point.h。
#ifndef _POINT_H_
#define _POINT_H_

class Point
{

} ;
#endif


(5)下列函数的功能是判断字符串str是否对称,对称则返回true,否则返回false。请在横线处填上适当内容,实现该函数。
bool fun (char *str)
{
int i=0, j=0;
while (str[j]) j++;
for(j–; i<j && str==str[j]; i++, j–);
return i>=j ;
}


(6)请将下列程序补充完整,使得输出结果为“Destructor Derived Destructor Base”。
#include <iostream>
using namespace std;
class Base
{
public:
virtual ~Base () { cout << “Destructor Base”<< endl; }
};
class Derived : public Base
{
public:
~Derived(){ cout << “Destructor Derived” << endl; }
};
void main ()
{
Base *pBase = new Derived;
delete pBase ;
}


(7)如有下列程序:
#include <iostream>
using namespace std;
class Demo
{
public:
Demo(){cout<<”default constructor\n”;}
Demo(const Demo &x){cout<<”copy constructor\n”;}
};
Demo userCode(Demo b){Demo c(b);return c;}
void main()
{
Demo a,d;
cout<<”calling userCode()\n”;
d = userCode(a); //rong:调用缺省的operator=操作符,浅拷贝。
}
执行上面的程序的过程中,构造函数Demo()和Demo(const Demo &x)被调用的次数分别是 2 和3 次。


三、程序题(30分,每题10分)
(1)程序改错
每个注释“// ERROR”所在的一行语句存在错误。请改正这些错误,使程序的输出结果为:
00:00:00
01:37:19
注意:只需修改注释“// ERROR”所在的那一行语句,不要改动程序中的其他内容。
#include<iostream>
#include<iomanip>
using namespace std;
class StopWatch //”秒表”类
{
int hours; //小时
int minutes; //分钟
int seconds; //秒
public:
StopWatch():hours(0), minutes (0), seconds(0){}
void reset(){hours=minutes=seconds=0;}
//前进1秒
StopWatch& operator++() // ERROR ①
{
if(seconds==60) // ERROR ②
{
seconds=0;
if(++minutes==60)
{
minutes=0;
++hours;
}
}
return *this;
}

friend void show(StopWatch);
};
void show(StopWatch watch)
{
cout<<setfill(‘*’); // ERROR ③
cout<<setw(2)<<watch.hours<<’:’
<<setw(2)<<watch.minutes<<’:’
<<setw(2)<<watch.seconds<<endl;
}
int main()
{
StopWatch sw;
show(sw);
for(int i=0;i<5839;i++)
sw++;
show(sw);
return 0;
}
解答:
①: StopWatch& operator++(int) (4分)
②: if(++seconds==60) (3分)
③: cout<<setfill(’0′); (3分)


(2)语句填空
下面的程序设计了一个宠物类,请你在空行上填入合适的语句,使程序完整。(每空2分)
#include <iostream>
enum Pets_type{dog,cat,bird,fish};
class Pets
{
private:
char *name;
Pets_type type;
public:
Pets(const char *n=”sonny”,int type1 = 0);
Pets(const Pets &s);
Pets& operator=(const Pets &s);
~Pets();
};
Pets::Pets(const char *n,int type1) //构造函数
{
name = new char[strlen(n) + 1];
strcpy(name,n);
type = Pets_type(type1);
}
Pets::Pets(const Pets &s) //拷贝构造函数
{
name = new char[strlen(s.name) + 1] ;
strcpy(name,s.name);
type = s.type;
}
Pets::~Pets() //析构函数
{
delete[] name ;
}
Pets& Pets::operator =(const Pets &s)
{
if (this==&s) //确保不要向自身赋值
return *this;
delete [] name;
name = new char[strlen(s.name) + 1];
strcpy(name,s.name);
type = s.type;
return *this ;
}
void main()
{
Pets mypet1,mypet2(“John”,1),hispet(“Danny”,2);
Pets youpet(hispet);
mypet1 = youpet;}


3) 编写程序段
补充编制下列程序,其功能是从键盘读取任意长度的文本内容,将文本存放到Doc类的对象myDoc中。然后在显示器输出。
请在//******* begin ******和//******* end *******之间补充程序。
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
class Doc
{
private:
static const int MaxLength; // 可能的最大文本字符串长度
char *str; // 文本字符数组首地址指针
int length; // 文本字符串长度
public:
Doc(const char *s = “”); // 构造函数
~Doc(); // 析构函数
// 重载istream的提取运算符
friend istream& operator>>(istream& is, Doc& doc);
// 重载ostream的插入运算符
friend ostream& operator<<(ostream& os, Doc& doc);
};
const int Doc::MaxLength=256; // 可能的最大文本字符串长度
//重载提取运算符,从输入流is提取字符串,存入参数doc中
istream& operator>>(istream& is, Doc& doc)
{
//提示:使用is.getline函数可以从is流提取字符串,包括空格。
//********************** begin *************************
char buffer[Doc::MaxLength];
is.getline(buffer, Doc::MaxLength);
int inLen = (int)strlen(buffer);
if (inLen > doc.length) {
delete [] doc.str;
doc.str = new char[inLen+1];
}
strcpy(doc.str, buffer);
doc.length = inLen;
return is;
//******************** end *****************************
}
ostream& operator<<(ostream& os, Doc& doc)
{
os << doc.str;
return os;
}
Doc::Doc(const char *s) : length((s!=NULL) ? (int)strlen(s) : 0)
{
str = new char[length + 1];
if (s != NULL)
strcpy(str, s);
else
str[0] = ‘\0′;
}
Doc::~Doc()
{
delete [] str;
}
void main()
{
Doc myDoc(“Initial String”);
cin >> myDoc;
cout<< myDoc;

0 0
原创粉丝点击