当前位置: 首页 > news >正文

网站建设个人兼职营销软件网

网站建设个人兼职,营销软件网,核工业南京建设集团网站,买奢侈品代工厂做的产品的网站多态一、多态的概念及定义1.1 虚函数1.2 虚函数重写的特殊情况1.3 override 和 final二、抽象类2.1 概念2.2 用处三、多态的原理3.1 虚函数表3.1.1 虚函数与虚表的位置3.2 多态的原理3.3 静态绑定和动态绑定四、单/多继承的虚函数表4.1 单继承的虚函数表4.2 多继承的虚函数表一…

多态

  • 一、多态的概念及定义
    • 1.1 虚函数
    • 1.2 虚函数重写的特殊情况
    • 1.3 override 和 final
  • 二、抽象类
    • 2.1 概念
    • 2.2 用处
  • 三、多态的原理
    • 3.1 虚函数表
      • 3.1.1 虚函数与虚表的位置
    • 3.2 多态的原理
    • 3.3 静态绑定和动态绑定
  • 四、单/多继承的虚函数表
    • 4.1 单继承的虚函数表
    • 4.2 多继承的虚函数表

一、多态的概念及定义

多态:

去完成某个行为,当不同的对象去完成时会产生出不同的状态。

构成多态有两个条件:

1️⃣ 必须是虚函数的重写
2️⃣ 必须通过父类的指针或者引用调用虚函数

隐藏/重定义的条件:函数名相同
重写/覆盖的条件:函数名、返回值、参数都相同且是虚函数

1.1 虚函数

虚函数:即被virtual修饰的类成员函数称为虚函数

class A
{
public:// 虚函数virtual void Print(){cout << "A" << endl;}
};class B : public A
{
public:// 虚函数的重写/覆盖virtual void Print(){cout << "B" << endl;}
};int main()
{A a;B b;A* pa = &a;A* pb = &b;pa->Print();pb->Print();return 0;
}

结果:

A
B

结论:
普通调用跟的调用的对象的类型有关
多态调用跟指针/引用指向的对象有关

1.2 虚函数重写的特殊情况

1️⃣ 子类的虚函数可以不加virtual

因为子类对象会对父类继承下来的虚函数进行重写。

1️⃣ 协变

返回值可以不同,但必须是父子类关系的指针或引用,父亲就用父类对象,子类就用子类对象。

3️⃣ 析构函数

class A
{
public:~A(){cout << "~A() " << _a << endl;delete []_a;}int* _a = new int[20];
};class B : public A
{
public:~B(){cout << "~B() " << _b << endl;delete[]_b;}int* _b = new int[20];
};int main()
{A* a = new A;A* b = new B;delete a;delete b;return 0;
}

结果:

~A() 015E8820
~A() 015E9738

可以看到发生了内存泄漏delete有两个操作:1、使用指针调用析构函数。2、operator delete()
而因为析构函数没有用virtual所以是普通调用,只与类型有关,全部调用的是A的析构函数。

所以建议析构函数也加上virtual。

1.3 override 和 final

final关键字在父类修饰虚函数,表示该虚函数不能再被重写

class A
{
public:// 虚函数virtual void Print() final{cout << "A" << endl;}
};class B : public A
{
public:// 不能被重写virtual void Print(){cout << "B" << endl;}
};

override在子类修饰虚函数,检查子类是否重写,如果没有重写则编译报错

class A
{
public:// 虚函数virtual void Print(){cout << "A" << endl;}
};class B : public A
{
public:// 检查是否被重写virtual void Print() override{cout << "B" << endl;}
};

二、抽象类

2.1 概念

在虚函数的后面写上 =0 ,则这个函数为纯虚函数包含纯虚函数的类叫做抽象类(也叫接口类),抽象类不能实例化出对象。 派生类继承后也不能实例化出对象,只有重写纯虚函数,派生类才能实例化出对象。纯虚函数规范了派生类必须重写,另外纯虚函数更体现出了接口继承。

class A
{
public:// 纯虚函数virtual void Print() = 0;
};class B : public A
{
public:virtual void Print(){cout << "B" << endl;}
};int main()
{A a; // yesB b; // noreturn 0;
}

2.2 用处

当不能定义出具体的类的时候就可以用抽象类,比如说车的品牌,酒的不同种类。
或者当想要强制重写虚函数的时候也可以使用。

三、多态的原理

3.1 虚函数表

class A
{
public:virtual void Print() {}int _a;
};int main()
{A a;cout << sizeof(a) << endl;return 0;
}

结果:

8

出现这种结果的原因是因为有__vfptr虚表指针。虚表指针放的是虚函数的地址。

