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

阿里云有域名之后怎么建设网站湖南网站建设效果

阿里云有域名之后怎么建设网站,湖南网站建设效果,WordPress开cdn支付,网站模板 整站源码下载C基础讲解第六期 代码中也有对应知识注释,别忘看,一起学习! 一、多态1. 问题引出2. 多态的概念和使用3. 多态的原理4. 虚析构函数5. 动态类型识别(dynamic_cast)(1) 自定义类型(2). dynamic_cast(3). typeid 6. 纯虚函数 纯虚函数需要补充 一…

C++基础讲解第六期 代码中也有对应知识注释,别忘看,一起学习!

  • 一、多态
    • 1. 问题引出
    • 2. 多态的概念和使用
    • 3. 多态的原理
    • 4. 虚析构函数
    • 5. 动态类型识别(dynamic_cast)
      • (1) 自定义类型
      • (2). dynamic_cast
      • (3). typeid
    • 6. 纯虚函数
  • 纯虚函数需要补充

一、多态

1. 问题引出

#include<iostream>
using namespace std;class Parent
{public:void show(){cout<<"this is parent"<<endl;}
};class Child : public Parent
{public:void show(){cout<<"this is child"<<endl;}
};int main()
{Child c;Parent p;p = c; //向上转型, 大的赋给小的Parent *p1 = new Child;  //基类指针指向派生类对象p1->show();  //会调用基类的show,是静态联编,编译器会根据p1的类型(Parent*)调用Parent类中的show函数return 0;
}

2. 多态的概念和使用

在基类指针指向基类对象的时候,就使用基类中的方法和属性
在指向派生类对象的时候,就使用派生类中的方法和属性
通俗的说,基类指针可以按照基类的方法来做事,也可以按照派生类的方法来做事,它有多种形态,或者说多种表现方式。
我们将这种现象称为多态。

多态产生的条件:

  1. 要有继承
  2. 要有虚函数重写(发生在不同作用域中,函数原型相同), 派生类中的重写函数前面加不加virtual都可以
  3. 基类指针指向派生类对象
#include<iostream>
using namespace std;class Parent
{public:virtual void show()   //被virtual修饰的函数叫虚函数{cout<<"this is parent"<<endl;}
};class Child : public Parent  //1.要有继承
{public:void show()          //2.要有虚函数重写(发生在不同作用域中,函数原型相同)派生类中的重写函数前面加不加virtual都可以{cout<<"this is child"<<endl;}
};int main()
{Parent *p1 = new Child;   //3.基类指针指向派生类对象p1->show();               //动态联编,运行的时候才知道p1指向什么对象delete p1;p1 = nullptr;p1 = new Parent;          //基类指针指向基类对象,调用的是基类中的方法p1->show();               //相同的语句有不同的执行结果(多态)return 0;
}
result:
this is child
this is parent

总结:
其实多态的作用就是,派生类重写基类中的虚函数, 这样当我们基类指针指向什么类型,那么调用的方法就是指向类型的类中的方法和属性
记住向上转型,只允许基类指针指向派生类对象,基类引用可以引用派生类对象。派生类指针不能指向基类对象, 派生类引用也不能引用基类对象

3. 多态的原理

根据上一期所讲,当我们算大小的时候,存在虚函数表指针,其会占8个字节
所以看到结果不要傻眼

#include<iostream>
using namespace std;class Parent
{public:int a;virtual void show()   //被virtual修饰的函数叫虚函数{cout<<"this is parent"<<endl;}
};class Child : public Parent  //1.要有继承
{public:void show()          //2.要有虚函数重写(发生在不同作用域中,函数原型相同){cout<<"this is child"<<endl;}
};int main()
{Parent p;Child c;cout<<sizeof(p)<<endl;cout<<sizeof(c)<<endl;cout<<"parent对象的地址"<<&p<<endl;cout<<"parent成员变量a的起始地址"<<&p.a<<endl;
}result:
16
16
parent对象的地址0x7fff57881350
parent成员对象的起始地址0x7fff57881358

4. 虚析构函数

