C++面向对象程序设计(试题)求高手解答
第一题#include<iostream.h>//友元类、子对象与静态数据成员classX{friendclassY;public:X(inti=5){a=i;}void...
第一题
#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]<<" ";
}
void main( ){
float y[3]={ 4.5,5.6, 6.7 };
char z[13]="How are you?";
cout<<"y中值:";printArray(y,3);
cout<<"z中字符:";printArray(z,7);
cout<<endl;
}
---------写结果
第三题
#include <iostream.h>
//构造函数与析构函数
class Coord{
int x, y;
public:
Coord(int a=0,int b=0)
{ x=a; y=b; cout<<"Coord("<<x<<","<<y<<")\n"; }
~Coord(){ cout<<"Coord["<<x<<","<<y<<"]\n"; }
};
void main( ){ Coord ob1, ob2(-3,8) ; }
------------写结果
第四题
#include <iostream.h>
//派生类的构造函数与析构函数
class Base{
int x;
public:
Base(int y=0) { x=y; cout<<"构造Base \n"; }
~Base() { cout<<"析构Base\n";}
};
class Derived : public Base {
public:
Derived (int x=0, int y=0):Base(x), d(y) { cout<<"构造Derived\n"; }
~Derived () { cout<<"析构 Derived \n"; }
private:
Base d;
};
void main( ){ Derived Ob; }
-------------写结果 展开
#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]<<" ";
}
void main( ){
float y[3]={ 4.5,5.6, 6.7 };
char z[13]="How are you?";
cout<<"y中值:";printArray(y,3);
cout<<"z中字符:";printArray(z,7);
cout<<endl;
}
---------写结果
第三题
#include <iostream.h>
//构造函数与析构函数
class Coord{
int x, y;
public:
Coord(int a=0,int b=0)
{ x=a; y=b; cout<<"Coord("<<x<<","<<y<<")\n"; }
~Coord(){ cout<<"Coord["<<x<<","<<y<<"]\n"; }
};
void main( ){ Coord ob1, ob2(-3,8) ; }
------------写结果
第四题
#include <iostream.h>
//派生类的构造函数与析构函数
class Base{
int x;
public:
Base(int y=0) { x=y; cout<<"构造Base \n"; }
~Base() { cout<<"析构Base\n";}
};
class Derived : public Base {
public:
Derived (int x=0, int y=0):Base(x), d(y) { cout<<"构造Derived\n"; }
~Derived () { cout<<"析构 Derived \n"; }
private:
Base d;
};
void main( ){ Derived Ob; }
-------------写结果 展开
2个回答
展开全部
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
========================
如有误,请指正。
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
========================
如有误,请指正。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询