*文件名称:test.cpp
*作 者:张明宇
*完成日期:2016年5月6日
*/
问题及代码:
阅读程序,写出执行结果
(1)#include <iostream>
using namespace std;
class Data
{
public:
Data(int i):x(i){cout<<"A";}
~Data(){ cout<<"B";}
private:
int x;
};
class Base
{
public:
Base(int i):b1(i){cout<<"C";}
~Base(){ cout<<"D";}
private:
int b1;
};
class Derived:public Base
{
public:
Derived (int i,int j):Base(i),d1(j){
cout<<"E";
}
~Derived(){cout<<"F";}
private:
Data d1;
};
int main()
{
Derived obj(1,2);
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
A(){a=0;}
A (int i){a=i;}
void print(){cout<<a<<" ";}
private:
int a;
};
class B: public A
{
public:
B(){b=0;}
B(int i, int j, int k): A(i),aa(j){b=k;}
//思考:这3处出现的print,有何区别
void print() //(1)
{
A::print(); //(2)
aa.print(); //(3)
cout<<b<<endl;
}
private:
int b;
A aa;
};
int main()
{
B test[2];
test[0]=B(1,4,7);
test[1]=B(2,5,8);
for(int i=0; i<2; i++)
test[i].print();
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
A(char *s){cout<<s<<endl;}
};
class B:public A
{
public:
B(char *s1, char *s2):A(s1){
cout<<s2<<endl;
}
};
class C:public A
{
public:
C(char *s1,char *s2):A(s1){
cout<<s2<<endl;
}
};
class D:public B, C
{
public:
D(char *s1,char *s2,char *s3,char *s4):
B(s1,s2),C(s3,s4){cout<<s4<<endl;}
};
int main()
{
D d("class A","class B","class C","class D");
return 0;
}