An interface class is a class that has no members variables, and where all of the functions are pure virtual!

(接口类是一个没有任何成员变量并且所有的函数都是纯虚函数的类。)

In other words, the class is purely a definition, and has no actual implementation.

Interfaces are useful when you want to define the functionality that derived classes must implement, but leave the details of how the derived class implements that functionality entirely up to the derived class.

Interface classes are often named beginning with an I. Here’s a sample interface class:

class IErrorLog
{virtual bool OpenLog(const char *strFilename) = 0;virtual bool CloseLog() = 0;virtual bool WriteError(const char *strErrorMessage) = 0;
};

Any class inheriting from IErrorLog must provide implementations for all three functions in order to be instantiated.

(任何继承IErrorLog的派生类都必须实现这三个方法才可以被实例化。)

You could derive a class named FileErrorLog, where OpenLog() opens a file on disk, CloseLog() closes it, and WriteError() writes the message to the file. You could derive another class called ScreenErrorLog, where OpenLog() and CloseLog() do nothing, and WriteError() prints the message in a pop-up message box on the screen.

Now, let’s say you need to write some code that uses an error log. If you write your code so it includes FileErrorLog or ScreenErrorLog directly, then you’re effectively stuck using that kind of error log. For example, the following function effectively forces callers of MySqrt() to use a FileErrorLog, which may or may not be what they want.

 double MySqrt(double dValue, FileErrorLog &cLog)
{if (dValue < 0.0){cLog.WriteError("Tried to take square root of value less than 0");return 0.0;}elsereturn dValue;
}

A much better way to implement this function is to use IErrorLog instead(多态):

double MySqrt(double dValue, IErrorLog &cLog)
{if (dValue < 0.0){cLog.WriteError("Tried to take square root of value less than 0");return 0.0;}elsereturn dValue;
}

Now the caller can pass in any class that conforms to the IErrorLog interface.

If they want the error to go to a file, they can pass in an instance of FileErrorLog. If they want it to go to the screen, they can pass in an instance of ScreenErrorLog. Or if they want to do something you haven’t even thought of, such as sending an email to someone when there’s an error, they can derive a new class from IErrorLog (eg. EmailErrorLog) and use an instance of that! By using IErrorLog, your function becomes more independent and flexible.

Interface classes have become extremely popular because they are easy to use, easy to extend, and easy to maintain. In fact, some modern languages, such as Java and C#, have added an “interface” keyword that allows programmers to directly define an interface class without having to explicitly mark all of the member functions as abstract. Furthermore, although Java and C# will not let you use multiple inheritance on normal classes, they will let you multiply inherit as many interfaces as you like. Because interfaces have no data and no function bodies, they avoid a lot of the traditional problems with multiple inheritance while still providing much of the flexibility.

URL:http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/



Interface classes相关推荐

  1. ABAP和Java的tag(marker) interface

    In my previous blog How many fat interfaces are there in SAP system I introduce the concept of " ...

  2. java 日志 生成_Java日志(转)

    日志对于一个系统来说非常重要,查找异常信息.分析系统运行情况等都需要用到日志.所以无论是JDK还是第三方都提供了关于日志的相关工具,本文分别介绍以下几种工具,以及各种工具间的整合.原理. JDK的ja ...

  3. java setmethod_Java Operation.setJavaMethod方法代码示例

    import com.sun.tools.internal.ws.processor.model.Operation; //导入方法依赖的package包/类 private void createJ ...

  4. Effective C++笔记_条款31将文件间的编译依存关系降至最低

    Effective C++笔记_条款31将文件间的编译依存关系降至最低 这个章节,读了两遍还是不是很清楚,有一种没法和作者沟通的感觉,看来我还是一个C++的初学者呀.好吧,不多说了,回归主题,今天的笔 ...

  5. Effective C++ --5 实现

    上一部分 Effective C++ --4 设计与声明 26.尽可能延后变量定义式的出现时间 (1)这样可以增加程序的清晰度并改善程序效率.如定义变量后还未使用遇到return或者抛出异常,这样未使 ...

  6. 读书笔记_Effective_C++_条款三十一:将文件间的编译依存关系降至最低(第二部分)...

    下面再来看书,去理解书上说的Handler classes就简单多了,我们大概过一下. 假设我们要写一个Person类,如下: 1 class Person 2 { 3 private: 4 stri ...

  7. 万字长文带你一文读完Effective C++

    Effective C++ 视C++为一个语言联邦 STL Template C++ C Object-oriented C++ 一开始C++只是C加上一些面向对象特性,但是随着这个语言的成熟他变得更 ...

  8. Effective 笔记

    1,C++属于一个语言联邦 : C     Object-Oriented C++     Template C++       STL 2,应尽量以const,enum,inline替换#defin ...

  9. Effective C++ 阅读笔记(一)透彻了解inline以及降低编译依存关系

    inline内敛 1.类似于C中的#define 在C++中,提供了inline函数来代替C中的宏定义.(通常可以使用const来代替单纯变量的宏定义,它可以提供类型检查.对于形似函数的宏,最好改用i ...

最新文章

  1. C++11中override的使用
  2. mysql windows 管道连接,科技常识:Windows Server 2016 MySQL数据库安装配置详细安装教程...
  3. 汉字转拼音,中文拼音排序器
  4. C++显式隐式构造函数
  5. .NET技术学习目录整理
  6. python处理表格数据教程_用Python的pandas框架操作Excel文件中的数据教程
  7. 图解python pdf_Python合并同一个文件夹下所有PDF文件的方法
  8. C++ 使用 TinyXml 解析 XML 文件
  9. java 解析/操作 xml 几种常用方式 xml的增加/删除/修改
  10. 单片机ADC采样算法----限幅消抖滤波法
  11. python装饰器详解-python中的装饰器详解
  12. JavaScript浏览器window对象→简介、消息对话框、计时器、history、打开新窗口及模式showModalDialog、location、navigator、screen、窗口位置尺寸
  13. 2018年全国卷Ⅰ卷理科数学图片版
  14. 诺基亚E63凤凰刷机实战
  15. h5+vue+php仿微信源码-泡泡IM
  16. pycharm windows 重置_pycharm重置设置,恢复默认设置
  17. 实用的项目管理网络计划软件-MS Project
  18. 查找重复姓名的SQL语句
  19. 企业间数据竞争规则研究
  20. 微软商店无法连接网络的问题解决

热门文章

  1. 第二章 centos安装maven
  2. 摘自《解析极限编程-拥抱变化》
  3. wpf Command Binding
  4. PHP 学习 第一天
  5. SilverLight入门实例(一)
  6. 春运男子持刀强行劫走17张卧铺票 ....
  7. windows设置自动清理log
  8. JS实现sleep()方法
  9. 003thinkphp 数据库查询及表关联
  10. Kenshin Cui's Blog