国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

[Boolan] C++第三周 類(lèi)的關(guān)系。 復(fù)合,委托,繼承

2019-11-09 21:11:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1. Composition復(fù)合

has a的關(guān)系,表示一個(gè)類(lèi)是另一個(gè)類(lèi)的成員變量,一個(gè)類(lèi)包含另一個(gè)類(lèi)class A;class B {public: B(){} ~B(){}PRivate: A a; int b;};

這里寫(xiě)圖片描述

構(gòu)造與析構(gòu)

構(gòu)造-由內(nèi)而外:B的構(gòu)造函數(shù)會(huì)首先調(diào)用A的默認(rèn)構(gòu)造函數(shù)(編譯器自己調(diào)用,如果需要傳遞參數(shù),需要在初始化列表顯示調(diào)用),然后在調(diào)用自己的構(gòu)造函數(shù) B::B(...):A(){...}析構(gòu)-由外而內(nèi):B的析構(gòu)函數(shù)首先執(zhí)行自己的,然后才調(diào)用A的析構(gòu)函數(shù) B::~B(...){... ~A()}

Adapter作用

新需求所要求的所有功能在一個(gè)已有的C類(lèi)中已經(jīng)全部實(shí)現(xiàn),但是C中功能繁多,此時(shí)可以設(shè)計(jì)一個(gè)類(lèi)D對(duì)C的功能進(jìn)行一次封裝,僅暴露需要的結(jié)構(gòu)結(jié)構(gòu),此時(shí)就非常適合Composition關(guān)系class C;class D{public: void func() { c.func(); }private: C c;};

2. Delegation委托

has a point類(lèi)的成員變量是另一個(gè)類(lèi)的指針,class A;class B {public: B(){} ~B(){}private: A *a; int b;};

這里寫(xiě)圖片描述

這種方法有個(gè)名詞pImpl(Pointer to IMPLementation),簡(jiǎn)單理解就是接口與實(shí)現(xiàn)分離

參考鏈接: 1. 明智地使用Pimpl 2. 編譯防火墻——C++的Pimpl慣用法解析

典型用例

C++11中的string就是用了這種方法,方法和實(shí)際實(shí)現(xiàn)分離,這樣就可以在兩個(gè)字符串相同的時(shí)候,就使用同一塊內(nèi)存,當(dāng)其中一個(gè)發(fā)生改變時(shí)就重新分配一塊內(nèi)存可以通過(guò)下列代碼進(jìn)行驗(yàn)證,但請(qǐng)確保是在C++11版本(因?yàn)槲抑粶y(cè)試了這個(gè)版本)#include <stdio.h>#include <string>using std::string;int main(int argc, char *argv[]){ string s1("123456789"); string s2(s1); string s3("123456789"); printf("s1=%p, s2=%p, s3=%p/n", s1.c_str() , s2.c_str(), s3.c_str()); s1 += "00"; printf("s1=%p, s2=%p, s3=%p/n", s1.c_str() , s2.c_str(), s3.c_str()); return 0;}----------------------s1=00FC2AAC, s2=00FC2AAC, s3=00FC2ACCs1=00FC3AF4, s2=00FC2AAC, s3=00FC2ACC

3. Inheritance繼承

繼承 is a,C++分為三種方式public,protected, private.使用最廣泛的是publicclass A{public: A(){} virtual ~A(){}}class B : public A{};

這里寫(xiě)圖片描述

構(gòu)造與析構(gòu)

構(gòu)造-由內(nèi)而外:B的構(gòu)造函數(shù)首先調(diào)用A的默認(rèn)構(gòu)造函數(shù),然后在執(zhí)行自己 B::B():A(){...};析構(gòu)-由外而內(nèi):B的析構(gòu)函數(shù)首先執(zhí)行自己,然后才調(diào)用A的析構(gòu)函數(shù) B::~B(...){...~A()};注意:基類(lèi)的析構(gòu)函數(shù)必須是virual的,否則會(huì)出現(xiàn)undefined behavior

4. 應(yīng)用

觀察者模式

