一、问答题
1.请说明类的纯虚函数、虚函数、静态成员函数、普通成员函数的区别。
2.什么情况下,类的析构函数应该声明为虚函数?为什么?
3.对于下面的代码:
classmyString;
myString*pStringArray=newmyString[13];
以下两种delete有什么区别?
deletepStringArray;
delete[]pStringArray;
二、说明题
下列题目,请写出输出结果,并要求说明原因。
4.下面的函数调用输出什么?
voidTest()
{
char*p="Test";
cout<<&p<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<p[0]<<endl;
void*q="Test";
cout<<&q<<endl;
cout<<q<<endl
}
5.有如下的类:
classCBase
{
public:
virtualvoidTest()const{cout<<"OutputfromCBase!"<<endl;};
};
classCDerived:publicCBase
{
public:
virtualvoidTest()const{cout<<"OutputfromCDerived!"<<endl;};
};
下面是两个函数:
voidTest1(CBasetest)
{
test.Test();
}
voidTest2(constCBase&test)
{
test.Test();
}
请问调如下的函数输出什么?
voidTest()
{
CDerivedoTest;
Test1(oTest);
Test2(oTest);
}
6.有如下的类:
classB
{
public:
B(){cout<<"OutputfromtheconstructorofclassB!"<<endl;}
~B(){cout<<"OutputfromthedestructorofclassB!"<<endl;}
};
classD1:publicB
{
public:
D1(intn){cout<<"Theintegervalueis:"<<n<<endl;};
~D1(){cout<<"OutputfromthedestructorofclassD1!"<<endl;};
};
classD2:publicB
{
public:
D2(intn){cout<<"Theintegervalueis:"<<n<<endl;};
~D2(){cout<<"OutputfromthedestructorofclassD2!"<<endl;};
};
classCTest
{
public:
CTest():d2(2),d1(1){};
~CTest(){};
private:
D1d1;
D2d2;
};
请问调如下的函数输出什么?
7.有如下的类:
classCBase
{
public:
virtualvoidTest()const{cout<<"OutputfromCBase!"<<endl;};
};
classCDerived:publicCBase
{
public:
voidTest()const{cout<<"OutputfromCDerived!"<<endl;};
};
请问调如下的函数输出什么?
voidTest()
{
CDerivedd;
CBase*pB=&d;
pB->Test();
CDerived*pD=&d;
pD->Test();
}
8.如果把上题中类CBase的Test方法改为非虚函数,输出又是什么?
9.有如下的类:
classCBase
{
public:
virtualvoidTest(intiTest=0)const=0;
};
classCDerived:publicCBase
{
public:
voidTest(intiTest=1)const{cout<<iTest<<endl;};
};
请问调如下的函数输出什么?
voidTest()
{
CBase*p=newCDerived;
p->Test();
deletep;
}
三、分析题
有如下的复数类:
classcomplex
{
public:
complex(doubler=0.0,doublei=0.0){re=r;im=i;};
doublereal()const{returnre;};
doubleimage()const{returnim;};
private:
doublere;
doubleim;
};
complexoperator+(constcomplex&left,constcomplex&right)
{
returncomplex(left.real()+right.real(),left.image()+right.image());
}
complexoperator*(constcomplex&left,constcomplex&right)
{
returncomplex(left.real()*right.real()-left.real()*right.image(),left.real()*right.image()+left.image()*right.real());
}
请分析:
10.没有把运算符重载设置为类的成员函数有什么好处?
11.能不能把函数参数前面的const去掉?为什么?
12.类的成员函数real()和image()后面的const表示什么?这样写有什么好处?
分享到:
相关推荐
C++试题C++试题C++试题C++试题(私人版)
c++试题 绝对经典c++试题 绝对经典c++试题 绝对经典c++试题 绝对经典
宝典 宝典 宝典 C++试题宝典C++试题宝典C++试题宝典
C++试题集C++试题集C++试题集C++试题集C++试题集
《C++试题》一个很好的c++试题咯 如果侵犯了您的版权,请与本人联系,本人将立即删除 需要大于20个字符,不支持HTML标签。
历年 自考 c++试题答案文档及教学源码
C++试题C++试题C++试题C++试题C++试题C++试题C++试题C++试题C++试题C++试题C++试题
广东高校计算机专业c++试题,( C)1. 下列 为合法的C++语言标识符。 A. const B. B-A C. _3H D. B#A ( B )2. 下列 为正确的变量说明语句。 short i=j=1; B. char ch, ch1=32; C. double: a,b; D. long float f,d;...
C++ 经典试题 C++ 经典试题 C++ 经典试题 C++ 经典试题 C++ 经典试题
NOIP2017提高组C++试题。HazeOJ整理。全网首个整理出的电子版试题!
C++试题汇总C++试题汇总C++试题汇总C++试题汇总C++试题汇总
C++试题及答案C++试题及答案C++试题及答案C++试题及答案C++试题及答案C++试题及答案
C++习题 C++基础试题
C++试题大全,足有300道,非常全面,并附有详细答案,希望对你有所帮助
中软国际C++笔试试题[1].pdf
2009-2013NOIP初赛提高组C++试题
中兴移动2014届校园招聘C++类试题,要笔试的赶快拿去,还新鲜着呢
网络游戏公司经典c++试题,很有参考价值。 希望大家能够喜欢
吉林大学C++历年试题 2002年到2007年的。c++面向对象考试试题
2007年c++考试试题含有答案 2007年c++考试试题含有答案