C++:复习笔记2

92 阅读3分钟

operator type() const explicit template specialization


inheritance(继承) composition(复合) delegation(委托)

  • composition(复合) has-a queue |--->deque
Adapter 适配器

template<class T>
class queue{ //Adapter 适配器 deque 功能已经很强大,queue只需要开放其中一部分功能
...
protected:
  deque<T> c; //底层容器
public:
  //以下完全利用c的操作函数完成
  bool empty() const{return c.empty();}
  size_type size() const {return c.size();}
  reference fron(){return c.front();}
  //
  void push(const value_type&x){c.push_back(x);}
  void pop(){c.pop_front();}
}
  • composition(符合)下的构造和析构

container|--->component

  1. 构造由内而外

container的够着函数首先调用component的default够着函数 然后才执行自己的。

Container::Container(...):component(){...};
                         ————————————
                         这部分是编译器
                         自己加的
  1. 析构由外到内

container的析构函数首先执行自己,然后才调用component的 析构函数。

container::~container{..~component()};
                        ———————————
                        编辑以自己加
                        的
  • delegation(委托).composition by reference
class string{
publicstring();
    string(const char* s);
    string(const string& s);
    string &operator=(const string& s);
    ~string();
...
private:
   stringRep* rep;//pimpl 委托指针
}

string --->stringrep

  • inheritance(继承),is-a
struct _List_node_base
{
  _List_node_base* _M_next;
  _List_node_base* _M_prev;
};

template<typename _Tp>
struct _List_node: public _List_node_base
{
 _Tp _M_data;
};


__List_node_base
     /\
     |
     |
 _List_node
  • inheritance(继承)关系下的的构造和析构
 base    --->基类的析构函数必须是virtual,       
  /\
  |
  |
derived
  1. 构造由内而外

derived的构造函数首先调用base的default构造函数 然后执行自己

derived::derived(...):base(){....};

  1. 析构函数由外到内

derived的析构函数先执行,然后调用base的析构函数。 注意要写virtual

derived::derived(...){...~base()};


  • 虚函数和多态

inheritance with vritual funcations

  1. no-virtual函数:你不希望derived class重新定义(override)它
  2. vitural函数:你希望derived class重新定义(overriade,重写)它,它已经有自己的定义
  3. pure virtual函数:你希望derived class 一定要重写它(overrade),没有默认定义
class shape{
publicvirtual void draw() const=0;
  virtual void error(const std::string& msg);
  int objectid() const;
  ...
}
  • Template method 非常重要
cdocument::serialize为一个纯虚函数

cdocument::
onfileopen()
{
  ...
  serialize()
  ...
}

class cmydoc:public cdocument
{
 virtual serialize(){...}
}

main()
{
 cmydoc mydoc;
 mydoc.onfileopen()

}

实际测试代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

class base
{
public:
  virtual void base_1() const
  {
   cout<<"in base"<<endl;
  }
  void process() const
  {
    cout<<"test in base begin"<<endl;
    base_1();//框架写好了,子函数实现
    cout<<"test in base end"<<endl;
  }
};




class myclass:public base
{
public:
  void base_1() const //virtual 可写可不写
  {
    cout<<"in myclass"<<endl;
  }

};



int main()
{
 //method 1:template method
 //base_1方法子类做了重新定义
 myclass my; //定义一个子类对象
 my.process();//实际上做this->process 然后this->base_1,vptr指针
  
 //method 2:父类指针指向子类对象
 base* test = &my; 
 test->base_1();
 
 //父类自己的实现
 base mybase;
 mybase.process();
 
}

结果为:
test in base begin
in myclass
test in base end
in myclass
test in base begin
in base
test in base end

  • component+inheritnce
 base(1)
  |
  |
derived(3) |--->component(2)
构造谁再前?已经标号

 base(2) |--->component(1)
   |
   |
derived(3)

构造谁再前?已经标号
  • 观察者 delegation + inheritance
         n
subject---->observer
               |
               |
            具体的obs 
class subject
{
  int m_value;
  vetor<observer*>m_views;
  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_view.size();++i)
			m_view[i]->update(this,m_value);  
  }  
}

class observer
{
public:
 virtual void update(subject* sub,int value) = 0;
}

const 函数,指定this中的对象不能更改

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;


class base{
public:
  int* p;
  base():p(NULL)
  {}
  ~base()
  {
    free(p);
  }

};



class test{
  public:
    base* p;
    const int   t;
    test():t(1)
    {
     p = new base();

    }
    void change() const
    {

     (this->p)->p = new int(4); //只要不改变this->p的值即可,指向对象的值可以更改
     cout<<*((this->p)->p)<<endl;
    }
    ~test()
   {
    delete p;
   }

};


int main()
{
  test a;
  a.change();
  base b;
}