C++解析(24):抽象类和接口、多重继承
0.目录
1.抽象类和接口
2.被遗弃的多重继承
3.小结
1.抽象类和接口
1.1 抽象类
面向对象中的抽象类:
- 可用于表示现实世界中的抽象概念
- 是一种只能定义类型,而不能产生对象的类
- 只能被继承并重写相关函数
- 直接特征是相关函数没有完整的实现
Shape是现实世界中各种图形的抽象概念,因此:
- 程序中必须能够反映抽象的图形
- 程序中通过抽象类表示图形的概念
- 抽象类不能创建对象,只能用于继承
1.2 纯虚函数
抽象类与纯虚函数:
- C++语言中没有抽象类的概念
- C++中通过纯虚函数实现抽象类
- 纯虚函数是指只定义原型的成员函数
- 一个C++类中存在纯虚函数就成为了抽象类
纯虚函数的语法规则:
示例——抽象类:
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double area() = 0;
};
class Rect : public Shape
{
int ma;
int mb;
public:
Rect(int a, int b)
{
ma = a;
mb = b;
}
double area()
{
return ma * mb;
}
};
class Circle : public Shape
{
int mr;
public:
Circle(int r) { mr = r; }
double area()
{
return 3.14 * mr * mr;
}
};
void area(Shape* p)
{
double r = p->area();
cout << "r = " << r << endl;
}
int main()
{
Rect rect(1, 2);
Circle circle(10);
area(&rect);
area(&circle);
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
r = 2
r = 314
- 抽象类只能用作父类被继承
- 子类必须实现纯虚函数的具体功能
- 纯虚函数被实现后成为虚函数
- 如果子类没有实现纯虚函数,则子类成为抽象类
1.3 接口
满足下面条件的C++类则称为接口:
- 类中没有定义任何的成员变量
- 所有的成员函数都是公有的
- 所有的成员函数都是纯虚函数
- 接口是一种特殊的抽象类
示例——接口:
#include <iostream>
using namespace std;
class Channel
{
public:
virtual bool open() = 0;
virtual void close() = 0;
virtual bool send(char* buf, int len) = 0;
virtual int receive(char* buf, int len) = 0;
};
int main()
{
return 0;
}
C++中没有真正的接口,但是C++的后续语言Java、C#直接支持接口的概念!
2.被遗弃的多重继承
2.1 C++中的多重继承
C++支持编写多重继承的代码:
- 一个子类可以拥有多个父类
- 子类拥有所有父类的成员变量
- 子类继承所有父类的成员函数
- 子类对象可以当作任意父类对象使用
多重继承的语法规则:
2.2 多重继承的问题一
示例——多重继承的问题一:
#include <iostream>
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();
cout << "d.getA() = " << d.getA() << endl;
cout << "d.getB() = " << d.getB() << endl;
cout << "d.getC() = " << d.getC() << endl;
cout << endl;
BaseA* pa = &d;
BaseB* pb = &d;
cout << "pa->getA() = " << pa->getA() << endl;
cout << "pb->getB() = " << pb->getB() << endl;
cout << endl;
void* paa = pa;
void* pbb = pb;
if( paa == pbb )
{
cout << "Pointer to the same object!" << endl;
}
else
{
cout << "Error" << endl;
}
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
cout << "paa = " << paa << endl;
cout << "pbb = " << pbb << endl;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
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 = 0x7ffc9f641dc0
pb = 0x7ffc9f641dc4
paa = 0x7ffc9f641dc0
pbb = 0x7ffc9f641dc4
通过多重继承得到的对象可能拥有“不同的地址”!!
解决方案:无
(其实pa和pb还是指向了同一个对象,但是指向的是同一个对象的不同位置,打个比方就是pa指向了这个对象的脑袋,pb指向了这个对象的胸口。。。)
2.3 多重继承的问题二
多重继承可能产生冗余的成员:
示例——多重继承的问题二:
#include <iostream>
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 + "1", age + 10), Student(name + "2", age + 1) { }
};
int main()
{
Doctor d("Bob", 33);
//d.print();
d.Teacher::print();
d.Student::print();
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Name = Bob1, Age = 43
Name = Bob2, Age = 34
当多重继承关系出现闭合时将产生数据冗余的问题!!!!
解决方案:虚继承
- 虚继承能够解决数据冗余问题
- 中间层父类不再关心顶层父类的初始化
- 最终子类必须直接调用顶层父类的构造函数
示例——使用虚继承解决数据冗余:
#include <iostream>
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+"1", age), Student(name+"2", age), People(name+"3", age) { }
};
int main()
{
Doctor d("Delphi", 33);
d.print();
d.Teacher::print();
d.Student::print();
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
Name = Delphi3, Age = 33
Name = Delphi3, Age = 33
Name = Delphi3, Age = 33
问题:
当架构设计中需要继承时,无法确定使用直接继承还是虚继承!!
2.4 多重继承的问题三
多重继承可能产生多个虚函数表
示例——多重继承的问题三:
#include <iostream>
using namespace std;
class BaseA
{
public:
virtual void funcA()
{
cout << "BaseA::funcA()" << endl;
}
};
class BaseB
{
public:
virtual void funcB()
{
cout << "BaseB::funcB()" << endl;
}
};
class Derived : public BaseA, public BaseB
{
};
int main()
{
Derived d;
BaseA* pa = &d;
BaseB* pb = &d;
BaseB* pbb = (BaseB*)pa;
cout << "sizeof(d) = " << sizeof(d) << endl;
cout << "Using pa to call funcA()..." << endl;
pa->funcA();
cout << "Using pb to call funcB()..." << endl;
pb->funcB();
cout << "Using pbb to call funcB()..." << endl;
pbb->funcB();
cout << endl;
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
cout << "pbb = " << pbb << endl;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
sizeof(d) = 16
Using pa to call funcA()...
BaseA::funcA()
Using pb to call funcB()...
BaseB::funcB()
Using pbb to call funcB()...
BaseA::funcA()
pa = 0x7fffd5157c20
pb = 0x7fffd5157c28
pbb = 0x7fffd5157c20
需要进行强制类型转换时,C++中推荐使用新式类型转换关键字!!
解决方案:dynamic_cast
示例——使用新式类型转换dynamic_cast关键字:
#include <iostream>
using namespace std;
class BaseA
{
public:
virtual void funcA()
{
cout << "BaseA::funcA()" << endl;
}
};
class BaseB
{
public:
virtual void funcB()
{
cout << "BaseB::funcB()" << endl;
}
};
class Derived : public BaseA, public BaseB
{
};
int main()
{
Derived d;
BaseA* pa = &d;
BaseB* pb = &d;
BaseB* pbb = (BaseB*)pa; // oops!!
BaseB* pbc = dynamic_cast<BaseB*>(pa);
cout << "sizeof(d) = " << sizeof(d) << endl;
cout << "Using pa to call funcA()..." << endl;
pa->funcA();
cout << "Using pb to call funcB()..." << endl;
pb->funcB();
cout << "Using pbb to call funcB()..." << endl;
pbb->funcB();
cout << "Using pbc to call funcB()..." << endl;
pbc->funcB();
cout << endl;
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
cout << "pbb = " << pbb << endl;
cout << "pbc = " << pbc << endl;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
sizeof(d) = 16
Using pa to call funcA()...
BaseA::funcA()
Using pb to call funcB()...
BaseB::funcB()
Using pbb to call funcB()...
BaseA::funcA()
Using pbc to call funcB()...
BaseB::funcB()
pa = 0x7ffcc2c27ff0
pb = 0x7ffcc2c27ff8
pbb = 0x7ffcc2c27ff0
pbc = 0x7ffcc2c27ff8
2.5 正确的使用多重继承
工程开发中的“多重继承”方式:
- 单继承某个类 + 实现(多个)接口
示例——正确的多继承方式:
#include <iostream>
using namespace std;
class Base
{
protected:
int mi;
public:
Base(int i) { mi = i; }
int getI() { return mi; }
bool equal(Base* obj)
{
return (this == obj);
}
};
class Interface1
{
public:
virtual void add(int i) = 0;
virtual void minus(int i) = 0;
};
class Interface2
{
public:
virtual void multiply(int i) = 0;
virtual void divide(int i) = 0;
};
class Derived : public Base, public Interface1, public Interface2
{
public:
Derived(int i) : Base(i) { }
void add(int i) { mi += i; }
void minus(int i) { mi -= i; }
void multiply(int i) { mi *= i; }
void divide(int i)
{
if( i != 0 ) { mi /= i; }
}
};
int main()
{
Derived d(100);
Derived* p = &d;
Interface1* pInt1 = &d;
Interface2* pInt2 = &d;
cout << "p->getI() = " << p->getI() << endl; // 100
pInt1->add(10);
pInt2->divide(11);
pInt1->minus(5);
pInt2->multiply(8);
cout << "p->getI() = " << p->getI() << endl; // 40
cout << endl;
cout << "pInt1 == p : " << p->equal(dynamic_cast<Base*>(pInt1)) << endl;
cout << "pInt2 == p : " << p->equal(dynamic_cast<Base*>(pInt2)) << endl;
return 0;
}
运行结果为:
[root@bogon Desktop]# g++ test.cpp
[root@bogon Desktop]# ./a.out
p->getI() = 100
p->getI() = 40
pInt1 == p : 1
pInt2 == p : 1
一些有用的工程建议:
- 先继承自一个父类,然后实现多个接口
- 父类中提供equal()成员函数
- equal()成员函数用于判断指针是否指向当前对象
- 与多重继承相关的强制类型转换用dynamic_cast完成
3.小结
- 抽象类用于描述现实世界中的抽象概念
- 抽象类只能被继承不能创建对象
- C++中没有抽象类的概念
- C++中通过纯虚函数实现抽象类
- 类中只存在纯虚函数的时成为接口
- 接口是一种特殊的抽象类
- C++支持多重继承的编程方式
- 多重继承容易带来问题
- 可能出现“同一个对象的地址不同”的情况
- 虚继承可以解决数据冗余的问题
- 虚继承的使得架构设计可能出现问题
- 多继承中可能出现多个虚函数表指针
- 与多重继承相关的强制类型转换用dynamic_cast完成
- 工程开发中采用“单继承多接口”的方式使用多继承
- 父类提供成员函数用于判断指针是否指向当前对象