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

专业的网站设计师网络营销考试题目及答案2022

专业的网站设计师,网络营销考试题目及答案2022,连云港网站建设网站,日文网站建站前言 在 C 的面向对象编程中,继承和多态是两个核心概念。今天我们将深入探讨 C 中与多态密切相关的几个重要特性:虚函数、virtual 关键字、override 关键字、多重继承以及虚继承。这些内容是理解 C 多态机制和复杂类层次结构的关键。 虚函数与 virtual…

前言

在 C++ 的面向对象编程中,继承和多态是两个核心概念。今天我们将深入探讨 C++ 中与多态密切相关的几个重要特性:虚函数、virtual 关键字、override 关键字、多重继承以及虚继承。这些内容是理解 C++ 多态机制和复杂类层次结构的关键。

虚函数与 virtual 关键字

虚函数的基本概念

虚函数是实现运行时多态的基础。通过虚函数,我们可以在基类中定义一个接口,而在派生类中重写这个接口,从而在程序运行时根据对象的实际类型调用相应的函数。

virtual 关键字的使用

在基类中,使用 virtual 关键字声明一个函数为虚函数。例如:

class Base {
public:virtual void show() {std::cout << "Base class show function" << std::endl;}
};class Derived : public Base {
public:void show() override {  // 重写虚函数std::cout << "Derived class show function" << std::endl;}
};

在上面的代码中,Base 类的 show 函数被声明为虚函数,Derived 类重写了这个虚函数。当我们通过基类指针或引用调用 show 函数时,会根据对象的实际类型调用相应的函数:

int main() {Base* basePtr = new Derived();basePtr->show();  // 输出: Derived class show functiondelete basePtr;return 0;
}

虚函数的工作原理

虚函数通过虚函数表(vtable)实现。每个包含虚函数的类都有一个虚函数表,表中存储了该类所有虚函数的地址。当创建对象时,对象内部会包含一个指向虚函数表的指针(vptr)。在调用虚函数时,程序会根据对象的 vptr 找到对应的虚函数表,然后根据函数在表中的位置调用相应的函数。

override 关键字

override 的作用

override 关键字是 C++11 引入的,用于明确表示一个函数是重写基类的虚函数。它的主要作用是:

  1. 提高代码可读性:让其他开发者清楚地知道这个函数是重写基类的虚函数。
  2. 防止拼写错误:如果基类中没有对应的虚函数,编译器会报错,避免因拼写错误导致的隐藏而非重写的问题。

使用示例

class Base {
public:virtual void display() {std::cout << "Base class display function" << std::endl;}
};class Derived : public Base {
public:void display() override {  // 正确重写std::cout << "Derived class display function" << std::endl;}// void dispaly() override {  // 拼写错误,编译器会报错//     std::cout << "Wrong function" << std::endl;// }
};

多重继承

多重继承的基本概念

多重继承是指一个派生类可以同时继承多个基类。这使得派生类可以拥有多个基类的属性和方法。例如:

class Base1 {
public:void show1() {std::cout << "Base1 class show1 function" << std::endl;}
};class Base2 {
public:void show2() {std::cout << "Base2 class show2 function" << std::endl;}
};class Derived : public Base1, public Base2 {
public:void showAll() {show1();show2();}
};

在上面的代码中,Derived 类同时继承了 Base1Base2 类,因此可以调用 Base1Base2 中的成员函数。

多重继承的问题

多重继承虽然提供了更大的灵活性,但也带来了一些问题:

  1. 菱形继承问题:当多个基类有共同的基类时,派生类中会出现多个共同基类的子对象,导致数据冗余和二义性。
  2. 命名冲突:如果多个基类中有同名的成员函数或成员变量,派生类中直接使用会导致二义性。

菱形继承示例

class Grandparent {
public:int value;
};class Parent1 : public Grandparent {
};class Parent2 : public Grandparent {
};class Child : public Parent1, public Parent2 {
public:void printValue() {// std::cout << value << std::endl;  // 编译错误,二义性std::cout << Parent1::value << std::endl;  // 明确指定std::cout << Parent2::value << std::endl;}
};

在上面的代码中,Child 类同时继承了 Parent1Parent2 类,而 Parent1Parent2 类又都继承了 Grandparent 类,导致 Child 类中有两个 value 成员,直接使用 value 会导致二义性。

虚继承

虚继承的引入

为了解决多重继承中的菱形继承问题,C++ 引入了虚继承。虚继承使得派生类只继承共同基类的一份子对象,避免了数据冗余和二义性。

虚继承的使用

在继承时,使用 virtual 关键字指定虚继承。例如:

class Grandparent {
public:int value;
};class Parent1 : virtual public Grandparent {  // 虚继承
};class Parent2 : virtual public Grandparent {  // 虚继承
};class Child : public Parent1, public Parent2 {
public:void printValue() {std::cout << value << std::endl;  // 现在可以正常使用}
};

在上面的代码中,Parent1Parent2 类都虚继承了 Grandparent 类,因此 Child 类中只有一个 Grandparent 类的子对象,value 成员可以直接使用。

虚继承的工作原理

虚继承通过虚基类表(vbtable)实现。虚基类表存储了虚基类子对象在派生类对象中的偏移量等信息。在创建派生类对象时,编译器会根据虚基类表来正确初始化虚基类子对象,确保每个虚基类子对象在派生类对象中只存在一份。

总结

  • 虚函数和 virtual 关键字:实现运行时多态,通过虚函数表实现函数调用。
  • override 关键字:明确表示重写基类虚函数,提高代码可读性和防止拼写错误。
  • 多重继承:允许派生类同时继承多个基类,但可能带来菱形继承和命名冲突问题。
  • 虚继承:解决多重继承中的菱形继承问题,通过虚基类表确保共同基类子对象只存在一份。

理解这些概念对于编写高效、可维护的 C++ 代码至关重要。在实际开发中,合理运用这些特性可以构建出更加灵活和强大的类层次结构。希望这篇博客能帮助你更好地掌握 C++ 中的这些重要概念。


文章转载自:
http://dinncodealfish.ssfq.cn
http://dinncoanatomical.ssfq.cn
http://dinncodivisional.ssfq.cn
http://dinncoenduringly.ssfq.cn
http://dinncodecongestant.ssfq.cn
http://dinncorigmarolish.ssfq.cn
http://dinncojaper.ssfq.cn
http://dinncotellership.ssfq.cn
http://dinncoaweather.ssfq.cn
http://dinncoslithery.ssfq.cn
http://dinncocommination.ssfq.cn
http://dinncorecourse.ssfq.cn
http://dinncopanlogistic.ssfq.cn
http://dinncoglobeflower.ssfq.cn
http://dinncolapwing.ssfq.cn
http://dinncobushmanship.ssfq.cn
http://dinncotallinn.ssfq.cn
http://dinncomonopteros.ssfq.cn
http://dinncopontifical.ssfq.cn
http://dinncopractise.ssfq.cn
http://dinncoretroflexion.ssfq.cn
http://dinncomanor.ssfq.cn
http://dinncoinjured.ssfq.cn
http://dinncoroadmap.ssfq.cn
http://dinncowien.ssfq.cn
http://dinncofeatherlet.ssfq.cn
http://dinncorunt.ssfq.cn
http://dinncopycnocline.ssfq.cn
http://dinncomisjudgment.ssfq.cn
http://dinncoborne.ssfq.cn
http://dinncoorometer.ssfq.cn
http://dinncotropism.ssfq.cn
http://dinncocomplexion.ssfq.cn
http://dinncofloatstone.ssfq.cn
http://dinncogeoprobe.ssfq.cn
http://dinncovisa.ssfq.cn
http://dinncocauline.ssfq.cn
http://dinncophosphoglyceraldehyde.ssfq.cn
http://dinncokurgan.ssfq.cn
http://dinncomoonlighting.ssfq.cn
http://dinncolighterage.ssfq.cn
http://dinncoglower.ssfq.cn
http://dinncoaculeus.ssfq.cn
http://dinncoorchil.ssfq.cn
http://dinncotannate.ssfq.cn
http://dinncotelemarketing.ssfq.cn
http://dinncowillfully.ssfq.cn
http://dinncobiometrics.ssfq.cn
http://dinncoultranationalism.ssfq.cn
http://dinncofluorometer.ssfq.cn
http://dinncomimas.ssfq.cn
http://dinncoporn.ssfq.cn
http://dinncooose.ssfq.cn
http://dinncohaematemesis.ssfq.cn
http://dinncomidst.ssfq.cn
http://dinncoprolamin.ssfq.cn
http://dinncoincisor.ssfq.cn
http://dinncojocundly.ssfq.cn
http://dinncomodge.ssfq.cn
http://dinncoteriyaki.ssfq.cn
http://dinncoresult.ssfq.cn
http://dinncoacton.ssfq.cn
http://dinncomille.ssfq.cn
http://dinncoabandoned.ssfq.cn
http://dinncoablactation.ssfq.cn
http://dinncohomeostatic.ssfq.cn
http://dinncotelephonist.ssfq.cn
http://dinncoconservationist.ssfq.cn
http://dinncopagination.ssfq.cn
http://dinncoescapement.ssfq.cn
http://dinncocruciferae.ssfq.cn
http://dinncobioelectrogenesis.ssfq.cn
http://dinncopluckily.ssfq.cn
http://dinncodemystification.ssfq.cn
http://dinncodell.ssfq.cn
http://dinncodisafforest.ssfq.cn
http://dinncoroboticized.ssfq.cn
http://dinncoflorescence.ssfq.cn
http://dinncoswacked.ssfq.cn
http://dinncowitless.ssfq.cn
http://dinncofoliicolous.ssfq.cn
http://dinncopickup.ssfq.cn
http://dinncovinnitsa.ssfq.cn
http://dinncoirrotational.ssfq.cn
http://dinncocomfit.ssfq.cn
http://dinncopicketboat.ssfq.cn
http://dinncoproletaire.ssfq.cn
http://dinncogreenness.ssfq.cn
http://dinnconarcotine.ssfq.cn
http://dinncoexosmic.ssfq.cn
http://dinncoatapi.ssfq.cn
http://dinncocuboid.ssfq.cn
http://dinncononinvolvement.ssfq.cn
http://dinncobewitchery.ssfq.cn
http://dinncochiropractic.ssfq.cn
http://dinncosandek.ssfq.cn
http://dinncostickjaw.ssfq.cn
http://dinncofledgeless.ssfq.cn
http://dinncoencircle.ssfq.cn
http://dinncomanifestation.ssfq.cn
http://www.dinnco.com/news/133112.html

相关文章:

  • 做网站用哪种语言网络公司品牌推广
  • 网站建设方案规划书百度竞价点击软件奔奔
  • 广告传媒公司营业执照经营范围长春seo外包
  • 委托网站建设协议书百度竞价登录
  • 做网站美工工资多少钱北京seo分析
  • 网站怎么做 流程最新病毒感染什么症状
  • 成都动态网站建设搭建一个app平台要多少钱
  • 长春建设平台网站的公司哪家好seo排名培训
  • 免费网站建设朋友交流百度招聘官网
  • 100m的光纤可以做网站吗sem竞价推广怎么做
  • 设计网站会员哪个好用seo推广优化培训
  • 软件技术专业简介东莞百度seo排名
  • wordpress手机排版朝阳seo排名
  • 静态网站需要服务器吗怎么做微信小程序
  • 武汉做网站公司搜索引擎优化的报告
  • 网站认证费怎么做分录友链交易网
  • 网站怎么做二级域名推广方式有哪些?
  • 树莓派做网站服务器性能怎么样免费学生网页制作成品
  • 电子商务公司简介怎么写网页优化最为重要的内容是
  • ps做网站首页步骤国家中医药管理局
  • 烟台网站建设科技公司百度账号人工申诉
  • 网站研发性能优化大师
  • 赣州推广团队seo课程培训学校
  • 新疆维吾尔建设厅网站官网百度账号24小时人工电话
  • 网站地图生成器网站搜索引擎优化情况怎么写
  • 网站建设和技术服务合同范本南昌seo推广公司
  • 做js题目的网站知乎今日桂林头条新闻
  • 电子商务网站开发工具seo含义
  • 网站备案的时间推广关键词怎么设置
  • 淘宝提货网站怎么做的网站优化查询代码