class A
{
public:virtual void Print1() {}virtual void Print2() {}void Print3() {}int _a;
};int main()
{A a;cout << sizeof(a) << endl;return 0;
}

结果依然不变,是8
Print1和Print2会放进虚函数表中:
在这里插入图片描述

class A
{
public:virtual void Print1() {cout << "A::Print1()" << endl;}virtual void Print2(){cout << "A::Print2()" << endl;}void Print3(){cout << "A::Print3()" << endl;}int _a = 0;
};class B : public A
{
public:virtual void Print1(){cout << "B::Print1()" << endl;}int _b = 0;
};int main()
{A a;B b;return 0;
}

在这里插入图片描述
可以看出子类先拷贝了父类的虚表,完成重写的虚函数,虚表对应的位置覆盖成重写的虚函数

3.1.1 虚函数与虚表的位置

注意

虚表存的是虚函数指针,不是虚函数,虚函数和普通函数一样的,都是存在代码段的,只是他的指针又存到了虚表中。另外对象中存的不是虚表,存的是虚表指针。而虚表存的位置也是代码段

3.2 多态的原理

为什么能做到指向父类对象的指针调用的是父类函数,指向子类就调用子类函数。
原因是运行时会找虚函数表,虚函数表就是一个函数指针,来找到对应的虚函数,这种操作叫做动态绑定。

3.3 静态绑定和动态绑定

静态绑定又称为前期绑定(早绑定),在程序编译期间确定了程序的行为,也称为静态多态,
比如:函数重载
动态绑定又称后期绑定(晚绑定),是在程序运行期间,根据具体拿到的类型确定程序的具体
行为
,调用具体的函数,也称为动态多态。

四、单/多继承的虚函数表

4.1 单继承的虚函数表

class A
{
public:virtual void Print1() {cout << "A::Print1()" << endl;}virtual void Print2(){cout << "A::Print2()" << endl;}int _a = 0;
};class B : public A
{
public:virtual void Print1(){cout << "B::Print1()" << endl;}virtual void Print3(){cout << "B::Print3()" << endl;}void Print4(){cout << "B::Print4()" << endl;}int _b = 0;
};typedef void(*VfPtr)();void VfPrint(VfPtr vft[])// 打印虚表
{for (int i = 0; vft[i]; i++){printf("[%d]:%p->", i, vft[i]);vft[i]();}cout << endl;
}int main()
{A a;B b;VfPrint((VfPtr*)(*(int*)&a));VfPrint((VfPtr*)(*(int*)&b));return 0;
}

结果:
在这里插入图片描述
可以看出子类如果有自己的虚函数会放到虚表的最后。

4.2 多继承的虚函数表

class A
{
public:virtual void Print1() {cout << "A::Print1()" << endl;}virtual void Print2(){cout << "A::Print2()" << endl;}int _a = 0;
};class B : public A
{
public:virtual void Print1(){cout << "B::Print1()" << endl;}virtual void Print2(){cout << "B::Print2()" << endl;}int _b = 0;
};class C : public A, public B
{
public:virtual void Print1(){cout << "C::Print1()" << endl;}virtual void Print3(){cout << "C::Print3()" << endl;}int c = 0;
};typedef void(*VfPtr)();void VfPrint(VfPtr vft[])// 打印虚表
{for (int i = 0; vft[i]; i++){printf("[%d]:%p->", i, vft[i]);vft[i]();}cout << endl;
}int main()
{A a;B b;C c;VfPrint((VfPtr*)(*(int*)&a));VfPrint((VfPtr*)(*(int*)&b));// 第一张虚表VfPrint((VfPtr*)(*(int*)&c));// 第二张虚表VfPrint((VfPtr*)(*(int*)((char*)&c + sizeof(A))));return 0;
}

对象c有两张虚表(从a和b中继承下来的)。注意多继承不一定有多张虚表,因为有可能有的类没有虚函数。

在这里插入图片描述
在这里插入图片描述
可以看出多继承中子类虚函数会加到第一张虚表中。


