C++大学基础教程_10_10_代理类
2014-07-15 13:54
239 查看
//Implementation.h //包含欲隐藏的私有实现的类生成类定义 class Implementation { public: Implementation(int v) :value(v) { //空函数体 } void setValue(int v) { value = v; } int getValue() { return value; } private: int value; };
//Interface.h //代理类Interface class Implementation;//声明 class Interface { public: Interface(int); ~Interface(); void setValue(int); int getValue() const; private: Implementation *iPtr; };
//Interface.cpp #include "Interface.h" #include "Implementation.h" Interface::Interface(int v) :iPtr(new Implementation(v)) { // empty body } Interface::~Interface() { delete iPtr; iPtr = 0; } void Interface::setValue(int v) { iPtr->setValue(v); } int Interface::getValue() const { return iPtr->getValue(); }
//main.cpp //#include "Implementation.h" #include "Interface.h"//只有这一个头文件包含在代码中,!!! #include <iostream> using namespace std; int main() { Interface testInt(6); cout << "Interface contains: " << testInt.getValue() << " before setValue." << endl; testInt.setValue(7); cout << "Interface contains:" << testInt.getValue() << " after setValue." << endl; system("pause >> cout"); return 0; }
相关文章推荐
- C++大学基础教程_10_5_使用this指针
- C++大学基础教程_11_10_实例研究:String类
- C++大学基础教程_10_67_new和delete和static
- C++大学基础教程 _10_3_组成:对象作为类的成员
- C++大学基础教程_10_4_friend函数和friend类
- C++大学基础教程_12_面向对象编程:继承
- C++大学基础教程_11_8_实例研究:Array类
- 笔记10 《C++ 大学教程》
- C++基础教程(一)——Hello World以及计算两个数值的和
- C++ 基础教程
- C++大学基础编程第二章部分题目答案
- C++基础教程(一)——Hello World以及计算两个数值的和
- 图形学基础教程10--高级应用 (带演示程序)
- Python基础教程——10 充电时刻
- C++基础教程(三)——枚举enum
- 《马哥出品高薪linux运维教程》wingkeung学习笔记-linux基础入门课程10
- C/C++ 基础教程
- C++基础教程(二)——数组
- Python基础教程——10 充电时刻
- 信息系统项目管理师视频教程——10 信息系统项目管理基础