[源码下载]

不可或缺 Windows Native (23) - C++: 虚函数

作者:webabcd

介绍
不可或缺 Windows Native 之 C++

  • 虚函数

示例
1、基类
CppHuman.h

#pragma once 

#include <string>

using namespace std;

namespace NativeDll
{
class CppHuman
{ protected:
string Name; public:
// 我是虚函数
virtual string Show(); // 我是纯虚函数(后面的“=0”只起形式上的作用,用于告诉编译器:“这是纯虚函数”)
// 纯虚函数只有声明,没有定义,其具体的功能是留给派生类定义的
// 凡是包含纯虚函数的类都是抽象类,抽象类是无法实例化的,因为纯虚函数是不能被调用的
// virtual string Display() = 0; CppHuman(string name); // 我是 virtual 的析构函数
virtual ~CppHuman(); };
}

CppHuman.cpp

/*
* 基类
*/ #include "pch.h"
#include "CppHuman.h"
#include "cppHelper.h" using namespace NativeDll; string CppHuman::Show()
{
return "human: " + Name;
} CppHuman::CppHuman(string name) : Name(name)
{ } CppHuman::~CppHuman()
{ }

2、派生类
CppChild.h

#pragma once 

#include <string>
#include "CppHuman.h" using namespace std; namespace NativeDll
{
class CppChild : public CppHuman
{ public:
// 由于基类 CppHuman 的 Show() 函数是虚函数,所以其所有直接或间接派生类中,如果声明了此函数则均为虚函数(virtual 可以省略)
virtual string Show(); CppChild(string name); ~CppChild(); };
}

CppChild.cpp

/*
* 派生类(基类是 CppHuman)
*/ #include "pch.h"
#include "CppChild.h"
#include "cppHelper.h" using namespace NativeDll; string CppChild::Show()
{
return "child: " + Name;
} CppChild::CppChild(string name) : CppHuman(name)
{ } CppChild::~CppChild()
{ }

3、示例
CppClass7.h

#pragma once 

#include <string>

using namespace std;

namespace NativeDll
{
class CppClass7
{
public:
string Demo();
};
}

CppClass7.cpp

/*
* 虚函数
*/ #include "pch.h"
#include "CppClass7.h"
#include "CppChild.h" using namespace NativeDll; void cppclass7_demo1();
void cppclass7_demo2(); string CppClass7::Demo()
{
// 虚函数
cppclass7_demo1(); // virtual 的析构函数
cppclass7_demo2(); return "看代码及注释吧";
} // 虚函数
void cppclass7_demo1()
{
// 不使用虚函数的示例:参见 CppClass5.cpp 中的“基类与派生类的转换” // 以下演示了如何使用虚基类
CppHuman human("webabcd");
CppChild child("diandian"); // 指针指向基类,调用虚函数后执行的是基类的虚函数
CppHuman *humanPointer = &human;
string result = humanPointer->Show(); // human: webabcd // 指向基类的指针改为指向派生类,调用虚函数后执行的是派生类的虚函数
humanPointer = &child;
result = humanPointer->Show(); // child: diandian // 像上面这种方式是在程序运行阶段把虚函数和类对象“绑定”在一起的,因此此过程称为动态关联(dynamic binding)或滞后关联(late binding),其属于动态多态性
// 如果使用了虚函数,则编译器会为该类构造一个虚函数表(virtual function table,简称 vtable),它是一个指针数组,存放每个虚函数的入口地址,据此可做静态关联和动态关联
} // virtual 的析构函数
void cppclass7_demo2()
{
// 一般来说,清理派生类时,会先调用派生类的析构函数,然后调用基类的析构函数
// 但是下面这种情况例外
CppHuman *pt = new CppChild("diandian");
delete pt;
// 此时,如果基类的析构函数不是 virtual 的,则只会执行基类的析构函数
// 此时,如果基类的析构函数是 virtual 的,则会先执行派生类的析构函数,再执行基类的析构函数
// 所以,最好把基类的析构函数声明为虚函数(其会使所有派生类的析构函数都自动变为虚函数),以避免清理不彻底
}

OK
[源码下载]

不可或缺 Windows Native (23) - C++: 虚函数的更多相关文章

  1. 不可或缺 Windows Native (20) - C++: 友元函数, 友元类

    [源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...

  2. 不可或缺 Windows Native (6) - C 语言: 函数

    [源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...

  3. 不可或缺 Windows Native 系列文章索引

    [源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...

  4. 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类

    [源码下载] 不可或缺 Windows Native (22) - C++: 多重继承, 虚基类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 多重继承 虚基类 示例1 ...

  5. 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板

    [源码下载] 不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板 作者:webabcd 介绍不可或缺 Windows Native 之 C++ ...

  6. 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)

    [源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...

  7. 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native

    [源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...

  8. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  9. 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板

    [源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Window ...

随机推荐

  1. table下属标签与标签中间不能加其他任何标签

    今天设计网页,很想在table下面的caption下面加hr标签,不能正常解析,后来发现hr标签可以显示,但是用浏览器查看解析,发现hr在table上面进行了解析,不会再caption下面显示, 又连 ...

  2. EF架构~Cannot attach the file as database

    回到目录 Cannot attach the file as database这个异常是在EF的code frist里经常出现的,解决方法很简单,只要重新启动一下V11实例即可. CMD> sq ...

  3. Git Sophisticated Commands

    Compare two branches: branch_1 and branch_2: git diff branch_1…branch_2 Merge specified files of one ...

  4. 趣谈、浅析CRLF和LF

    作为程序员,在处理文件和输入输出的时候经常要跟CRLF和LF打交道.可能大家多少知道一些,但总是记不清楚,我也是这样的,因此写下这篇博文,作为记录. 首先,明确他们的意思:CR(回车),LF(换行). ...

  5. lufylegend游戏引擎

    lufylegend游戏引擎介绍:click 这个链接我觉得已经很详细的介绍了这个引擎. 所以以下我只说说一些简单的游戏代码过程. 首先从canvas做游戏叙述起: 这是一个让人很熟悉的简单小游戏,网 ...

  6. CSS背景100%平铺 浏览器缩小背景显示不全解决办法

    本文我们分享前端CSS背景100%平铺,浏览器缩小背景显示不全bug解决的两个方法,如果你也遇到了,那么就可以参考下面文章. 把浏览器的窗口缩小时,拖动滚动条时你会发现原本设定的CSS背景100%平铺 ...

  7. 深入理解CSS绝对定位

    × 目录 [1]定义 [2]特性 [3]display[4]clip[5]静态位置[6]overflow 前面的话 前面已经介绍了定位的偏移和层叠,例子中大量的应用了绝对定位.因为相较于相对定位和固定 ...

  8. 《Administrator's Guide》之Managing Memory

    Automatic Memory Management 1. 如果要启动Automatic Memory Management,如何确定MEMORY_TARGET的值呢? 1> 在SQL*Plu ...

  9. .net, java, c/c++ 和钱

    .net, java, c/c++ 和钱 最近有一段时间没有写博客了,原因是没时间,项目需要在短时间内增加一些安全性的支持,为此我花了近两个月的时间做基础研究,现在路已经跑通了,稍闲下来,看到园子里面 ...

  10. Windows Azure HandBook (7) 基于Azure Web App的企业官网改造

    <Windows Azure Platform 系列文章目录> 1.用户场景: C公司是全球大型跨国连锁餐厅,在世界上大约拥有3万间分店.其IT系统主要部署其海外数据中心,或者租用其他ID ...