第18节 多态与继承 – 下
-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
重写与重载
面试题
重载和重写有什么区别?什么时候是重载,什么时候是重写?
下面的继承方式有问题吗?为什么?
#include <cstdlib>
#include <iostream>
using namespace std;
//重载只能发生在同一个类里面(同一个作用域),不能发生在二个类
//否则就是自动覆盖
class Parent
{
public:
//3个虚函数
virtual void func()
{
cout<<"void func()"<<endl;
}
virtual void func(int i)
{
cout<<"void func(int i)"<<endl;
}
virtual void func(int i, int j)
{
cout<<"void func(int i, int j)"<<endl;
}
};
class Child : public Parent
{
public:
//此处是重写关系
//子类自动重写父类的virtual void func(int i, int j)函数
void func(int i, int j)
{
cout<<"void func(int i, int j)"<<" "<<i + j<<endl;
}
void func(int i, int j, int k)
{
cout<<"void func(int i, int j, int k)"<<" "<<i + j + k<<endl;
}
};
void run(Parent* p)//指针
{
p->func(1, 2);
}
int main(int argc, char *argv[])
{
Parent p;
p.func();
p.func(1);
p.func(1, 2);
Child c;
c.func(1, 2);
run(&p);//重写 多态
run(&c);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
函数重载
必须在同一个类中进行
子类无法重载父类的函数,父类同名函数将被覆盖
重载是在编译期间根据参数类型和个数决定调用函数
函数重写
必须发生于父类与子类之间
并且父类与子类中的函数必须有完全相同的原型
使用 virtual 声明之后能够产生多态
多态是在运行期间根据具体对象的类型决定调用函数
第二:
虚函数深入理解
问题
是否可以将类的每个成员函数都声明为虚函数?
C++ 中多态的实现原理
当类中声明虚函数时,编译器会在类中生成一个虚函数表
虚函数表是一个存储类成员函数指针的数据结构
虚函数表是由编译器自动生成与维护的
virtual 成员函数会被编译器放入虚函数表中
存在虚函数时,每个对象中都有一个指向虚函数表的指针
第四:
问题
对象中的 VPTR 指针什么时候被初始化?
对象在创建的时候由编译器对 VPTR 指针进行初始化
只有当对象的构造完全结束后 VPTR 的指向才最终确定
父类对象的 VPTR 指向父类虚函数表
子类对象的 VPTR 指向子类虚函数表
结论:构造函数中调用虚函数无法实现多态。
构造函数中的“ 多态”
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
public:
Parent()
{
this->func();
}
virtual void func()//构造函数里的 虚函数
{
cout<<"void Parent::func()"<<endl;
}
};
class Child : public Parent
{
public:
void func()//此处子类重写func(),但是并不会发生多态!!!
{
cout<<"void Child::func()"<<endl;
}
};
int main(int argc, char *argv[])
{
Child c;
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
第五:
纯虚函数
面向对象中的抽象概念
在进行面向对象分析时,会有一些抽象的概念!
在现实中需要知道具体的图形类型才能求面积,
所以对概念上的 “ 图形 ” 求面积是没有意义的!
用 Shape作为基类进行继承
#include <cstdlib>
#include <iostream>
using namespace std;
class Shape//求面积 属于抽象类,要用到纯虚函数
{
public:
virtual double area() = 0;//area 是纯虚函数, = 0告诉编译器,这个函数故意只声明不定义
};
class Rectangle : public Shape
{
double m_a;
double m_b;
public:
Rectangle(double a, double b)//矩形
{
m_a = a;
m_b = b;
}
double area()
{
return m_a * m_b;//矩形面积公式
}
};
class Circle : public Shape//圆
{
double m_r;//半径
public:
Circle(double r)
{
m_r = r;
}
double area()
{
return 3.14 * m_r * m_r;
}
};
//不能定义抽象类对象,但是可以定义抽象类指针
void area(Shape* s)//指针 多态特性
{
cout<<s->area()<<endl;
}
int main(int argc, char *argv[])
{
Rectangle rect(2, 3);//矩形对象
Circle circle(4); //圆对象
//shape s; //错误,抽象类不能用于定义对象
//抽象类只能用于定义指针和引用
area(&rect);//打印矩形面积
area(&circle);//打印圆面积
//area(&s); //错误
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
纯虚函数
面向对象中的抽象类
抽象类可用于表示现实世界中的抽象概念
抽象类是一种只能定义类型,而不能产生对象的类
抽象类只能被继承并重写相关函数
抽象类的直接特征是纯虚函数
纯虚函数是只声明函数原型,而 故意 不定义函数体的虚函数
抽象类与纯虚函数
抽象类不能用于定义对象
抽象类只能用于定义指针和引用
抽象中的纯虚函数必须被子类重写
小结:
函数重载与函数重写不同
多态是通过虚函数表实现的
虚函数在效率上会受到影响
抽象类可用于表示现实世界中的抽象概念
抽象类是通过纯虚函数实现的