美文网首页
C++中的继承和动态绑定小结—一个例子

C++中的继承和动态绑定小结—一个例子

作者: fwei | 来源:发表于2017-04-13 19:27 被阅读0次

抽象、继承和动态绑定是面向对象编程的核心思想,这里通过一个代码做一个简答的总结,其中主要的一些知识点,都写在代码的注释中。
exampleOOP.h 文件(对用到的基类和派生类做了定义)

#ifndef EXAMPLEOOP_H
#define EXAMPLEOOP_H
#include <iostream>

class base {
public:
    base() = default; //******显式声明默认构造函数,无参数
    base(int i){      //******自己定义的默认构造函数
        std::cout << "the based "<< i << 
            " class has been created !" << std::endl; 
    }
    void base_fun1() { 
        std::cout << "This is the fun1 of base" << std::endl; 
    }
    virtual void virtual_final()  final { //****final参数,该函数不可以再在派生类中定义,虽然他是一个虚函数
        std::cout << "final function" << std::endl; 
    }
    virtual void virtua_fun2(){ 
        std::cout << "this is the virtual funciton,from base" << std::endl; 
    }
    virtual ~base() {  //析构函数,必须声明为虚函数
        std::cout << "delete base " << std::endl; 
    }
};

class child : public base {
public:
    child(int i,int j):base(j) { //派生类中初始化基类
        std::cout << "the child "<< i 
            <<"  class has been created !" << std::endl; 
    }
    child(int i) : base(i){}
    void virtua_fun2() override {  //override 显式声明重写了派生类中的虚函数
        std::cout << "this is the virtual funciton,from child class" 
            << std::endl; 
    }
    void child_fun() { 
        std::cout << "this is the child function,only child have" 
            << std::endl; 
    }
    void virtua_fun2(int x) { 
        std::cout << "the same name of base_fun2, but define in child2" << std::endl; 
    }
    ~child() { 
        std::cout << "delete child" << std::endl;
    }
};

#endif
#include "exampleOOP.h"

int main() {
    static int i = 0;
    static int j = 0;
    base base_example(1);
    child child_example(2,3);   //先构建基类,在构建派生类
    
    // 关于派生类和基类的对象的一些规则的例子
    child_example.base_fun1();  //派生类访问基类的非虚函数函数
    child_example.virtua_fun2();    //派生类对象调用派生类的同名函数
    child_example.virtual_final(); //派生类可以使用基类的final函数,但不可以重新定义
    base_example.virtua_fun2();   //基类对象调用基类的函数
    //base_example.virtua_fun2()

    base * base_point = new child(1); //简单理解,可以认为base_point是一个“阉割版”的派生类指针,即这个指针对象只可以使用派生类继承的函数成员,而不能使用派生类自己特有的函数成员,在本例中,base_point不能调用virtua_fun2(int x)。
    base_point->base_fun1();//指向派生类类型的基类指针可以使用派生类中,继承的基类的函数
    base_point->virtua_fun2();//调用虚函数的时候,调用的是派生类的中定义的虚函数
    //base_point->virtua_fun2(1); 不能通过,因为这个函数虽然和虚函数有着同样的名字,但是参数不同,因此相当于是派生类中特有的函数,而派生类中特有的函数,只有通过派生类的对象、指针或引用来使用。
    delete base_point; //用完要删除,new完记得delete
    
    child child_example2(1);
    base &t = child_example2;
    t.virtua_fun2();//可以简单的记为,基类的引用对象,如果引用的是派生类,那么这个对象就可以看做是派生类对象
    t.base_fun1();// 可以使用基类的所有函数

    child *child_point = &child_example;
    child_point->virtua_fun2(2);//可以使用virtua_fun(int x)函数,相当于重载
    //delete child_point; 此处不能delete,因为child是指向了一个已经存在的对象,如果不再使用这个指针,可以使这个指针指向NULL 
    child_point = NULL;
    
    return 0;
}
*/

程序执行结果如下:

基类和派生类

相关文章

  • C++中的继承和动态绑定小结—一个例子

    抽象、继承和动态绑定是面向对象编程的核心思想,这里通过一个代码做一个简答的总结,其中主要的一些知识点,都写在代码的...

  • 第14篇:C++类继承中的动态绑定

    寄语 我们前几篇文章分别解析从函数指针的角度初步阐述了静态绑定和动态绑定的区别。而且又因为C++中在类继承的上下文...

  • Java 多态的例子

    多态就是通过继承和动态绑定来决定程序使用哪个实现. 一个简单的例子。 输出结果: 从例子可以看出: 成员变量不具备...

  • c++面向对象: public 继承, 虚方法, 动态绑定

    title: 'c++面向对象: public 继承, 虚方法, 动态绑定'date: 2018-11-04 13...

  • 深刻剖析之c++博客文章

    三大特性 封装、继承、多态 多态 C++ 虚函数表解析C++多态的实现原理 介绍了类的多态(虚函数和动态/迟绑定)...

  • C++语法系列之14-- virtual总结

    1 动态绑定 在C++中,默认不会触发动态绑定,如果需要触发动态绑定,那么就需要:1)将函数指定为虚函数;2)通过...

  • C++中的(纯)虚函数

    简介 本章节会介绍在C++中虚函数及纯虚函数的主要作用,C++也是通过虚函数实现动态绑定,本小节不会去讲述动态绑定...

  • effective C++ 3nd

    条款1:视C++为一个语言联邦 1. C 用于C的基本特性。 2. 面向对象 封装、继承、多态、动态绑定、虚函数表...

  • C++动态绑定和静态绑定

    动态绑定和静态绑定 静态绑定是编译期间就可以确定的类型 动态绑定则只能在运行时确定 用C++描述轿车和卡车 可以抽...

  • C++面向对象

    小结 虚函数是virtual声明的。实现动态链接的功能。 纯虚函数是没有函数主体的虚函数。 一、C++继承 1.当...

网友评论

      本文标题:C++中的继承和动态绑定小结—一个例子

      本文链接:https://www.haomeiwen.com/subject/ubwxattx.html