源程序:

#include <iostream>
using namespace std;

class Base1
{
public:
Base1()
{
b1=6;
}
Base1(int i)
{
b1=i;
}
int get1()
{
return b1;
}
private:
int b1;
};

class Base2
{
public:
Base2(int i)
{
b2=i;
}
int get2()
{
return b2;
}
private:
int b2;
};
class Derived:public Base1,public Base2
{
int d1;
Base1 ob1;
Base2 ob2;
public:
Derived(int x,int y,int z):ob1(x),Base2(y),ob2(y)
{
d1=z;
}
void print()
{
cout<<"\nb1="<<get1()<<",ob1.b1="<<ob1.get1()
<<"\nb2="<<get2()<<",ob2.b2="<<ob2.get2()
<<"\nd1="<<d1;
}
};
int main()
{
Derived d(9,8,7);
d.print();
return 1;
}

 运行结果:

c++多重继承典型小程序_源程序