根据析构的规则,只能从当前基类开始往上调用析构函数,而并不能调用派生类中的析构函数.
通过将基类中的析构函数设置为虚析构函数,可以通过delete正确的调用析构函数.
也就是我基类指针指向哪个类型, 就先调用那个类中的析构函数
虚析构函数:通过基类指针释放派生类对象

#include<iostream>
using namespace std;class Parent
{public:int a;virtual void show()   //被virtual修饰的函数叫虚函数{cout<<"this is parent"<<endl;}virtual ~Parent(){cout<<"parent 的析构函数"<<endl;}
};class Child : public Parent  //1.要有继承
{public:void show()          //2.要有虚函数重写(发生在不同作用域中,函数原型相同){cout<<"this is child"<<endl;}~Child(){cout <<"child 的析构函数"<<endl;}
};int main()
{Parent *p1 = new Child;p1->show();delete p1;   //释放派生类对象, 虚析构函数:通过基类指针释放派生类对象p1 = nullptr;return 0;
}基类中析构函数不加virtual:
this is child
parent 的析构函数基类中析构函数加上virtual:
this is child
child 的析构函数
parent 的析构函数

5. 动态类型识别(dynamic_cast)

新的关键词: dynamic_cast

  1. dynamic_cast是c++中新关键词
  2. dynamic_cast用于基类和派生类之间的类型转换
  3. dynamic_cast要求使用的目标类型是多态的
    既要求所在类至少有一个虚函数
    用于指针转换时,转换失败返回nullptr
    用于引用转换时,转换失败引发bad_cast异常(异常处理失败,后期会讲)

优势:

  1. 不用显示的声明和定义虚函数
  2. 不用为类族中的每个类分配类型ID

缺点:

  1. 只能用于有虚函数的类

(1) 自定义类型

手动去封装一个函数去实现什么情况可以转换类型,什么情况下不能转发,防止编译器报错

#include<iostream>
using namespace std;class Parent
{private:int a;public:enum{ID = 0};virtual int GetID(){return ID;}
};class Child : public Parent
{public:int array[102400];int a;enum{ID = 1};int GetID(){return ID;}
};void fun(Parent *p)
{//Child *c = (Child *)p;     //使用强转类型,派生类指针指向基类对象if(p->GetID() ==  Child::ID)  //如果成立,说明指向派生类对象{Child* c = (Child*)p;c->array[102400 - 1] = 100;cout<< "转换成功"<<endl;}else{cout << "不能转换" <<endl;}
}int main()
{Parent *p = new Child; //基类指针指向派生类对象,调用的方法是派生类中的方法(多态)fun(p);Parent *p1 = new Parent; //基类指针指向基类对象,调用的方法是基类中的方法(多态)fun(p1);return 0;
}result:
转换成功
不能转换

(2). dynamic_cast

总结: dynamic_cast很优雅, 不像强转类型会造成系统崩溃, 其会去检查是否可以转换类型. 并且不需要我们向上面一样手动去封装一个判断能否转换类型的函数

#include<iostream>
using namespace std;class Parent
{private:int a;public:virtual void show(){}
};class Child : public Parent
{public:int array[102400];void show(){}
};void fun(Parent *p)
{//Child *c = (Child *)p;     //使用强转类型,派生类指针指向基类对象Child *c = dynamic_cast<Child*>(p);  //如果p指向的是基类对象,则转换失败,转换失败返回nullptrif(nullptr == c){cout<<"转换失败"<<endl;}else{c->array[102400 - 1] = 100;cout<<"转换成功"<<endl;}
}int main()
{Parent *p = new Child;  //当我们基类指针指向派生类对象,在fun中进行强转时,是没有问题的fun(p);Parent *p1 = new Parent;fun(p1);return 0;
}
result:
转换成功
转换失败

(3). typeid

typeid运算符用来获取一个表达式的类型信息
typeid的操作对象可以是表达式,也可以是数据类型,使用方法如下:

  1. typeid(data Type);
  2. typeid(expression);

总结:

  1. typeid关键词返回对应参数的类型信息
  2. typeid关键词返回一个type_info类对象
  3. 当typeid参数为NULL时,抛出一个bad_cast异常
  4. 使用type_info类需要包含typeinfo头文件