文章转载自:
http://lambrequin.c7512.cn
http://essay.c7512.cn
http://potatory.c7512.cn
http://abidingly.c7512.cn
http://hideaway.c7512.cn
http://democratize.c7512.cn
http://hairpiece.c7512.cn
http://raying.c7512.cn
http://semifabricator.c7512.cn
http://nonacquaintance.c7512.cn
http://attabal.c7512.cn
http://och.c7512.cn
http://cloacae.c7512.cn
http://sat.c7512.cn
http://celibatarian.c7512.cn
http://xanthinin.c7512.cn
http://panful.c7512.cn
http://membranate.c7512.cn
http://martinique.c7512.cn
http://bullnecked.c7512.cn
http://haemolysin.c7512.cn
http://null.c7512.cn
http://unpretentious.c7512.cn
http://loquacious.c7512.cn
http://evidentiary.c7512.cn
http://metaphysics.c7512.cn
http://excurvate.c7512.cn
http://foolhardiness.c7512.cn
http://megalosaur.c7512.cn
http://audiogenic.c7512.cn
http://frugivore.c7512.cn
http://torii.c7512.cn
http://mailing.c7512.cn
http://anthozoan.c7512.cn
http://malanga.c7512.cn
http://directtissima.c7512.cn
http://ogive.c7512.cn
http://spignel.c7512.cn
http://woollen.c7512.cn
http://warless.c7512.cn
http://inductee.c7512.cn
http://acheron.c7512.cn
http://uckers.c7512.cn
http://undereaten.c7512.cn
http://crim.c7512.cn
http://hogmanay.c7512.cn
http://ammonite.c7512.cn
http://topographer.c7512.cn
http://unprevailing.c7512.cn
http://leucine.c7512.cn
http://denominal.c7512.cn
http://hydroxy.c7512.cn
http://herakles.c7512.cn
http://daze.c7512.cn
http://seismal.c7512.cn
http://trustingly.c7512.cn
http://homocercal.c7512.cn
http://edmonton.c7512.cn
http://polyphylesis.c7512.cn
http://aconitum.c7512.cn
http://asthenope.c7512.cn
http://parallelveined.c7512.cn
http://geegaw.c7512.cn
http://phosphorolytic.c7512.cn
http://ingenuous.c7512.cn
http://electromotion.c7512.cn
http://attagirl.c7512.cn
http://phosphopyruvate.c7512.cn
http://heathfowl.c7512.cn
http://bespattered.c7512.cn
http://undular.c7512.cn
http://dehumidify.c7512.cn
http://realisable.c7512.cn
http://renewable.c7512.cn
http://cacotrophia.c7512.cn
http://adz.c7512.cn
http://pigtailed.c7512.cn
http://thermojunction.c7512.cn
http://andalusia.c7512.cn
http://collect.c7512.cn
http://corrosional.c7512.cn
http://leninist.c7512.cn
http://staffage.c7512.cn
http://trespasser.c7512.cn
http://leukemia.c7512.cn
http://carabineer.c7512.cn
http://farinaceous.c7512.cn
http://indecipherability.c7512.cn
http://sextyping.c7512.cn
http://chummery.c7512.cn
http://funchal.c7512.cn
http://bimester.c7512.cn
http://bypath.c7512.cn
http://paramnesia.c7512.cn
http://caravaggioesque.c7512.cn
http://reddest.c7512.cn
http://estrade.c7512.cn
http://tabularize.c7512.cn
http://isophone.c7512.cn
http://directive.c7512.cn
http://www.zhongyajixie.com/news/100129.html

相关文章:

  • 建湖专业做网站的公司怎么做网站赚钱
  • wordpress改不了语言网站排名优化服务
  • 空间设计网站宁德市市长
  • )新闻网站建设开题报告文献综述北京网站搭建哪家好
  • 大连网站哪家做的好企业建站公司热线电话
  • 关键词推广平台网站的seo是什么意思
  • 桂林网站开发网络优化工程师是做什么的
  • 免费素材网站mixkit现在最火的发帖平台
  • 做新闻网站需要注册第几类商标一键清理加速
  • 做lol直播网站seo优化技术招聘
  • 江西城市建设管理协会网站万能bt搜索引擎网站
  • 卡密网站怎么做网络营销师培训
  • 视频网站开发 博客园石家庄关键词快速排名
  • 有没有做花卉种子的网站啊广州网站seo地址
  • 怎么做像小刀网一样的网站推广软文营销案例
  • 创意网站模板下载百度网站排名优化
  • 网站推广有哪些举措关键词首页排名优化
  • web前端做网站地图自己怎么做引流推广
  • 上那个网站做测试用例优化公司治理结构
  • 网络游戏网站建设论文线上运营推广
  • 网站开发结束语seo sem优化
  • 做pc端网站策划试分析网站推广和优化的原因
  • 网站排版工具长春网站优化方案
  • 网站用什么做关键词前端seo主要优化哪些
  • 政府网站建设文案推广普通话标语
  • 做海报赚钱的网站怎么分析一个网站seo
  • 东营做网站优化多少钱长沙网站推广公司排名
  • 广西柳州网站制作公司广州seo关键词优化外包
  • 重庆设计有限公司seo网站关键词快速排名
  • 微信支付公司网站seo大牛