多态:(多种状态)
C:switch/case , if条件语句
C++: 函数/运算符重载
编译时:函数重载
运行时:switch/case
现象:类型兼容性原则+重写
重写:发生在继承关系,父类和子类都有相同的函数原型(成员覆盖)
重载:同名不同作用(重载的函数原型不同)
相同点:名字相同
早期联编(静态链接):
(编译时的多态)
后期联编(动态链接)
(运行时的多态)
虚函数:
语法: virtual <类型> 成员函数名(<参数列表>)
必须是基类中成员函数,且当基类的某个成员函数定义为虚函数的时候
他所有的派生类中与基类虚函数相同(函数原型相同)的函数都是虚函数
基类中声明过virtual,派生类中不需要再声明(不用再加virtual)
* 通常情况,基类的某个成员函数被定义为虚函数,要在派生类中对他重新定义(重写)
否则虚函数是无效的;
多态:
成立的三个条件:
1.要有继承;
2.要有虚函数重写
3.要用父类指针(或者引用)指向子类对象; p = &s2;
如何抑制多态的实现: 域解析符::
p->Student::cal()
#include <iostream>
using namespace std;
class Student
{
public:
virtual void cal()
{
cout << "student cal..." << endl;
}
};
class Graduate:public Student
{
public:
void cal()
{
cout << "Graduate cal.." << endl;
}
};
int main()
{
Student s1,*p;
Graduate s2;
p = &s1;
p->cal(); student cal
p = &s2;
//早期联编导致,编译到Student s1,*p;,p就已经确定好了是"student cal..."
// 不管在后面做什么操作,p->cal永远是"student cal..."
//不管p指向哪一个派生类,都不会为之改变为派生类版本
//如何解决:
//动态链接(后期联编)
// C++ 虚函数 virtual
p->cal(); // Graduate cal
s2.cal(); // Graduate cal;
return 0;
}
多态原理:
1.虚函数是在运行时候完成,所以需要寻址
普通函数在编译时就确定了需要调用的函数
2.寻址(时间)+虚函数表(空间) 虚函数是要耗费更多的时间与空间
出于效率考虑,谨慎添加virtual虚函数声明
#include <iostream>
#define PI 3.14
using namespace std;
class CShape
{
protected:
double x;
double y;
public:
CShape(double x,double y)
{
this->x = x;
this->y = y;
}
virtual double get_V()
//虚函数表(virtual声明会额外申请一块存储空间)
//VPTR指针指向这个虚函数表
// CShape | double get_V()
// BALL | double get_V() (存的是地址)
// Rectangle | double get_V()
// 运行时候再确定用哪个函数
{
return 0;
}
};
class Ball:public CShape
{
protected:
double r;
public:
Ball(double x,double y,double r):CShape(x,y)
{
this->r = r;
}
double get_V()
{
return PI*1.33*r*r*r;
}
};
class Rectangle:public CShape
{
protected:
double z;
public:
Rectangle(double x,double y,double z):CShape(x,y)
{
this->z = z;
}
double get_V()
{
return x*y*z;
}
};
void func(CShape &p) // 通过一个函数接口实现多态
//可扩展/节省代码
{
cout << p.get_V() << endl;
}
int main()
{
Ball b1(0,0,5.0);
func(b1); // CShape &p = b1
Rectangle r1(3.0,4.0,5.0);
func(r1); // CShape &p = r1
return 0;
}
构造中调用虚函数(无意义)
为什么多态失效?
(继承的构造函数顺序:先Base后Derived)
在构造基类的时候,派生类obj还没有构造,没有函数重写
没有函数重写,那就没有多态
虚析构函数;(重点)
何时使用:当将基类指针/引用new运算符指向派生类实例时候
作用:为了在释放派生类实例时能够调用派生类的析构函数,
必须将析构函数声明为虚函数
纯虚函数与抽象类:
为何引出抽象类:
Shape,Animal,....
有的时候基类往往表示一些抽象的概念
作用:
func(),提供统一接口,可以将接口与实现分开chou'xiang
抽象类使用规则:
1.抽象类只能做基类,不能建立实例(不可以创建对象)
2.抽象类不能做参数类型/返回值类型/显式类型转换
3.可以声明抽象类的指针/引用,仅仅是用来指向派生类,实现多态
如何定义:
不可以直接定义
间接:
1.将该类所有构造函数设为private/protected;
*2.在该类中声明纯虚函数,他就是抽象类
virtual <返回值类型> 函数名(<参数列表>)= 0;
举例: class ABS{public: virtual void test()=0;};
纯虚函数使用规则:
1.声明纯虚函数的时候,不可以定义纯虚函数实现部分。
因此在重写纯虚函数之前不能调用;
2.纯虚函数 "=0" 没有实际意义,仅仅表示是一个纯虚函数
3.在定义具有纯虚函数的类的派生类时,必须对纯虚函数重写
如果不重写纯虚函数,那么这个类还是一个抽象类。
#include <iostream>
using namespace std;
class Abs
{
protected:
int a;
public:
Abs(int a)
{
this->a = a;
}
virtual void print()=0;
void fun()
{
cout << "Abs::fun()" << endl;
}
};
class Derived:public Abs
{
protected:
int b;
public:
Derived(int x,int y):Abs(x)
{
b = y;
}
void print()
{
cout << "b:" << b << endl;
}
};
int main()
{
Abs a; //抽象类不可以创建对象
Abs *p; //可以声明抽象类的指针 用来指向派生类,实现多态
p->print();
p->fun(); // 68+70是直接把p看作抽象类实例来使用 错误的
Derived c(1,2);
p = &c; // 用来指向派生类
p->print();
p->fun();
return 0;
}
抽象类实例:
#include <iostream>
using namespace std;
class CPoint
{
protected:
int x;
int y;
public:
CPoint(int i,int j)
{
this->x = i;
this->y = j;
}
virtual void set() = 0;
virtual void draw() = 0;
};
class Line:public CPoint
{
protected:
int p;
int q;
public:
Line(int i,int j,int p,int q):CPoint(i,j)
{
this->p = p;
this->q = q;
}
void set()
{
cout << "line set" << endl;
}
void draw()
{
cout << "line draw" << endl;
}
};
class Plane:public CPoint
{
protected:
int p;
int q;
int m;
int n;
public:
Plane(int i,int j,int p,int q,int m,int n):CPoint(i,j)
{
this->p = p;
this->q = q;
this->m = m;
this->n = n;
}
void set()
{
cout << "PLane set" << endl;
}
void draw()
{
cout << "plane draw" << endl;
}
};
void func_set(CPoint *p)
{
p->set();
}
void func_draw(CPoint *p)
{
p->draw();
}
int main()
{
Line *line = new Line(1,2,3,4);
Plane *plane = new Plane(1,2,3,4,5,6);
func_set(line);
func_set(plane);
func_draw(line);
func_draw(plane);
return 0;
}