#include <iostream>

using namespace std;

class Human{
public:
Human(string const& name, int age):m_name(name), m_age(age)
{
cout << "Human构造:" << this << endl;
}
~Human(void)
{
cout << "Human析构:"<< this << endl;
}
void eat(string const& food)
{
cout << "I am eating " << food << '.' << endl;
}
void sleep(string const& loc)
{
cout << "I am sleepping at " << loc << endl;
}
void who(void)
{
cout << "My name is " << m_name << ", I'm " << m_age << " years old." << endl;
}
string m_name;
int m_age;
}; class Student: public Human{
public:
Student(string const& name, int age, int no):Human(name, age), m_no(no)
{
cout << "Student构造:" << this << ' ' << &m_no << endl;
}
~Student(void)
{
cout << "Student析构:"<< this << endl;
}
void learn(string const& course)
{
cout << "I am a student, My no is " << m_no << ", I am learning " << course << '.' << endl;
}
int m_no;
private:
int m_a;
protected:
int m_c; //子类可以访问
}; class Teacher: public Human{
public:
Teacher(string const& name, int age, float salary):Human(name, age), m_salary(salary)
{
cout << "Teacher构造:" << this <<' ' << &m_salary << endl;
}
~Teacher(void)
{
cout << "Teacher析构:"<< this << endl;
}
void teach (string const& course)
{
cout << "I am a teacher, My salary is " << m_salary << ", I'm teaching " << course << '.' << endl;
}
float m_salary;
}; int main(void)
{
Student s1("WJ Zhang", , );
cout << s1.m_name << endl;
cout << s1.m_age << endl;
cout << s1.m_no << endl;
s1.who();
s1.eat("noodle");
s1.sleep("floor");
s1.learn("C++"); Teacher t1("SF Zhang", , );
cout << t1.m_name << endl;
cout << t1.m_age << endl;
cout << t1.m_salary << endl;
t1.who();
t1.eat("chicken");
t1.sleep("sofa");
t1.teach("C++");
cout << sizeof(Human) << endl;
cout << sizeof(Student) << endl;
cout << sizeof(Teacher) << endl;
cout << sizeof(string) << endl;
Human* ph = &s1; //is a ...
cout << ph->m_name << endl;
cout << ph->m_age << endl;
//cout << ph->m_no << endl; //error: ‘class Human’ has no member named ‘m_no’
ph->who();
Student* ps = static_cast<Student*>(ph);
cout <<ps->m_no << endl;
/*导致风险
Human h1("ZR Zhou", 18);
ps = static_cast<Student*> (&h1);
cout << ps->m_no << endl;
ps->learn("123");
*/ /*谨慎慎用对象截切
Human h1 = s1;
cout << h1.m_name << endl;
cout << h1.m_age << endl;
*/ return ;
}

C++_day7_继承的更多相关文章

  1. javaScript的原型继承与多态性

    1.prototype 我们可以简单的把prototype看做是一个模版,新创建的自定义对象都是这个模版(prototype)的一个拷贝 (实际上不是拷贝而是链接,只不过这种链接是不可见,给人们的感觉 ...

  2. JavaScript的继承实现方式

    1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...

  3. javascript中的继承与深度拷贝

    前言 本篇适合前端新人,下面开始...... 对于前端新手来说(比如博主),每当对js的对象做操作时,都是一种痛苦,原因就是在于对象的赋值是引用的传递,并非值的传递,虽然看上去后者赋值给了前者,他们就 ...

  4. 谈谈一些有趣的CSS题目(四)-- 从倒影说起,谈谈 CSS 继承 inherit

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  5. JS继承类相关试题

    题目一: //有关于原型继承的代码如下:function Person(name) {   this.name = name;}Person.prototype = {     getName : f ...

  6. JS继承之寄生类继承

    原型式继承 其原理就是借助原型,可以基于已有的对象创建新对象.节省了创建自定义类型这一步(虽然觉得这样没什么意义). 模型 function object(o){ function W(){ } W. ...

  7. JS继承之借用构造函数继承和组合继承

    根据少一点套路,多一点真诚这个原则,继续学习. 借用构造函数继承 在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术( ...

  8. JS继承之原型继承

     许多OO语言都支持两种继承方式:接口继承和实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在ECMAScript中无法实现接口继承.ECMAScript只支 ...

  9. 深入浅出JavaScript之原型链&继承

    Javascript语言的继承机制,它没有"子类"和"父类"的概念,也没有"类"(class)和"实例"(instanc ...

随机推荐

  1. Excle中的使用小技巧

    关于从数据库中拷贝来的数字,拷贝到excle中,那些数字都变成了科学计算法. 步骤1,鼠标右键选中的列,选择“设置单元格格式(F)” 2.从这里面选中这些是否有小数,如果没有小数就把这个改成0

  2. mysql执行update语句受影响行数是0

    mybatis连接mysql数据库,发现同一个update执行多次,返回的int值都是1. 我记得同样的update再次执行时 受影响行数是0. 后来发现,我之前一直用的SQLyog是这样子的. 原来 ...

  3. .htaccess使用方法介绍

    1..htaccess文件使用前提 .htaccess的主要作用就是实现url改写,也就是当浏览器通过url访问到服务器某个文件夹时,作为主人,我们可以来接待这个url,具体地怎样接待它,就是此文件的 ...

  4. json文件常用代码

    1.json数据内容格式化处理 package com.sklm.lhb.json; public class JsonFormatTool { /** * 单位缩进字符串. */ private s ...

  5. Vue学习之路---No.3(分享心得,欢迎批评指正)

    同样的,我们先来回顾一下昨天学习的内容: 1.利用v-once来组织双向绑定 2.filter{}过滤器的使用(详情请看上一章) 3.computed(计算属性),利用computed属性实现filt ...

  6. openwrt路由器进入安全模式

    openwrt路由器型号:WNDR3800 一.实验背景 在pc机上通过xshell软件登录openwrt路由器,pc机通过网线与openwrt路由器的LAN接口相连.openwrt路由器自带两块无线 ...

  7. Tensorflow object detection API ——环境搭建与测试

    1.开发环境搭建 ①.安装Anaconda 建议选择 Anaconda3-5.0.1 版本,已经集成大多数库,并将其作为默认python版本(3.6.3),配置好环境变量(Anaconda安装则已经配 ...

  8. 2018-2019-2 《网络对抗技术》 Exp1 PC平台逆向破解 20165215

    2018-2019-2 <网络对抗技术> Exp1 PC平台逆向破解 20165215 目录 知识点描述 实验步骤 (一)直接修改程序机器指令,改变程序执行流程 (二)通过构造输入参数,造 ...

  9. MB SD Connect 5 vs 2017 FVDI2 Commander

    Both MB SD C5 and FVDI II are diagnostic and Programmer tools for Mercedes Benz Cars & Trucks.Th ...

  10. mount挂载相关参数详解

    mount   [ -t 设备类型 ]    [ -o 扩展参数 ]   dev   dir   -t:指定mount挂载设备类型,常见的类型有nfs.ntfs.vfat.iso9660等: is09 ...