C++工作笔记-C++代码实现接口的概念
生活随笔
收集整理的這篇文章主要介紹了
C++工作笔记-C++代码实现接口的概念
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接口:不能實例化的東西,只需把構造函數私有化即可!
源碼如下:
PrintMainViewProxy.h
#ifndef __PRINTMAINVIEWPROXY_H__ #define __PRINTMAINVIEWPROXY_H__class PrintMainViewProxy{public:static char *getHelloWorld();static char *getHowAreYou();private:PrintMainViewProxy();};#endif //__PRINTMAINVIEWPROXY_H__PrintMainViewProxy.cpp
#include "PrintMainViewProxy.h"char * PrintMainViewProxy::getHelloWorld() {return "Hello World"; }char * PrintMainViewProxy::getHowAreYou() {return "How Are You"; }PrintMainViewProxy::PrintMainViewProxy() {}main.cpp
#include <iostream> #include "PrintMainViewProxy.h" using namespace std;void main(){ char *helloWorld = PrintMainViewProxy::getHelloWorld();char *howAreYou = PrintMainViewProxy::getHowAreYou();cout << helloWorld << endl;cout << howAreYou << endl;getchar(); }程序運行截圖如下:
如果實例化就會報錯!
如下圖所示:
總結
以上是生活随笔為你收集整理的C++工作笔记-C++代码实现接口的概念的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++|Qt中QTreeWidget基本
- 下一篇: C++ STL vector的输出