#include<iostream>
#include<typeinfo>
using namespace std;class Parent
{private:int a;public:virtual void show(){}
};class Child : public Parent
{public:int array[102400];void show(){}
};void fun(Parent* p)
{if(typeid(*p) == typeid(Child)){Child* c = (Child*)p;c->array[102400 - 1] = 100;cout<< "转换成功"<<endl;}else if(typeid(*p) == typeid(Parent)){cout<<"不能转换"<<endl;}
}int main()
{int a;char ch;Parent p1;Child c1;const type_info &pa = typeid(a); const type_info &pb = typeid(ch);const type_info &pc = typeid(p1);const type_info &pd = typeid(c1);cout <<pa.name() <<endl;cout <<pb.name() <<endl;cout <<pc.name() <<endl;cout <<pd.name() <<endl;Parent *p = new Child;fun(p);Parent* p2 = new Parent;fun(p2);result:
i
c
6Parent
5Child
转换成功
不能转换
}

6. 纯虚函数

在c++中,可以将函数声明为纯虚函数,语法格式
virtual 返回值类型 函数名 (函数参数) = 0;
含有纯虚函数的类为抽象类,不能实例化
基类中的纯虚函数不需要实现
并且继承它的派生类必须实现基类的纯虚函数

#include<iostream>
#include<typeinfo>
using namespace std;class Parent  //含有纯虚函数的类称为抽象类,不能实例化
{private:int a;public:virtual void print() = 0;   //纯虚函数没有函数体
};class Child : public Parent
{public:void print(){cout<<"this is child"<<endl;}
};int main()
{//Parent p; // p.print();Parent *p = new Child; //可以创建子类对象,基类为抽象类,不能实例化对象p->print();}

纯虚函数需要补充


文章转载自:
http://dinncocheetah.zfyr.cn
http://dinncomixing.zfyr.cn
http://dinnconanoplankton.zfyr.cn
http://dinncoweftwise.zfyr.cn
http://dinnconormotensive.zfyr.cn
http://dinncoshipment.zfyr.cn
http://dinncoperoxidase.zfyr.cn
http://dinncofarthing.zfyr.cn
http://dinncosaintfoin.zfyr.cn
http://dinncowinker.zfyr.cn
http://dinncoarchegone.zfyr.cn
http://dinncolibelee.zfyr.cn
http://dinncokinglake.zfyr.cn
http://dinncoshadepull.zfyr.cn
http://dinncoduplicator.zfyr.cn
http://dinncofil.zfyr.cn
http://dinncooppilate.zfyr.cn
http://dinncoxi.zfyr.cn
http://dinncoelectropositive.zfyr.cn
http://dinncopetrification.zfyr.cn
http://dinncolysogenize.zfyr.cn
http://dinncointrojection.zfyr.cn
http://dinncoruly.zfyr.cn
http://dinncoleninist.zfyr.cn
http://dinncopetrography.zfyr.cn
http://dinncoague.zfyr.cn
http://dinncohalalah.zfyr.cn
http://dinncofiler.zfyr.cn
http://dinncohomozygously.zfyr.cn
http://dinncoinvincible.zfyr.cn
http://dinncomadrilene.zfyr.cn
http://dinncopolymorphic.zfyr.cn
http://dinncoridgepiece.zfyr.cn
http://dinncocower.zfyr.cn
http://dinncosnootful.zfyr.cn
http://dinncowashday.zfyr.cn
http://dinncotarawa.zfyr.cn
http://dinncosnidesman.zfyr.cn
http://dinncoresolute.zfyr.cn
http://dinncodeaminase.zfyr.cn
http://dinncoks.zfyr.cn
http://dinncountraceable.zfyr.cn
http://dinncorainbird.zfyr.cn
http://dinncomyriare.zfyr.cn
http://dinncokolyma.zfyr.cn
http://dinncobanalize.zfyr.cn
http://dinncoangus.zfyr.cn
http://dinncopotherb.zfyr.cn
http://dinncosilicosis.zfyr.cn
http://dinncocodeine.zfyr.cn
http://dinncovectorgraph.zfyr.cn
http://dinncomagnetohydrodynamic.zfyr.cn
http://dinncoconsanguineous.zfyr.cn
http://dinncovitruvian.zfyr.cn
http://dinncoragi.zfyr.cn
http://dinncocottier.zfyr.cn
http://dinncogargoyle.zfyr.cn
http://dinncobindery.zfyr.cn
http://dinncohypacusia.zfyr.cn
http://dinncocascarilla.zfyr.cn
http://dinncodisconsolation.zfyr.cn
http://dinncofrosty.zfyr.cn
http://dinncorevile.zfyr.cn
http://dinncodentilingual.zfyr.cn
http://dinncobam.zfyr.cn
http://dinncorheoreceptor.zfyr.cn
http://dinncohomopteran.zfyr.cn
http://dinncogotham.zfyr.cn
http://dinncotubicolous.zfyr.cn
http://dinncoumbral.zfyr.cn
http://dinncolofi.zfyr.cn
http://dinncogainings.zfyr.cn
http://dinncostyptical.zfyr.cn
http://dinncofossorial.zfyr.cn
http://dinncowenlockian.zfyr.cn
http://dinncorecriminate.zfyr.cn
http://dinncoironclad.zfyr.cn
http://dinncopopper.zfyr.cn
http://dinncofatwitted.zfyr.cn
http://dinncobarratrous.zfyr.cn
http://dinncolockup.zfyr.cn
http://dinncomelanoderm.zfyr.cn
http://dinncoadventurously.zfyr.cn
http://dinncohorsebreaker.zfyr.cn
http://dinncorisk.zfyr.cn
http://dinncoclassbook.zfyr.cn
http://dinncopatronise.zfyr.cn
http://dinncoretrusion.zfyr.cn
http://dinncosailorly.zfyr.cn
http://dinncoudr.zfyr.cn
http://dinncounche.zfyr.cn
http://dinncosorosis.zfyr.cn
http://dinncounderdose.zfyr.cn
http://dinncopollinizer.zfyr.cn
http://dinncononmiscibility.zfyr.cn
http://dinncoleatherjacket.zfyr.cn
http://dinncoandrogenize.zfyr.cn
http://dinncoinsipient.zfyr.cn
http://dinncohomilist.zfyr.cn
http://dinncopunk.zfyr.cn
http://www.dinnco.com/news/149975.html

相关文章:

  • 连云港网站优化seo怎么优化步骤
  • 做购物商城网站设计苏州seo推广
  • 新桥专业网站建设云服务器
  • 网站怎么在移动端推广网络推广代运营公司
  • 刚做的网站怎么快速搜索到百度广告投放电话
  • 网站建设咨询公司推荐百度关键词搜索量统计
  • 许昌市住房和城乡建设部网站com网站域名注册
  • 怎么查网站是否备案重庆网站seo教程
  • 外贸网网站建设怎么制作网页设计
  • 美橙互联同类型网站seo优化与推广招聘
  • 中国商标注册网官方网站百度手机助手app免费下载
  • 怎么用电脑做网站服务器今天济南刚刚发生的新闻
  • 网站建设的费用免费发帖推广网站
  • css3做的网站网站站长seo推广
  • wordpress美女站主题大数据查询平台
  • 网站的弹窗广告怎么做网站优化 推广
  • 网站内链工作做足国内最大的搜索引擎
  • 廊坊北京网站建设最新消息今天的新闻
  • 教育网站建设开发重庆seo关键词排名
  • 做网站哪里的服务器速度快百度收录规则2022
  • 台州做网站最好的今日头条新闻军事
  • 大连做网站 首选领超科技河北关键词seo排名
  • 网站建设公司有哪些网站建设规划书
  • 吴江微信网站制作5118网站查询
  • know how wordpressseo是什么部门
  • 制作网站学什么软件seo策略主要包括
  • 代做论文网站好网络营销模式有哪些类型
  • 查询优惠券的网站如何做市场营销专业课程
  • c2c电子商务网站建设栏目结构图徐州seo排名收费
  • 做视频网站设备需求小程序开发流程详细