//多重继承
#include<iostream>
using namespace std;
class Telephone
{
public:
Telephone()
{
cout << "Telephone构造"<< this << endl;
}
void call()
{
cout << "有服务吗" << endl;
}
int m_t;
};
class Camera
{
public:
Camera()
{
cout << "Camera构造:" << this << endl;
}
void Photo()
{
cout << "马赛克" << endl;
}
int m_c;
};
class Ipod
{
public:
Ipod()
{
cout << "Ipod构造:"<< this << endl;
}
void Music()
{
cout << "马赛克"<<endl;
}
int m_i;
};
class IPhone : public Telephone, public Camera, public Ipod
{
public:
IPhone()
{
cout << "IPhone构造"<< this << endl;
}
}
int main()
{
IPhone ipx;
ipx.call();
ipx.Photo();
ipx.Music();
Telephone* t = &ipx; //Telephone *t (Telephone *)&ipx;
//Camera* c = ipx;
//ipod* i = &ipx;
//cout << t << “ ” << c << “ ” << i << “ ” << endl;
//子类隐式转换成基类 从而实现皆然性
//double b;
//int *p=(int*)(&b);
return 0;
}
//菱形(钻石)继承
class A
{
public:
A()
{
cout << “A构造” << this << endl;
}
void foo()
{
m_a = 10;
cout << m_a << endl;
}
int m_a;
};
class X :public A
{
public:
X()
{
cout << “X构造” << this << endl;
}
}
class Y :public A
{
public:
Y()
{
cout << “Y构造” << this << endl;
}
}
class Z :public X, public Y
{
public:
Z()
{
cout << “构造” << this << endl;
}
}
int main()
{
Z z;
z.foo();//调用不明确
z.x::foo();
z.y::foo();
z.A::foo();//两个A会报错
return 0;
}
//虚继承
class A
{
public:
A()
{
cout << "A构造" << this << endl;
}
void foo()
{
m_a = 10;
cout << m_a << endl;
}
int m_a;
};
class X :virtual public A
{
public:
X()
{
cout << "X构造"<< this << endl;
}
};
class Y :virtual public A
{
public:
Y()
{
cout << "Y构造" << this << endl;
}
};
class Z :public X, public Y
{
public:
Z()
{
cout << "构造" << this << endl;
}
};
int main()
{
Z z;
z.foo();
return 0;
}