c++学习-继承

继承

#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=1;
    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=110;

    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(1);
    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 0;
}

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

向基类构造函数传递参数

#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(1,2,3) ;

    cout<<a.height<<endl;
    cout<<a.weight<<endl;
cout<<a.age<<endl;

    return 0;
}

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

#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 0;
}
时间: 2024-08-10 23:30:13

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

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

为什么要引入继承? 假如我要做一个媒体库,里面可以放CD,可以放DVD.如果把CD和DVD做成两个没有联系的类的话,那么在管理这个媒体库的时候,要单独做一个添加CD的函数,单独做一个添加DVD的函数,如果还要往这个媒体库里添加其他的媒体类,还要再创建另一个添加函数.我们说这样的代码不具备可扩展性.另外,CD和DVD有很多相似之处,它们的成员变量里都有标题,播放时长,评论,等等,我们称这样的程序存在大量的代码复制,代码复制是代码不良的表现.所以我们可以创建一个它们的父类,把他们相同的成员变量都添加

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

继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成员初始化: 3 执行派生类的构造函数体,对派生类数据成员初始化. 代码如下:#include<iostream.h> class base{ int x; public: base(int i) { x=i; cout<<"基类的构造函数"<<endl;

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

想要了解 Yii2 的话,一定要对 Yii2 中向相关类的继承关系有所了解.由于暂时读的代码有限,下面的图中只列出了部分继承关系,之后回跟着源码阅读的越来越多而增加 由上图可以看到 Yii2 中大多数类都继承自 yii\base\Object,实现了属性的功能. yii\base\Component yii\base\Event 和 yii\base\Behavior 都继承了 yii\base\Object,yii\base\Component 是一个实现了属性/事件和行为功能的基础类,yii

学习继承、重写、虚方法

对于我来说好累哦!压力好大,到现在为止我对继承只了解一点点,而虚方法与重写就更晕了,有些时候晕的头好疼哦!哎哎哎 就如我现在写的一些,都不是太懂: EG: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace p_20150423_2 { class Internowork     {//网络部   

学习-继承与多态

一 继承 类与类的is a关系,叫做继承 关键字:extends-继承 语法: public class 子类名 extends 父类名 { 效果:父类所以属性行为子类自动具有;java中只有单继承,一个子类只能继承一个父类 重写:子类把来自于父类的方法覆盖,要求: a.返回类型,方法名,形参列表必须与被重写方法完全一致 b.访问修饰符不能比被重写方法小 c.不能比被重写方法抛出更多异常 内存中效果:内存叠加 父类构造方法不会被继承,但是会参与子类对象创建(如图),在子类new对象时,会先开辟空

【 python 学习笔记 -- OOP】在实例中学习继承(inheritance)和组合(composition)

[面向对象的三大特征]: [封装(encapsulation)]: 通过抽象的类把数据和方法封装起来.实例的数据只能通过public interface(即实例的方法)来获取或操作. [优点]:1. 功能只需在一个地方定义,而不需要在很多地方重复定义 2. 保证对象内部的数据的安全性 3. 当我们想要使用一个方法的时候,我们只需要知道我们用这个方法会返回什么结果,而不需要知道内部到底做了哪些操作来实现的.  就好像我们看电视时想要换频道,只需要按几个按钮,不需要知道要怎么调频. [继承  (in

1.java学习---继承

一.继承的基本概述 继承的概述 就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性.相同的行为.子类可以直接访问父类中的非私有的属性和行为. 继承的格式 public class 子类 extends 父类{ } 继承的优缺点 (1)子类自动拥有了父类的非私有的成员,子类也可以有自己的成员. (2)提高了代码的互用性和扩展性. (3)耦合性太高,不利于开发原则:高内聚低耦合. (4)java中只支持单继承. 二.继承的特点 继承中成员变量的访问特点 子类和父类的成员变量重名时,如Zi

jQuery2.0.3源码学习---继承方法

jQuery中的extend()方法主要有三种用法: // 扩展插件 调用方法:$.a() $.extend({ a : function{ alert(1) } }) // 合并对象到第一个 $.extend(a,{name:'nick'},{age:'30'}) //拷贝 var a = {}; var b = {nage: 'nick'} $.extend(a, b) //浅拷贝 $.extend(true,a, b) //浅拷贝 其源码部分如下所示: jQuery.extend = jQ

Java泛型学习笔记 - (六)泛型的继承

在学习继承的时候, 我们已经知道可以将一个子类的对象赋值给其父类的对象, 也就是父类引用指向子类对象, 如: 1 Object obj = new Integer(10); 这其实就是面向对象编程中的is-a关系. 既然上面的代码正确, 那么在泛型中, 也可以使用如下代码: 1 public class Box<T> { 2 private T obj; 3 4 public Box() {} 5 6 public T getObj() { 7 return obj; 8 } 9 10 pub