继承

#include <iostream>
using namespace std; class father{ public:
void getHeight(){cout<<height<<endl;}
void getAge(){cout<<age<<endl;}
protected:
int height,age;
}; class son: public father{
private:
int weight;
public:
void getWeight(){cout<<weight<<endl;}
void shwo()
{
cout<<this->weight<<endl;
cout<<this->height<<endl;
cout<<this->age<<endl;
}
}; int main()
{
son one;
one.shwo(); }

子类对象赋值给父类

#include <iostream>
using namespace std; class father{ public:
int height;
}; class son: public father{
public:
int weight; }; int main()
{
son a;
father b; a.height=;
b=a;//子类赋给父类
}

父类引用指向子类对象

#include <iostream>
using namespace std; class father{ public:
int height;
}; class son: public father{
public:
int weight; }; int main()
{
father *p;
son jack; p=&jack; //父类引用指向子类对象 父类还可以作为子类的别名(虚函数)
p->height=; cout<<jack.height<<endl;
cout<<jack.weight<<endl; }
#include <iostream>
using namespace std; class father{ protected:
int height;
}; //私有派生用的不多,因为继承的成员都变成私有的了,不可以访问
class son: private father{ //继承的父类的protected,public全部变成 private
public:
int weight; int getHeight()
{
return height; } void setHeight(int x)
{
this->height=x; } }; int main()
{
son a ;
a.setHeight();
cout<<a.getHeight()<<endl;
}

继承中构造函数的执行顺序(先构造基类,后构造子类, 先析构子类,在析构基类)

#include <iostream>
using namespace std; class father{ private:
int height;
public:
father(){cout<<"father construct"<<endl;}
~father(){cout<<"father destruct"<<endl;} }; class son: public father{
public:
int weight;
son(){cout<<"son construct"<<endl;}
~son(){cout<<"son destruct"<<endl;} }; int main()
{
son a ; return ;
}

多重继承,以 继承的顺序进行构造

向基类构造函数传递参数

#include <iostream>
using namespace std; class father{ public:
int height;
int weight;
public:
father(int height, int weight ){
this->height=height;
this->weight=weight;
cout<<"father construct"<<endl;
}
~father(){cout<<"father destruct"<<endl;} }; class son: public father{
public:
int age;
son(int height,int weight,int age);
~son(){cout<<"son destruct"<<endl;} }; son::son(int height, int weight, int age):father(height, weight)
{
this->age=age;
cout<<"son construct"<<endl;
} int main()
{
son a(,,) ; cout<<a.height<<endl;
cout<<a.weight<<endl;
cout<<a.age<<endl; return ;
}

多继承的歧义(作用域操作符)

#include <iostream>
using namespace std; //class father{
//
//public:
// int height;
// int weight;
//public:
// father(int height, int weight ){
// this->height=height;
// this->weight=weight;
// cout<<"father construct"<<endl;
// }
// ~father(){cout<<"father destruct"<<endl;}
//
//};
//
//class son: public father{
//public:
// int age;
// son(int height,int weight,int age);
// ~son(){cout<<"son destruct"<<endl;}
//
//};
//
//son::son(int height, int weight, int age):father(height, weight)
//{
// this->age=age;
// cout<<"son construct"<<endl;
//} class a{
public:
void hello(){cout<<"a hello"<<endl;}
}; class b{
public:
void hello(){cout<<"b hello"<<endl;}
}; class c : public a, public b{
public:
void hello(){cout<<"c hello"<<endl;}
}; int main()
{ c i;
i.hello(); //本类的无需
i.a::hello(); //作用域操作符,作用域分辨
i.b::hello(); return ;
}

解决两异性(虚基类)

#include <iostream>
using namespace std; class common{
public:
void stand(){} }; class a: virtual public common{
public:
void hello(){cout<<"a hello"<<endl;}
}; class b: virtual public common{
public:
void hello(){cout<<"b hello"<<endl;}
}; class c : public a, public b{
public:
void hello(){cout<<"c hello"<<endl;}
}; int main()
{ c i;
i.stand(); return ;
}

