2012年(1008)
分类:
2012-08-01 11:08:50
原文地址:多继承 作者:luozhiyong131
/*
* 多继承
* Lzy 2011-七夕
*/
#include
using namespace std;
class A
{
private:
int a;
public:
A():a(1){}
};
class B
{
private:
int b;
public:
B():b(2){}
void displayB(){cout<<"B :"<
};
class C:public A,public B
{
private:
int c;
public:
C():c(3){}
void displayC(){cout<<"C :"<
};
int main(void)
{
C c;
c.displayA();
c.displayB();
c.displayC();
return 0;
}
/*
* 多继承的构造函数和析构函数
* 对于所有需要给予参数进行初始化的基类,都要显示给出基类名和参数表
* Lzy 2011-七夕
*/
#include
using namespace std;
class Base1
{
private:
int b1;
public:
Base1(){cout<<"Base1 缺省构造函数:"<
Base1(int x):b1(x){cout<<"Base1 构造函数:"<
~Base1(){cout<<"Base1 析构函数:"<
};
class Base2
{
private:
int b2;
public:
Base2(){cout<<"Base2 缺省构造函数:"<
Base2(int x):b2(x){cout<<"Base2 构造函数:"<
~Base2(){cout<<"Base2 析构函数:"<
};
class Derive:public Base1, public Base2
{
private:
Base1 b1;
Base2 b2;
public:
Derive(){cout<<"Derive 缺省构造函数:"<
Derive(int x, int y,int i, int j):b1(x),b2(y),Base1(i),Base2(j)
{cout<<"Derive 构造函数:"<
~Derive(){cout<<"Derive 析构函数:"<
};
int main(void)
{
Derive obj(1,2,3,4);
return 0;
}
/*
* 多继承二义性问题
* 作用域运算符
* Lzy 2011-七夕
*/
#include
using namespace std;
class Base1
{
private:
int b1;
public:
Base1():b1(3){}
void display(){cout<<"Base1 :"<
};
class Base2
{
private:
int b2;
public:
Base2():b2(5){}
void display(){cout<<"Base2 :"<
};
class Derive:public Base1, public Base2
{
private:
int d;
public:
Derive():d(0){}
void print(){cout<<"Derive :"<
void display(){Base1::display();Base2::display();}
};
int main(void)
{
Derive d;
d.Base1::display();
d.Base2::display();
d.print();
d.display();
return 0;
}