C++_day7_继承


#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_继承的更多相关文章
- javaScript的原型继承与多态性
1.prototype 我们可以简单的把prototype看做是一个模版,新创建的自定义对象都是这个模版(prototype)的一个拷贝 (实际上不是拷贝而是链接,只不过这种链接是不可见,给人们的感觉 ...
- JavaScript的继承实现方式
1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...
- javascript中的继承与深度拷贝
前言 本篇适合前端新人,下面开始...... 对于前端新手来说(比如博主),每当对js的对象做操作时,都是一种痛苦,原因就是在于对象的赋值是引用的传递,并非值的传递,虽然看上去后者赋值给了前者,他们就 ...
- 谈谈一些有趣的CSS题目(四)-- 从倒影说起,谈谈 CSS 继承 inherit
开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...
- JS继承类相关试题
题目一: //有关于原型继承的代码如下:function Person(name) { this.name = name;}Person.prototype = { getName : f ...
- JS继承之寄生类继承
原型式继承 其原理就是借助原型,可以基于已有的对象创建新对象.节省了创建自定义类型这一步(虽然觉得这样没什么意义). 模型 function object(o){ function W(){ } W. ...
- JS继承之借用构造函数继承和组合继承
根据少一点套路,多一点真诚这个原则,继续学习. 借用构造函数继承 在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术( ...
- JS继承之原型继承
许多OO语言都支持两种继承方式:接口继承和实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在ECMAScript中无法实现接口继承.ECMAScript只支 ...
- 深入浅出JavaScript之原型链&继承
Javascript语言的继承机制,它没有"子类"和"父类"的概念,也没有"类"(class)和"实例"(instanc ...
随机推荐
- vue中$refs、$slot、$nextTick相关的语法
Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来 1.$data和$el var data = { a: 1 } var vm = new Vue({ el ...
- 【Mac】-NO.133.Mac.1 -【重置忘记macos root密码】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- IDEA 破解_补丁永久_2018.3
主要是Eclipse我已经玩坏了三次了,切换jdk8到jdk10,再切换到jdk8,大量文件乱码,怎么改都没用,有的时候Eclipse久了不用,项目放在里面发霉了,坏掉了,MMP,换到I ...
- django框架配置mysql数据库
django配置mysql数据库: 1.首先更改django项目文件中的settings.py的数据库配置 DATABASES = { 'default': { 'ENGINE': 'django.d ...
- windows server 2008 R2服务器无法通过ShellClass获取mp3音乐时长
我们先看一段代码,获取mp3播放时长: #region GetMediaDetailInfo 获取媒体文件属性信息 /// <summary> /// 获取媒体文件属性信息 /// < ...
- Ajax不执行回调函数的原因(转)
今天用ajax的post请求后台,但是始终不执行回调函数,经查得知,ajax不执行回调函数的原因如下: jquery中规定返回的JSON字符串的KEY要用引号括起来,如{“result”: 1}这样才 ...
- 叮咚,你的Lauce上线了!
哈,2014 - 2016 - 2018,虽然每隔两年才有那么一篇随笔,博客园,我还是爱你的~ 嗯,2018,马上又要失业了,我这是自带黑属性啊啊啊哈,工作了4年多的项目要被砍掉了, 倒不是说非要这个 ...
- BindingResult 作用原理
controller代码 @RequestMapping("") public String index(@Valid User user , BindingResult bind ...
- 安卓GreenDao(基础)
GreenDao的基础使用很简单,网上一大筐,推荐在简书里面搜索,那么我这里要说些什么呢,试想,这些简单的Demo可以带你了解GreenDao,但你能用这些代码做公司的项目么,肯定不行,所以我结合自身 ...
- PyCharm下使用Scrapy建立爬虫项目--MyFirstSpiderObject
首先下载并安装Anaconda3以及PyCharm Anaconda3选中添加环境变量,如果忘记选中可以手动在path中添加如下环境变量 建文件夹scrapy 安装scrapy cmd进入对应目录,执 ...