题目:
在各类中分别增加以下成员:
base1中增加私有数据成员int b1
base2中增加私有数据成员int b2
level1中增加私有数据成员int l1
level2中增加私有数据成员int l2
toplevel中增加私有数据成员int t
源代码:
//科目:C++实验5
//题目:修改书本p194页4.18的程序
//作者:武叶
//语言:C++
//创作时间:2012年4月14日
#include<iostream>
using namespace std;
class base1
{
private:
int b1;
public :
base1(int i)
{
b1=i;
cout<<"class base1"<<endl;
}
void show()
{
cout<<"b1:"<<b1<<endl;
}
};
class base2
{
private:
int b2;
public:
base2(int j)
{
b2=j;
cout<<"class base2"<<endl;
}
void show()
{
cout<<"b2:"<<b2<<endl;
}
};
class level1:public base2,virtual public base1
{
private:
int l1;
public:
level1(int i,int j,int k):base1(i),base2(j)
{
l1=k;
cout<<"class level1"<<endl;
}
void show()
{
base1::show();
base2::show();
cout<<"l1:"<<l1<<endl;
}
};
class level2:public base2,virtual public base1
{
private:
int l2;
public:
level2(int i,int j,int m):base1(i),base2(j)
{
l2=m;
cout<<"class level2"<<endl;
}
void show()
{
base1::show();
base2::show();
cout<<"l2:"<<l2<<endl;
}
};
class toplevel:public level1,virtual public level2
{
private:
int t;
public:
toplevel(int i,int j,int m,int k,int n):base1(i),level2(i,j,k),level1(i,j,m)
{
t=n;
cout<<"class toplevel"<<endl;
}
void show()
{
level1::show();
level2::show();
cout<<"t:"<<t<<endl;
}
};
int main()
{
toplevel obj(1,2,3,4,5);
obj.show();
return 0;
}
更多详细内容:::::去学习