庆阳市建设局门户网站,深圳宝安区医院,搜索引擎优化与关键词的关系,昌吉州住房和城乡建设局网站文章目录 一、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.kycxb.cn.gov.cn.kycxb.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.cfhwn.cn.gov.cn.cfhwn.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.cgdyx.cn.gov.cn.cgdyx.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.yrbqy.cn.gov.cn.yrbqy.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn