`
soboer
  • 浏览: 1310483 次
文章分类
社区版块
存档分类
最新评论

我出的C++试题

 
阅读更多

一、问答题

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;
};

请问调如下的函数输出什么?

voidTest()
{
CTesttest;
}


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;
};

complex
operator+(constcomplex&left,constcomplex&right)
{
returncomplex(left.real()+right.real(),left.image()+right.image());
}

complex
operator*(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表示什么?这样写有什么好处?

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics