C++面向对象程序设计(试题)求高手解答

来源:百度知道 编辑:UC知道 时间:2024/07/05 19:29:38
第一题
#include <iostream.h>
//友元类、子对象与静态数据成员
class X{
friend class Y;
public:
X(int i=5){a=i;}
void Display( ){cout<<"a="<<a<<",y="<<y<<endl;}
private:
int a;
static int y;
};
class Y{
X x;
public:
Y(int i,int j){x. a=i;X::y=j ;}
void Display( ){cout<<"a="<<x. a<<",y="<<X::y<<endl; }
};
int X::y=4;
void main( ){
X b(5); b.Display( );
Y C(7, 3); C.Display( ); b.Display( );
}
-----------写结果

第二题
#include<iostream.h>
//模板函数
template<class T>
void printArray(T * array,int Cnt){
for(int i=0;i<Cnt ;i++)
cout<<array[i]<<" ";

第一题:
a=5,y=4
a=7,y=3
a=5,y=3

第二题:
y中值:4.5 5.6 6.7z中字符:H o w a r e

第三题:
Coord(0,0)
Coord(-3,8)
Coord[-3,8]
Coord[0,0]

第四题:
构造Base
构造Base
构造Derived
析构 Derived
析构Base
析构Base

1.
a=5,y=4
a=7,y=3
a=5,y=3

2.
不知printArray(y,3); 是否正确,我觉得应该是printArray<float>(y,3);
y中值:4.5 5.6 6.7 z中字符:H o w a r e \n(回车)

3.
Coord(0,0)
Coord(-3,8)
Coord[-3,8]
Coord[0,0]

4.
构造Base \n
构造Base \n
构造Derived\n
析构 Derived \n
析构Base\n
析构Base\n

========================
如有误,请指正。