return0;//返回值0,返回操作系统
}
3.请完成下面地程序
#include//预处理命令
usingnamespacestd;//使用标准命名空间std
//复数类
classComplex
{
private:
doublerealPart;//实部
doubleimagePart;//虚部
public:
Complex(doublereal=0,doubleimage=0):
realPart(real),imagePart(image){}//构造函数
doubleGetRealPart()const{returnrealPart;}//返回实部
doubleGetImagePart()const{returnimagePart;}//返回虚部
Complexoperator+(constComplex&a)const//重载加法运算符+
{
returnComplex([3]);//返回和
}
};
intmain(void)//主函数main(void)
{
Complexa(1,2),b(2,6),c;//定义复数对象
c=a+b;//复数加法运算
cout<<"a="<cout<<"b="<cout<<"c="<return0;//返回值0,返回操作系统
}
四、程序分析题(本大题共4小题,每小题5分,共20分)给出下面各程序地输出结果.
1.阅读下面程序,写出输出结果.
#include//预处理命令
usingnamespacestd;//使用标准命名空间std
classA
{
public:
virtualvoidShow()const
{cout<<"ClassA"<};
classB:
publicA
{
public:
voidShow()const
{cout<<"ClassB"<};
voidShow(constA&obj)
{obj.Show();}
intmain(void)
{
Aa;Bb;
Show(a);Show(b);
A*p;
p=&a;p->Show();
p=&b;p->Show();
B*q;
q=&b;q->Show();
return0;//返回值0,返回操作系统
}
上面程序地输出结果为:
2.阅读下面程序,写出输出结果.
#include//预处理命令
usingnamespacestd;//使用标准命名空间std
template
voidShow(ElemTypea[],intn)
{
for(inti=0;i{cout<}
intmain(void)
{
inta[]={1,6,9};
Show(a,sizeof(a)/sizeof(int));
Show(a,2);
cout<return0;//返回值0,返回操作系统
}
上面程序地输出结果为:
3.阅读下面程序,写出输出结果.
#include//预处理命令
usingnamespacestd;//使用标准命名空间std
classMyClass
{
public:
MyClass(){count++;}
~MyClass(){count--;}
staticintGetCount(){returncount;}
private:
staticintcount;
};
intMyClass:
:
count=0;//初始化静态数据成员
intmain(void)
{
MyClassobj1;
cout<:
GetCount()<MyClassobj2;
cout<:
GetCount()<MyClassobj3;
cout<MyClass*p=newMyClass;
cout<:
GetCount()<deletep;
cout<:
GetCount()<return0;//返回值0,返回操作系统
}
上面程序地输出结果为:
4.阅读下面程序,写出输出结果.
#include//预处理命令
usingnamespacestd;//使用标准命名空间std
classA
{
public:
A(){cout<<"A()"<~A(){cout<<"~A()"<virtualvoidf(){cout<<"A:
:
f()"<};
classB:
pu