-
多态:派生类继承基类,派生类重写(覆盖)基类的虚方法.实现派生类个性.
基类提供统一接口虚函数,派生类都有不同的实现.实现多态必同时满足如下三个条件:
1.必须是公有继承
2.基类的方法必须是虚方法
3.必须是通过基类指针或引用调用基类方法
虚函数:存储在虚表(vtable),派生类继承虚表覆盖虚表的基类虚函数
覆盖(override):派生类重写基类的虚函数,函数形式完全一样(函数名 参数列表都相同)
重载 覆盖 隐藏区别?
重载:同一个类中函数名相同 参数列表不同
隐藏:
a.派生类中重新定义与基类完全一样非虚方法
b.派生类中重新定义与基类同名函数,参数列表不同方法
覆盖:派生类重写基类的虚函数,函数形式完全一样(函数名 参数列表都相同)
#include <iostream>
#include <stdio.h>
using namespace std;
class Base
{
public:
virtual void fun()//定义虚函数, 2.基类的方法必须是虚方法
{
cout<<"Base fun"<<endl;
}
};
class Child :public Base //1.必须是公有继承
{
public:
void fun()
{
cout<<"Child fun"<<endl;
}
};
int main(int argc, char const *argv[])
{
Base *ptr=new Child();//实现多态,调用派生类自身方法
ptr->fun();//3.必须是通过基类指针或引用调用基类方法,结果是调的派生类自身方法。
Child c;
c.fun();
return 0;
}
#include <iostream>
#include <stdio.h>
using namespace std;
class Base
{
public:
virtual void fun()//定义虚函数
{
cout<<"Base fun"<<endl;
}
};
class Child1 :public Base
{
public:
void fun()
{
cout<<"Child1 fun"<<endl;
}
};
class Child2 :public Base
{
public:
void fun()
{
cout<<"Child2 fun"<<endl;
}
};
//1.0和1.1 都是多态 都是覆盖。
//void test(Base& b)//1.0
void test(Base *b)//1.1
{
//b.fun();//1.0
b->fun();//1.1
}
void test2(Base b)
{
b.fun();
}
int main(int argc, char const *argv[])
{
Child1 c1;
Child2 c2;
// test(c1);//1.0 输出Child1 fun
// test(c2);//1.0 输出Child1 fun
test(&c1);//1.1 输出Child1 fun
test(&c2);//1.1 输出Child1 fun
test2(c1); //输出 Base fun 说明是隐藏(赋值兼容问题)
test2(c2); //输出 Base fun 说明是隐藏(赋值兼容问题)
return 0;
}
2.抽象类、纯虚函数
纯虚函数:方法体为0 虚函数,一般定义在基类中.让派生类覆盖
抽象类:至少存在一个纯虚函数的类称为抽象类
抽象类不能实例化对象
派生类没有重写抽闲基类的纯虚函数,派生类也是一个抽象类
抽象类一般是当作基类,指向或引用派生类对象.
#include <iostream>
#include <stdio.h>
using namespace std;
class Base//抽象类
{
public:
virtual void fun()=0;//纯虚函数
};
class Child1 :public Base
{
public:
void fun()
{
cout<<"Child1 fun"<<endl;
}
};
class Child2 :public Base
{
public:
void fun()
{
cout<<"Child2 fun"<<endl;
}
};
int main(int argc, char const *argv[])
{
Base b1;//错误,抽象类不能实例化对象
return 0;
}
3.虚表:存放类的虚函数地址值一维数组.类存在虚函数才存在一个虚表
派生类复制基类虚表
类如果存在虚函数,类的对象分配一个指针,指向存放类虚函数地址集合(虚表)
派生类继承基类,继承基类虚表.派生类对对象也存在一个指针
#include <iostream>
#include <stdio.h>
using namespace std;
class Base
{
public:
virtual void fun()=0;//纯虚函数
virtual void fun1()=0;
};
class Child1 :public Base
{
public:
void fun()
{
cout<<"Child1 fun"<<endl;
}
};
int main(int argc, char const *argv[])
{
cout<<sizeof(Base)<<endl;//输出 4 说明存在一个指针 指向虚表
cout<<sizeof(Child1)<<endl;// 输出 4 说明继承虚表,也有一个指针指向虚表
return 0;
}