c++学习-继承的更多相关文章

  1. JavaScript 核心学习——继承

    本篇博文讲述如何在 JavaScript 中实现继承,以及原型与原型链的知识,在附录中将会讲述 JavaScript 面向对象的常见错误. ##原型与原型链在 JavaScript 中,使用类将会付出 ...

  2. Java基础学习-- 继承 的简单总结

    代码参考:Java基础学习小记--多态 为什么要引入继承? 还是做一个媒体库,里面可以放CD,可以放DVD.如果把CD和DVD做成两个没有联系的类的话,那么在管理这个媒体库的时候,要单独做一个添加CD ...

  3. python学习-继承

    # 继承# 你的是我的,我的还是我的 class Animal: def __init__(self,name,private_v1): self.name = name self._private_ ...

  4. c++学习--继承与派生

    继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成 ...

  5. Yii2的深入学习--继承关系

    想要了解 Yii2 的话,一定要对 Yii2 中相关类的继承关系有所了解.由于暂时读的代码有限,下面的图中只列出了部分继承关系,之后回跟着源码阅读的越来越多而增加 由上图可以看到 Yii2 中大多数类 ...

  6. Java学习——继承

    将学生工人的共性描述提取出来,单独进行描述,只要让学生和工人与单独描述的这个类有关系,就可以了. 继承:1,提高了代码的复用性.2,让类与类之间产生了关系.有了这个关系,才有了多态的特性. 注意:千万 ...

  7. day25 python学习 继承,钻石继承 多态

    ---恢复内容开始--- 通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' d ...

  8. day25 python学习 继承,钻石继承

    通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' def __init__(s ...

  9. OC学习--继承

     1.什么是继承? 继承是指一个对象直接使用另一对象的属性和方法. 继承可以使得子类具有父类的各种属性和方法,而不是再次编写相同的代码.在子类继承父类的同时,可以重新定义某些属性,并重写某些方法, 即 ...

随机推荐

  1. 越狱Season 1-Episode 5: English, Fitz or Percy

    Season 1, Episode 5: English, Fitz or Percy -Pope: I assume this is about your transfer request for ...

  2. IAR MSP430如何生成烧写文件

    IAR生成430烧写方法有2种, 第一种是:将工程的debug模式切换成release模式,看图片操作.    那个.d43文件就是仿真调试模式的文件. 这里的test.txt文件就是烧写文件了,不要 ...

  3. when compile /home/wangxiao/NVIDIA-CUDA-7.5 SAMPLES, it warning: gcc version larger than 4.9 not supported, so: old verson of gcc and g++ are needed

    1. when compile /home/wangxiao/NVIDIA-CUDA-7.5 SAMPLES, it warning: gcc version larger than 4.9 not ...

  4. gdb Debugging Full Example

    http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html

  5. unity3d中切换武器

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  6. noip2012普及组——质因数分解

    [问题描述]已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. [输入]输入文件名为 prime.in.输入只有一行,包含一个正整数 n. [输出]输出文件名为 prime.out.输出只 ...

  7. android 二维码扫描

    了解二维码这个东西还是从微信 中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候, 老板说要加上二维码扫描功 ...

  8. Intent的FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT

    Activity的两种启动模式:FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_REORDER_TO_FRONT 1. 如果已经启动了四个Activity:A,B,C和D. ...

  9. ORA-01013:用户请求取消当前的操作

    ORA-01013:用户请求取消当前的操作 在测试一个通过ODBC连接ORACLE数据库的VB程序时,总是出现该错误,估计应该是数据量比较大,导致超时. 查到解决方法有如下四种 (选任意一种即可): ...

  10. WCF学习心得------(七)消息协定

    第七章 消息协定 7.1 消息协定概述 通常情况下,在定义消息的架构时只使用数据协定就足够,但是有时需要精确控制如何将类型映射到通过网络传输的SOAP消息.对于这种情况,通常解决方案是插入自定义的SO ...