1.C++支持编写多重继承
-> 一个子类可以拥有多个父类。
-> 子类拥有所有父类的成员变量。
-> 子类继承所有父类的成员函数。
-> 子类对象可以当作任意父类对象使用。
多重继承模板:
class Derived : public BaseA,
public BaseB,
public BaseC
{
// ......
};
代码示例:多重继承
#include <iostream>
#include <string>
using namespace std;
class BaseA
{
int ma;
public:
BaseA(int a)
{
ma = a;
}
int getA()
{
return ma;
}
};
class BaseB
{
int mb;
public:
BaseB(int b)
{
mb = b;
}
int getB()
{
return mb;
}
};
class Derived : public BaseA, public BaseB
{
int mc;
public:
Derived(int a, int b, int c) : BaseA(a), BaseB(b) //调用两个父类构造函数
{
mc = c;
}
int getC()
{
return mc;
}
void print()
{
cout << "ma = " << getA() << ", "
<< "mb = " << getB() << ", "
<< "mc = " << mc << endl;
}
};
int main()
{
cout << "sizeof(Derived) = " << sizeof(Derived) << endl; // 12
Derived d(1, 2, 3); //构造一个对象
d.print(); //ma = 1 mb = 2 mc = 3
cout << "d.getA() = " << d.getA() << endl; //1
cout << "d.getB() = " << d.getB() << endl; //2
cout << "d.getC() = " << d.getC() << endl; //3
cout << endl; //换行
BaseA* pa = &d;
BaseB* pb = &d;
cout << "pa->getA() = " << pa->getA() << endl; //1
cout << "pb->getB() = " << pb->getB() << endl; //2
cout << endl; //换行
void* paa = pa;
void* pbb = pb;
if( paa == pbb )
{
cout << "Pointer to the same object!" << endl;
}
else //执行error
{
cout << "Error" << endl;
}
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
cout << "paa = " << paa << endl;
cout << "pbb = " << pbb << endl;
return 0;
}
结果:
sizeof(Derived) = 12
ma = 1, mb = 2, mc = 3
d.getA() = 1
d.getB() = 2
d.getC() = 3
pa->getA() = 1
pb->getB() = 2
Error
pa = 0xbfe7e304
pb = 0xbfe7e308
paa = 0xbfe7e304
pbb = 0xbfe7e308
分析:
BaseA* pa = &d;? BaseB* pb = &d;? 结果两个地址相差4,说明pa指向BaseA的地址,pb指向BaseB的地址,表示同一个对象有多个虚函数表。
2.多重继承可能产生冗余的成员(成员变量或者成员函数)
菱形继承:
代码示例:产生冗余的成员(成员变量或者成员函数)
#include <iostream>
#include <string>
using namespace std;
class People
{
string m_name;
int m_age;
public:
People(string name, int age)
{
m_name = name;
m_age = age;
}
void print()
{
cout << "Name = " << m_name << ", "
<< "Age = " << m_age << endl;
}
};
class Teacher : public People
{
public:
Teacher(string name, int age) : People(name, age) //调用父类构造函数
{
}
};
class Student : public People
{
public:
Student(string name, int age) : People(name, age) //调用父类构造函数
{
}
};
class Doctor : public Teacher, public Student
{
public:
Doctor(string name, int age) : Teacher(name, age), Student(name, age)
{
}
};
int main()
{
Doctor d("Delphi", 33);
d.print();
return 0;
}
结果:编译错误
53-2.cpp: In constructor ‘Doctor::Doctor(std::string, int)’:
53-2.cpp:45:73: error: type ‘People’ is not a direct base of ‘Doctor’
Doctor(string name, int age) : Teacher(name, age), Student(name, age), People(name, age)
错误:'People'类型不是一个'Doctor'直接基类
53-2.cpp: In function ‘int main()’:
53-2.cpp:55:4: error: request for member ‘print’ is ambiguous
d.print();
^
错误:请求成员'print'是不明确的操作
53-2.cpp:17:7: note: candidates are: void People::print()
void print()
^
53-2.cpp:17:7: note: void People::print()
分析:
d有多个print函数,编译器不知道调用哪个print函数。
可以通过作用域访问符:这样就不会出现编译错误
d.Teacher::print()
d.Student::print()
打印结果:打印了相同的信息。
Name = Delphi, Age = 33
Name = Delphi, Age = 33
继续测试:
class Doctor : public Teacher, public Student
{
public:
Doctor(string name, int age) : Teacher(name + "1", age+1), Student(name + "2", age + 2), People(name, age) //直接调用顶层父类的构造函数
{
}
};
?结果:
Name = Delphi1, Age = 34
Name = Delphi2, Age = 35
分析:
结果虽然没有编译错误,但是还是打印出来了两个结果,还是会产生产生冗余的成员,解决的办法就是使用虚继承的方式,子类必须直接调用顶层父类的构造函数。
代码示例:解决办法,虚继承。
#include <iostream>
#include <string>
using namespace std;
class People
{
string m_name;
int m_age;
public:
People(string name, int age)
{
m_name = name;
m_age = age;
}
void print()
{
cout << "Name = " << m_name << ", "
<< "Age = " << m_age << endl;
}
};
class Teacher : virtual public People
{
public:
Teacher(string name, int age) : People(name, age) //调用父类构造函数
{
}
};
class Student : virtual public People
{
public:
Student(string name, int age) : People(name, age) //调用父类构造函数
{
}
};
class Doctor : public Teacher, public Student
{
public:
Doctor(string name, int age) : Teacher(name, age), Student(name, age)
{
}
};
int main()
{
Doctor d("Delphi", 33);
d.print();
return 0;
}
结果:
Name = Delphi, Age = 33
小结:
-> C++支持多重继承的编程方式。
-> 虚继承能够解决数据冗余问题,最终子类必须直接调用顶层父类的构造函数(注意)。
|