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

跟我一起做网站pdf电驴网站建设案例模板下载

跟我一起做网站pdf电驴,网站建设案例模板下载,网络解决方案公司,微信小程序怎么添加到桌面11.1 动态类识别 11.1.1 自定义类型 C中的多态根据实际的对象类型调用对应的函数 #xff08;1#xff09;可以在基类中定义虚函数返回具体的类型信息 #xff08;2#xff09;所有的派生类都必须实现类型相关的虚函数 #xff08;3#xff09;每个类中的类型虚函数都需…11.1 动态类识别 11.1.1 自定义类型 C中的多态根据实际的对象类型调用对应的函数 1可以在基类中定义虚函数返回具体的类型信息 2所有的派生类都必须实现类型相关的虚函数 3每个类中的类型虚函数都需要不同的实现 使用虚函数进行动态类型识别的缺陷 1必须从基类开始提供类型虚函数 2所有派生类都必须重写类型虚函数 3每个派生类的ID必须唯一 示例代码 #include iostreamusing namespace std;class Parent { private:int a; public:enum {ID 0};virtual int getID(){return ID; } };class Child : public Parent { public:enum {ID 1};int array[102400];virtual int getID(){return ID;}};void f(Parent *p) {//Child *c (Child *)p; //派生类指针指向基类对象 //如果指针指向派生类对象可以转换指向基类对象不能转换if (p-getID() Child::ID) //如果成立说明指向派生类对象{Child *c (Child *)p;c-array[102400 - 1] 100;}else{cout 不能转换 endl;} }int main() {//Parent *p new Child;Parent *p new Parent;f(p);return 0; }运行结果 11.1.2 dynamic_cast 新的关键字 dynamic_cast 1dynamic_cast是C中的新型关键字 2dynamic_cast用于基类和派生类之间的转换 3dynamic_cast要求使用的目标类型是多态的 即要求所在类族至少有一个虚函数 用于指针转换时转换失败返回空指针 用于引用转换时转换失败将引发bad_cast异常 dynamic_cast的优势 1不用显示的声明和定义虚函数 2不用为类族中的每个类分配类型ID dynamic_cast的缺陷 只能用于有虚函数的类族 示例代码 #include iostreamusing namespace std;class Parent { private:int a; public:virtual void show(){} };class Child : public Parent { public:int array[102400];void show(){} };void f(Parent *p) {Child *c dynamic_castChild *(p); //如果p指向的是基类对象则转换失败转换失败返回NULLif (NULL c){cout 转换失败 endl;}else{cout 转换成功 endl;c-array[102400 - 1] 100;} }int main() {//Parent *p new Child;Parent *p new Parent;f(p);return 0; }运行结果 11.1.3 typeid C提供了typeid关键字用于动态获取类型信息 1typeid关键字返回对应参数的类型信息 2typeid关键字返回一个type_info类对象当typeid参数为NULL时抛出bad_typeid异常 3type_info类的使用需要包含typeinfo头文件typeid的使用 示例代码 #include iostream #include typeinfousing namespace std;class Parent { private:int a; public:virtual void show(){} };class Child : public Parent { public:int array[102400]; public:void show(){} };void f(Parent *p) {if (typeid(*p) typeid(Child)){cout 可以转换 endl;Child *c (Child *)p; //派生类指针指向基类对象c-array[102400 - 1] 100;}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 pch typeid(ch); const type_info pp1 typeid(p1); const type_info pc1 typeid(c1); cout pa.name() endl;cout pch.name() endl;cout pp1.name() endl;cout pc1.name() endl;//if (pa.name() i) // 不同编译器可以不一样不能这么写Parent *p new Parent;//Parent *p new Child;f(p);return 0; }运行结果 11.2 纯虚函数和抽象类 11.2.1 基本概念 抽象类 1含有纯虚函数的类 2抽象类不能用于直接创建对象实例可以声明抽象类的指针和引用 3可使用指向抽象类的指针支持运行时多态性 4派生类中必须实现基类中的纯虚函数否则它仍将被看作一个抽象类 示例代码 #include iostreamusing namespace std;class Parent //含有纯虚函数的类称为抽象类 抽象类不能创建对象 { public:void show(){cout this is parent endl;}virtual void print() 0; //纯虚函数 没有函数体 };class Child : public Parent { public:void print(){cout this is Child endl;} };int main() {//Parent pp;//pp.print();Parent *p new Child;p-print();return 0; }运行结果
http://www.w-s-a.com/news/350468/

相关文章:

  • 做古代风格头像的网站对网站政务建设的建议
  • 网站搜索栏怎么做设计个网站要多少钱
  • 阿里巴巴网站建设目标wamp wordpress
  • 自己做的网站怎么挂网上金蝶erp
  • 网站的页面由什么组成淘宝网网站建设的需求分析
  • 软文网站推广法dede5.7内核qq个性门户网站源码
  • 个人备案网站名称校园网站建设特色
  • vr超市门户网站建设班级网站怎么做ppt模板
  • 网站建设一般是用哪个软件刚开始做写手上什么网站
  • 用jsp做的网站源代码下载有哪些做红色旅游景点的网站
  • 网站开发的技术选型黄石市网站建设
  • 做直播网站需要证书吗专做宝宝的用品网站
  • 网站标题用什么符号网站制作交易流程
  • dede模板网站教程jsp网站搭建
  • 上海网站开发外包公司鲜花导购网页制作
  • 宿州外贸网站建设公司个人注册网站一般做什么
  • 小公司做网站用哪种服务器什么是网站代理
  • 青岛李村网站设计公司cms建站平台
  • 做saas网站可行吗许昌抖音推广公司
  • 网站建设找谁做seo基础知识培训
  • 微网站怎么做的好建设网站不会写代码
  • 广州外贸网站制作wordpress信息搜索插件
  • 福建高端网站建设个人公众号怎么制作教程
  • 企业网站有哪些举几个例子wordpress ie兼容插件
  • 高端的深圳网站页面设计福清市建设局官方网站
  • 安装网站到服务器合肥建设干部学校网站
  • 影视网站如何做销售案例网站
  • 建设网站对比方案龙岗网站开发公司
  • 网站开发标准网站建设公司兴田德润可信赖
  • 如何建设一个公众号电影网站自动seo优化