#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. 使用commons.cli实现MyCP

    目录 Commons.cli库 MyCP 测试代码 总结 Commons.cli库 考虑到这次的任务是实现自己的命令行命令cp,我认为简单地使用args[]无法很好的完成需求.经过网上的一番搜索,我找 ...

  2. Jsoup解析XML

    先导入jsoup.jar  包 方法1:不推荐,了解即可 方法 方法3: 后期学习主流

  3. log4cplus在Linux下编译及使用

    log4cplus第一次在windows下使用的时候很快就完成了,最近在Linux下尝试使用时遇到了不少问题,主要原因是对Linux的编译连接不熟悉,以下就记录安装使用的过程,希望对需要的人有所帮助. ...

  4. echarts 中国地图 数据自动提示

    mounted() { // 首先每种图是根据series -->type决定的 eg:bar--柱状图 line 折线图 pie饼图 map地图等等三部曲 跟着走 // @第一步都是初始化 v ...

  5. cmd中查看MySQL数据库表数据及结构

    0. 1 .cmd进入mysql安装的bin目录(C:\Program Files\XXXXXX\MySQL Server 5.6\bin) mysql -hlocalhost -uroot -p 回 ...

  6. MFC消息 OnCtlColor 改变控件颜色

    OnCtlColor 有以下几个宏定义 #define CTLCOLOR_MSGBOX         0    #define CTLCOLOR_EDIT           1    #defin ...

  7. 爬虫 -----爬取百度时事热点和url

    使用scrapy top.py    爬虫主要工作 pipelines.py    数据保存 main.py   执行脚本 items.py   初始化item

  8. [virtualbox] win10与centos共享目录下,nginx访问问题

    原文,http://blog.csdn.net/zhezhebie/article/details/73554872 virtualbox自动挂载之后,默认是挂载在/media/sf_WWW下面的: ...

  9. YEP_footstepsounds

    脚步声插件 ============================================================================Introduction====== ...

  10. 异步 async & await

    1 什么是异步 异步的另外一种含义是计算机多线程的异步处理.与同步处理相对,异步处理不用阻塞当前线程来等待处理完成,而是允许后续操作,直至其它线程将处理完成,并回调通知此线程. 2 异步场景 l  不 ...