[GeekBand][C++面向对象高级编程]第三周学习笔记

Object Oriented Programming(OOP)/Object Oriented Design(OOD)

  • Inheritance(继承)
  • Composition(复合)
  • Delegation(委托)

Composition(复合),表示 has-a

template >
class queue
{
    …
protected:
    Sequence c;     // 底层容器
public:
    // 以下完全利用 c 的操作函数完成
    bool empty()    const { return c.empty(); }
    size_type size()    const { return c.size(); }
    reference front() { return c.front(); }
    reference back() { return c.back(); }
    // deque 是两端可进出, queue 是末端进前端出(先进先出)
    void push(const value_type& x) { c.push_back(x); }
    void pop() { c.pop_front(); }
};

以上的设计模式被称为Adapter,将原有的类包装,只开放部分功能。
构造由内而外
Container 的构造函数首先调用 Component 的 default 构造函数,然后才执行自己。(若要执行非默认的构造函数,需要手动写出要调用的构造函数)
Container::Container(…): Component() { … };
析构由外而内
Container 的析构函数首先执行自己,然后才调用 Component 的析构函数。
Container::~Container(…) { … ~Component(); };

Delegation(委托). Composition by reference.

// file String.hpp
class StringRep;
class String
{
public:
    String();
    String(const char* s);
    String(const String& s);
    String& operator=(const String& s);
    ~String();
    …
private:
    StringRep* rep; // pimpl
};
// file String.cpp
#include "String.cpp"
namespace
{
    class StringRep
    {
        friend class String;
        StringRep(const char* s);
        ~StringRep();
        int count;
        char* rep;
    };
}

String::String() { …; }
…

以上的设计模式被称为Handle/Body(pImpl),里面包含一个指针指向实现全部功能的类。
count的功能是做reference counting

Inheritance(继承),表示 is-a

struct _List_node_base
{
    _List_node_base* _M_next;
    _List_node_base* _M_prev;
};

template
struct _List_node :public _List_node_base
{
    _Tp _M_data;
};

构造由内而外
Derived 的构造函数首先调用 Base 的 default 的构造函数,然后才执行自己。
Derived::Derived(…): Base() { … };
析构由外而内
Derived 的析构函数首先执行自己,然后才调用 Base 的析构函数。
Derived::~Derived(…) { … ~Base(); };
注意:base class 的 dtor 必须是 virtual ,否则会出现 undefined behavior。

Inheritance(继承) with virtual functions(虚函数)

  • non-virtual 函数:你不希望 derived class 重新定义(override,覆写)它。
  • virtual 函数:你希望derived class 重新定义(override,覆写)它,且你对它已有默认定义。
  • pure virtual 函数:你希望 derived class 一定要重新定义(override,覆写)它,你对它没有默认定义。
class Shape
{
public:
    virtual void draw() const = 0;                  // pure virtual
    virtual void error(const std::string& msg);     // impure virtual
    int objectID() const;                           // non-virtual
};

class Rectangle:public Shape{ … };
class Ellipse:public Shape{ … };

以下的设计模式被称为Template Method

#include 
using namespace std;


class CDocument
{
public:
    void OnFileOpen()
    {
        // 这是个算法,每个 cout 输出代表一个实际动作
        cout << "dialog..." << endl;
        cout << "check file status..." << endl;
        cout << "open file..." << endl;
        Serialize();
        cout << "close file..." << endl;
        cout << "update all views..." << endl;
    }

    virtual void Serialize() {  };
};

class CMyDoc :public CDocument
{
public:
    virtual void Serialize()
    {
        // 只有应用程序本身才知道如何读取自己的文件(格式)
        cout << "CMyDoc::Serialize()" << endl;
    }
};
int main()
{
    CMyDoc myDoc;   // 假设对应[File/Open]
    myDoc.OnFileOpen();
}

Inheritance+Composition 关系下的构造和析构

测试代码如下:

#include 
class Base;
class Component;
class Derived;

class Base
{
public:
    Base()
    {
        std::cout << "The ctor of class Base has completed" << std::endl;
    }
};
class Component
{
public:
    Component()
    {
        std::cout << "The ctor of class Component has completed" << std::endl;
    }
};
class Derived :public Base
{
public:
    Derived(int n) :n(n)
    {
        std::cout << "The ctor of class Derived has completed" << std::endl;
    }
private:
    int n;
    Component a;
};

int main()
{
    Derived d(2);
    system("pause");
    return 0;
}

你可能感兴趣的:([GeekBand][C++面向对象高级编程]第三周学习笔记)