外汇直播室都是网站做,海南人,清远市企业网站seo联系方式,赤峰市网站建设培训文章目录 一、public 公有继承 - 示例分析1、类型兼容性原则2、类型兼容性原则应用场景 二、类型兼容性原则 - 示例分析1、父类指针 指向 子类对象2、使用 子类对象 为 父类对象 进行初始化3、完整代码示例 一、public 公有继承 - 示例分析 1、类型兼容性原则 类型兼容性原则 :… 文章目录 一、public 公有继承 - 示例分析1、类型兼容性原则2、类型兼容性原则应用场景 二、类型兼容性原则 - 示例分析1、父类指针 指向 子类对象2、使用 子类对象 为 父类对象 进行初始化3、完整代码示例 一、public 公有继承 - 示例分析 1、类型兼容性原则 类型兼容性原则 : C 的 类型兼容性原则 “ 又称为 ” 赋值兼容性原则 ;
子类代替父类 : 需要 基类 ( 父类 ) 对象的 地方 , 都可以使用 公有继承 的 派生类 ( 子类 ) 对象 替代 , 该 派生类 ( 子类 ) 得到了 除 构造函数 和 析构函数 之外的 所有 成员变量 和 成员方法 ;
功能完整性 : 公有继承 的 派生类 ( 子类 ) 本质上 具有 基类 ( 父类 ) 的 完整功能 , 使用 基类 可以解决的问题 , 使用 公有继承派生类 都能解决 ; 特别注意 : 保护继承 和 私有继承 的 派生类 , 是 不具有 基类 的 完整功能的 , 因为 最终继承 后的派生类 , 无法在 类外部调用 父类的 公有成员 和 保护成员 ; 2、类型兼容性原则应用场景 类型兼容性原则 应用场景 :
直接使用 : 使用 子类对象 作为 父类对象 使用 ;赋值 : 将 子类对象 赋值给 父类对象 ;初始化 : 使用 子类对象 为 父类对象 初始化 ;指针 : 父类指针 指向 子类对象 , 父类指针 值为 子类对象 在 堆内存 的地址 , 也就是 将 子类对象 地址 赋值给 父类类型指针 ;引用 : 父类引用 引用 子类对象 , 将 子类对象 赋值给 父类类型的引用 ; 二、类型兼容性原则 - 示例分析 定义父类 Parent , 子类 Child ;
class Parent {
public:void funParent(){cout 父类 funParent 函数 endl;}private:int c;
};// 子类 公有继承 父类
class Child : public Parent {
public:void funChild() {cout 子类 funChild 函数 endl;}
};1、父类指针 指向 子类对象 定义 一个子类对象 Child child ;
定义父类的指针 , 将 指针 指向 子类对象 的地址 , 这是合法的 ;
代码示例 : // 父类对象Parent parent;// 子类对象Child child;// I. 类型兼容性原则 : 父类指针 指向 子类对象Parent* p_parent2 NULL;p_parent2 child;该原则的应用场景如下 : 定义函数 , 接收 父类指针 或 父类引用 , 此处可以直接传入 子类指针 或 子类引用 ;
// 函数接收父类指针类型
// 此处可以传入子类对象的指针
void fun_pointer(Parent* obj)
{obj-funParent();
}// 函数接收父类引用类型
// 此处可以传入子类对象的引用
void fun_reference(Parent obj)
{obj.funParent();
}2、使用 子类对象 为 父类对象 进行初始化 定义父类对象 , 可以直接使用 子类对象 进行初始化操作 ; // II. 类型兼容性原则 : 使用 子类对象 为 父类对象 进行初始化Parent parent child;3、完整代码示例 #include iostream
using namespace std;class Parent {
public:void funParent(){cout 父类 funParent 函数 endl;}private:int c;
};// 子类 公有继承 父类
class Child : public Parent {
public:void funChild() {cout 子类 funChild 函数 endl;}
};// 函数接收父类指针类型
// 此处可以传入子类对象的指针
void fun_pointer(Parent* obj)
{obj-funParent();
}// 函数接收父类引用类型
// 此处可以传入子类对象的引用
void fun_reference(Parent obj)
{obj.funParent();
}int main() {// 父类对象Parent parent;// 子类对象Child child;// 父类对象 可以调用 父类公有函数parent.funParent();// 子类对象 可以调用 子类自身公有函数child.funChild();// 子类对象 可以调用 父类公有函数child.funParent();// 将指向子类对象的指针传给接收父类指针的函数// 也是可以的fun_pointer(child);// 接收父类引用 , 此处传入子类引用fun_reference(child);// 赋值兼容性原则 : cout \n赋值兼容性原则示例 : \n endl;// 常规操作 : 父类指针 指向 父类对象Parent* p_parent NULL;p_parent parent;// 通过父类指针调用父类函数p_parent-funParent();// 将指向子类对象的指针传给接收父类指针的函数// 也是可以的fun_pointer(p_parent);// 接收父类引用参数fun_reference(*p_parent);// I. 类型兼容性原则 : 父类指针 指向 子类对象Parent* p_parent2 NULL;p_parent2 child;// 通过父类指针调用父类函数p_parent2-funParent();// II. 类型兼容性原则 : 使用 子类对象 为 父类对象 进行初始化Parent parent3 child;// 控制台暂停 , 按任意键继续向后执行system(pause);return 0;
}执行结果 :
父类 funParent 函数
子类 funChild 函数
父类 funParent 函数
父类 funParent 函数
父类 funParent 函数赋值兼容性原则示例 :父类 funParent 函数
父类 funParent 函数
父类 funParent 函数
父类 funParent 函数
Press any key to continue . . . 文章转载自: http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.jtmrx.cn.gov.cn.jtmrx.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.bmmyx.cn.gov.cn.bmmyx.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.ubpsa.cn.gov.cn.ubpsa.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.yrdt.cn.gov.cn.yrdt.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.wrdpj.cn.gov.cn.wrdpj.cn http://www.morning.lfdzr.cn.gov.cn.lfdzr.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn