07.C++继承和多态

简介: (创建于2017/12/25)1.继承的形式#include //继承//代码的重用性using namespace std;//人类class Human{public: void say(){ cout...

(创建于2017/12/25)

1.继承的形式

#include <iostream>

//继承
//代码的重用性
using namespace std;


//人类
class Human{
public:
    void say(){
        cout << "说话" << endl;
    }
protected:
    char* name;
    int age;
};

//男人
class Man : public Human{
public:
    //泡妞
    void chasing(){
        cout << "泡妞" << endl;       
    }
private:
    //兄弟
    char* brother;
};

void work(Human& h){
    h.say();
}

void main(){
    Man m1;
    m1.say();

    //1.父类类型的引用或指针
    Human* h_p = &m1;
    h_p->say();

    Human &h1 = m1;
    h1.say();

    //子类对象初始化父类类型的对象
    Human h2 = m1;

    system("pause");
}


输出结果:
说话
说话
说话
请按任意键继续. . .

2.向父类构造方法传参

#include <iostream>

//继承
//代码的重用性
using namespace std;

class Human{
public:
    Human(char* name, int age){
        this->name = name;
        this->age = age;
    }
    void say(){
        cout << "说话" << endl;
    }
protected:
    char* name;
    int age;
};

//男人
class Man : public Human{
public:
    //给父类构造函数传参,同时给属性对象赋值
    Man(char *brother, char *s_name, int s_age, char *h_name, int h_age) : Human(s_name, s_age), h(h_name,h_age){
        this->brother = brother;
    }
    //泡妞
    void chasing(){
        cout << "泡妞" << endl;
    }
private:
    //兄弟
    char* brother;
    Human h;
};

void main(){
    Man m1("danny","jack",18,"jason",18);

    system("pause");
}

3.构造函数与析构函数调用的顺序

#include <iostream>

//继承
//代码的重用性
using namespace std;

class Human{
public:
    Human(char* name, int age){
        this->name = name;
        this->age = age;
        cout << "Human 构造函数" << endl;
    }
    ~Human(){
        cout << "Human 析构函数" << endl;
    }
    void say(){
        cout << "说话" << endl;
    }
protected:
    char* name;
    int age;
};

//男人
class Man : public Human{
public:
    //给父类构造函数传参
    Man(char *brother, char *s_name,int s_age) : Human(s_name, s_age){
        this->brother = brother;
        cout << "Man 构造函数" << endl;
    }
    ~Man(){
        cout << "Man 析构函数" << endl;
    }
    //泡妞
    void chasing(){
        cout << "泡妞" << endl;
    }
private:
    //兄弟
    char* brother;  
};

void func(){
    //父类构造函数先调用
    //子类的析构函数先调用
    Man m1("danny", "jack", 18);
}

void main(){
    func();

    system("pause");
}

输出结果:
Human 构造函数
Man 构造函数
Man 析构函数
Human 析构函数
请按任意键继续. . .

4.子类对象调用父类的成员

#include <iostream>

//继承
//代码的重用性
using namespace std;

class Human {
public:
    Human(char* name, int age) {
        this->name = name;
        this->age = age;
        cout << "Human 构造函数" << endl;
    }
    ~Human() {
        cout << "Human 析构函数" << endl;
    }
    void say() {
        cout << "说话" << endl;
    }
public:
    char* name;
    int age;
};

//男人
class Man : public Human {
public:
    //给父类构造函数传参
    Man(char *brother, char *s_name, int s_age) : Human(s_name, s_age) {
        this->brother = brother;
        cout << "Man 构造函数" << endl;
    }
    ~Man() {
        cout << "Man 析构函数" << endl;
    }
    //泡妞
    void chasing() {
        cout << "泡妞" << endl;
    }
    void say() {
        cout << "男人喜欢装逼" << endl;
    }
private:
    //兄弟
    char* brother;
};

void main() {
    //是覆盖,并非多态
    Man m1("alan", "john", 18);
    //调用子类的方法
    m1.say();
    //调用父类的方法
    m1.Human::say();
    //调用父类的方法
    Human h = m1;
    h.say();
    //修改父类属性值
    m1.Human::age = 10;

    system("pause");
}

5.多继承

#include <iostream>

//继承
//代码的重用性
using namespace std;
class Person{

};

//公民
class Citizen{

};

//学生,既是人,又是公民
class Student : public Person, public Citizen{

};

6.继承的访问修饰

#include <iostream>

//继承
//代码的重用性
using namespace std;

//继承的访问修饰
//基类中      继承方式             子类中
//public     & public继承 = > public
//public     & protected继承 = > protected
//public     & private继承 = > private
//
//protected  & public继承 = > protected
//protected  & protected继承 = > protected
//protected  & private继承 = > private
//
//private    & public继承 = > 子类无权访问
//private    & protected继承 = > 子类无权访问
//private    & private继承 = > 子类无权访问
//人类

class Human{
public:
    void say(){
        cout << "说话" << endl;
    }
private:
    char* name;
    int age;
};

//男人
class Man : protected Human{
public:
    //泡妞
    void chasing(){
        cout << "泡妞" << endl;       
    }
private:
    //兄弟
    char* brother;
};

7.继承的二义性,指的是A和B同时继承了C,C中有name属性,那么A和B都有了自己的name属性,此时D同时继承了A和B,那么D中的name属性究竟使用哪个?

#include <iostream>

//继承
//代码的重用性
using namespace std;

class A{
public:
    char* name;
};
//虚函数(virtual function),虚函数是指一个类中你希望重载的成员函数,当你用一个基类指针或引用指向一个继承类对象的时候,你调用一个虚函数,实际调用的是继承类的版本。

//如果没有这个virtual关键字,b.name = "aaa";会报错,因为name属性不明确
class A1 : virtual public A{
    
};

class A2 : virtual public A{

};

class B : public A1, public A2{

};

void main(){
    B b;    
    b.name = "aaa";
    //指定父类显示调用
    b.A1::name = "bbb";
    b.A2::name = "ccc";
    
    cout << b.name << endl;
    cout << b.A1::name << endl;
    cout << b.A2::name << endl;
    system("pause");
}

//使用了virtual关键字,使得调用的name属性是子类重写后的属性,也就是调用的子类的,
//几次赋值相当于都是给b中的name重新赋值,所以打印结果如下
输出结果:
ccc
ccc
ccc
请按任意键继续. . .

8.虚函数

//多态(程序的扩展性)
//动态多态:程序运行过程中,觉得哪一个函数被调用(重写)
//静态多态:重载

//发生动态的条件:
//1.继承
//2.父类的引用或者指针指向子类的对象
//3.函数的重写

#include "Plane.h"
#include "Jet.h"
#include "Copter.h"
//业务函数
void bizPlay(Plane& p){
    p.fly();
    p.land();
}

void main(){
    Plane p1;
    bizPlay(p1);

    //直升飞机
    Jet p2;
    bizPlay(p2);

    Copter p3;
    bizPlay(p3);

    system("pause");
}

Jet.h

#pragma once

#include "Plane.h"

//直升飞机
class Jet : public Plane{
    virtual void fly();
    virtual void land();
};

Jet.cpp

#include "Jet.h"
#include <iostream>
using namespace std;

void Jet::fly(){
    cout << "直升飞机在原地起飞..." << endl;
}

void Jet::land(){
    cout << "直升飞机降落在女神的屋顶..." << endl;
}

Plane.h

#pragma once

//普通飞机
class Plane{
public:
    virtual void fly();
    virtual void land();
};

Plane.cpp

#include "Plane.h"

#include <iostream>
using namespace std;

void Plane::fly(){
    cout << "起飞" << endl;
}

void Plane::land(){
    cout << "着陆" << endl;
}

Copter.h

#pragma once

#include "Plane.h"

//普通飞机
class Copter : public Plane{
public:
    virtual void fly();
    virtual void land();
};

Copter.cpp

#include "Copter.h"
#include <iostream>
using namespace std;

void Copter::fly(){
    cout << "喷气式飞机在跑道上起飞..." << endl;
}

void Copter::land(){
    cout << "喷气式飞机在跑道上降落..." << endl;
}

9.纯虚函数(抽象类)