委托+復(fù)合類(lèi)圖 這里寫(xiě)圖片描述代碼#include <stdio.h>#include <string>#include <vector>#include <iostream>using std::string;using std::vector;using namespace std;class Subject;class Observer{public: virtual void update(Subject *sub, int value) = 0;};class Subject{public: void attach(Observer*obs) { m_views.push_back(obs); } void set_val(int value) { m_value = value; notify(); } void notify() { for(int i = 0; i < m_views.size(); i++){ m_views[i]->update(this, m_value); } }private: int m_value; vector<Observer*> m_views;};class View : public Observer{public: void update(Subject *sub, int value) { m_watchValue = value; } void paintView() { cout << m_watchValue<< endl; }private: int m_watchValue;};int main(int argc, char *argv[]){ View v1; View v2; Subject s; s.attach(&v1); s.attach(&v2); s.set_val(100); v1.paintView(); v2.paintView(); cout << "-----------------------" << endl; s.set_val(200); v1.paintView(); v2.paintView(); return 0;}///////////////////////////----------------------100100-----------------------200200

組合模式Composite(結(jié)構(gòu)型)

參考鏈接: 設(shè)計(jì)模式(七)組合模式Composite(結(jié)構(gòu)型)組合模式(Composite Pattern )C++設(shè)計(jì)模式-Composite組合模式典型應(yīng)用場(chǎng)景 windows的文件夾與文件系統(tǒng),文件夾中又有文件類(lèi)圖 這里寫(xiě)圖片描述代碼class Component{public: Component(int val):m_value(val){} virtual void add(Component*){}private: int m_value;};class Primitive:public Component{public: Primitive(int val):Component(val){}};class Composite:public Component{public: Composite(int val):Component(val){} void add(Component *elem) { c.push_back(elem); }private: vector<Component*> c;};

原型模式prototype

參考鏈接 c++原型模式(Prototype)原型模式(Prototype)C++實(shí)現(xiàn)應(yīng)用場(chǎng)景 原型模式是通過(guò)已經(jīng)存在的對(duì)象的接口快速方便的創(chuàng)建新的對(duì)象。類(lèi)圖 這里寫(xiě)圖片描述代碼#include <iostream>using namespace std;enum imageType{ LSAT, SPOT};class Image{public: virtual void draw() = 0; static Image* findAndClone(imageType); virtual ~Image() {}protected: virtual imageType returnType() = 0; virtual Image *clone() = 0; static void addPrototype(Image *image) { _prototypes[_nextSlot++] = image; }private: static Image* _prototypes[10]; static int _nextSlot;};Image *Image::_prototypes[];int Image::_nextSlot;Image *Image::findAndClone(imageType type){ for(int i = 0 ; i < _nextSlot; i++) { if(_prototypes[i]->returnType() == type) { return _prototypes[i]->clone(); } } return NULL;}//////////////////////////////////////////////////////////////////////////class LandSatImage:public Image{public: imageType returnType() { return LSAT; } void draw() { cout << "LandSatImage::draw " << _id <<endl; } Image *clone() { return new LandSatImage(1); }protected: LandSatImage(int dummy) { _id = _count++; }private: static LandSatImage _landSatImage; LandSatImage(){ addPrototype(this); } int _id; static int _count;};LandSatImage LandSatImage::_landSatImage;int LandSatImage::_count = 1;//////////////////////////////////////////////////////////////////////////class SpotImage:public Image{public: imageType returnType() { return SPOT; } void draw() { cout << "SpotImage::draw "<< _id <<endl; } Image *clone() { return new SpotImage(1); }protected: SpotImage(int dummy) { _id = _count++; }private: SpotImage() { addPrototype(this); } static SpotImage _spotImage; int _id; static int _count;};SpotImage SpotImage::_spotImage;int SpotImage::_count = 1;//////mainconst int Num_IMAGES = 8;imageType input[Num_IMAGES] = { LSAT, LSAT, LSAT, LSAT, SPOT, SPOT, LSAT};int main(){ Image *images[Num_IMAGES]; int i = 0; for(i = 0; i < Num_IMAGES; i++) { images[i] = Image::findAndClone(input[i]); } for(i = 0; i < Num_IMAGES; i++) { images[i]->draw(); } for(i = 0; i < Num_IMAGES; i++) { delete images[i]; } return 0;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 贵阳市| 额济纳旗| 浦江县| 临夏市| 永宁县| 陕西省| 梅州市| 昌宁县| 西昌市| 尤溪县| 信阳市| 滨州市| 绥江县| 无棣县| 安义县| 中卫市| 永兴县| 彰化市| 巩义市| 叶城县| 乌拉特前旗| 台湾省| 苍南县| 翼城县| 灵川县| 什邡市| 永春县| 聊城市| 诸城市| 九江县| 巴林右旗| 来安县| 大冶市| 马公市| 惠水县| 朝阳市| 仁怀市| 武胜县| 巴林左旗| 吴桥县| 乾安县|