#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. Python super初始化理解过程

    # -*- coding:utf-8 -*-<br data-filtered="filtered"> class A(object): def __init__(se ...

  2. 外贸建站之图片预加载JS代码分享

    外贸建站之图片预加载JS代码分享 function preloadimg() { setTimeout(function() { new Image().src = "images/2017 ...

  3. java中的类型转换

    java中的类型转换分为两种 自动类型转换 要实现数据的自动类型转换必须同时满足下面两个条件 两种数据类型彼此兼容 目标类型的取值范围大于原类型范围 强制类型转换 当两种数据类型彼此不兼容,或者说目标 ...

  4. Linux安全之SSH 密钥创建及密钥登录

    1.首先进入Linux系统的用户目录下的.ssh目录下,root用户是/root/.ssh,普通用户是/home/您的用户名/.ssh,我们以root用户为例: cd .ssh #如果没有 自己创建 ...

  5. python format()用法

    转自 https://www.cnblogs.com/gide/p/6955895.html python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串 ...

  6. 记录tiny6410 使用linux-2.6.28.6内核遇到starting kernel...的问题

    1\问题的解决主要是参考了tiny210相同的问题 2\/home/suxuandong/Documents/computer/linux_kernel/tiny6410/linux-2.6.28.6 ...

  7. appium工作原理

    Appium原理 面试的时候,被问到appium原理,一点不会,实在尴尬.大家可以直接翻看原作https://blog.csdn.net/jffhy2017/article/details/69220 ...

  8. 20175208 《Java程序设计》第七周学习总结

    20175208<Java程序设计>第七周学习总结 第八章-常用实用类String类 构造String对象 string类: (1)常量对象:常量池中的数据在程序运行期间再也不允许改变. ...

  9. oracle 中对查询出来的数据进行切割、截取等操作

    oracle 中对查询出来的数据进行切割.截取等操作 最近遇到一个问题,需要把一个带有,的字符串拆分成多行.通过查询资料,这个操作需要使用以下2个关键知识: 1. REGEXP_SUBSTR函数 这个 ...

  10. SynchronousQueue------TransferQueue源码分析

    不像ArrayBlockingQueue.LinkedBlockingDeque之类的阻塞队列依赖AQS实现并发操作,SynchronousQueue直接使用CAS实现线程的安全访问.由于源码中充斥着 ...