//1.当一个类具有一个纯虚函数,这个类就是抽象类
//2.抽象类不能实例化对象
//3.子类继承抽象类,必须要实现纯虚函数,如果没有,子类也是抽象类
//抽象类的作用:为了继承约束,根本不知道未来的实现
//形状

class Shape{
public:
    //纯虚函数
    virtual void sayArea() = 0;
    void print(){
        cout << "hi" << endl;
    }
};

//圆
class Circle : public Shape{
public:
    Circle(int r){
        this->r = r;
    }
    void sayArea(){
        cout << "圆的面积:" << (3.14 * r * r) << endl;
    }
private:
    int r;
};

void main(){
    //Shape s;
    Circle c(10);

    system("pause");
}

10.接口(只是逻辑上的划分,语法上跟抽象类的写法没有区别)

//可以当做一个接口
class Drawble{
    virtual void draw();
};

11.模板函数(泛型)

/*void myswap(int& a,int& b){
    int tmp = 0;
    tmp = a;
    a = b;
    b = tmp;
}

void myswap(char& a, char& b){
    char tmp = 0;
    tmp = a;
    a = b;
    b = tmp;
}
*/

//发现:这两个函数业务逻辑一样,数据类型不一样
template <typename T>
void myswap(T& a, T& b){
    T tmp = 0;
    tmp = a;
    a = b;
    b = tmp;
}

void main(){
    //根据实际类型,自动推导
    int a = 10, b = 20;
    myswap<int>(a,b);
    cout << a << "," << b << endl;

    char x = 'v', y = 'w';
    myswap(x, y);
    cout << x << "," << y << endl;

    system("pause");
}


相关文章
|
3天前
|
C++
【C++】从零开始认识多态(二)
面向对象技术(oop)的核心思想就是封装,继承和多态。通过之前的学习,我们了解了什么是封装,什么是继承。 封装就是对将一些属性装载到一个类对象中,不受外界的影响,比如:洗衣机就是对洗衣服功能,甩干功能,漂洗功能等的封装,其功能不会受到外界的微波炉影响。 继承就是可以将类对象进行继承,派生类会继承基类的功能与属性,类似父与子的关系。比如水果和苹果,苹果就有水果的特性。
20 1
|
3天前
|
C++
【C++】从零开始认识多态(一)
面向对象技术(oop)的核心思想就是封装,继承和多态。通过之前的学习,我们了解了什么是封装,什么是继承。 封装就是对将一些属性装载到一个类对象中,不受外界的影响,比如:洗衣机就是对洗衣服功能,甩干功能,漂洗功能等的封装,其功能不会受到外界的微波炉影响。 继承就是可以将类对象进行继承,派生类会继承基类的功能与属性,类似父与子的关系。比如水果和苹果,苹果就有水果的特性。
18 4
|
3天前
|
安全 前端开发 Java
【C++】从零开始认识继承二)
在我们日常的编程中,继承的应用场景有很多。它可以帮助我们节省大量的时间和精力,避免重复造轮子的尴尬。同时,它也让我们的代码更加模块化,易于维护和扩展。可以说,继承技术是C++的灵魂。
11 1
|
3天前
|
安全 程序员 编译器
【C++】从零开始认识继承(一)
在我们日常的编程中,继承的应用场景有很多。它可以帮助我们节省大量的时间和精力,避免重复造轮子的尴尬。同时,它也让我们的代码更加模块化,易于维护和扩展。可以说,继承技术是C++的灵魂。
19 3
【C++】从零开始认识继承(一)
|
1天前
|
C++ 编译器 存储
|
2天前
|
存储 C++
C++中的多态
C++中的多态
7 0
|
2天前
|
存储 编译器 C++
C++中的继承
C++中的继承
10 0
|
5天前
|
设计模式 算法 编译器
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
【C++入门到精通】特殊类的设计 |只能在堆 ( 栈 ) 上创建对象的类 |禁止拷贝和继承的类 [ C++入门 ]
10 0
|
6天前
|
安全 程序员 编译器
【C++】继承(定义、菱形继承、虚拟继承)
【C++】继承(定义、菱形继承、虚拟继承)
13 1
|
存储 编译器 C++
[C++基础]-多态
[C++基础]-多态