C + + NetEase cloud Classroom Development Engineer--combination and inheritance

Source: Internet
Author: User

A chapter mainly around: inheritance , composite , commissioned Three aspects of the introduction.

Composite: Represents has-a

Template<class T, class Sequence = deque<t>> queue<>------------------>deque

The class queue{ queue contains deque

Protected: Both ends of the deque can be in and out, and the queue can enter and exit

    Sequence C; Bottom container

Public

The following fully utilizes the C operation function to complete

BOOL Empty () const {return c.empty ();}

Size_type size () const {return c.size ();}

Reference Front () {return C.front ();}

Reference back () {return c.back ();}

Deque is both ends can be in and out, the queue is the end of FIFO

void push (const value_type& push_back (x);)

void Pop () {C.pop_front ();}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compound, refers to the function of a class A contained in another class B,a is fully implemented by Class B.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From the memory point of view, understand the composition.

sizeof:40 sizeof:16*2 + 4 + 4 sizeof:4*4

Template <class t> template <class t> template <class t>

Class queue{class deque{class Ttr{

protected:protected:t* cur;

    deque<t>            C             itr<t> start; t* first;

} itr<t> finish; T* last;

t** map; t** node;

unsigned int map_size; }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Construction and destructor under compound relation

Container<*>--------->component

(Large memory) (low memory)

constructor from Inside Out

The constructor of container first calls the defaultconstructor of component, and then performs its own function.

Container::container (...): Component () {...}

The destructor is out of the inside

The destructor of the container first executes itself before the destructor of the component is called.

Container::~container (...) {... ~component ()}

Delegation (delegate). Composition by reference

Class Stringrep; #include "string.hpp"

Class string{namespace{

Public:class stringrep{

String (); Friend class String;

String (const char* s); Stringrep (const char* s);

String (const string& s); ~stringrep ();

String &operator = (const sting& s); int count;

~string (); Char* Rep;

Private:};

    stringrep* Rep; }                       

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unlike compositing, when the class on the left is created, the class on the right does not necessarily need to be created.

The right side of the change does not affect the class on the left. or, as a compiled firewall, when the right class changes, the compiler compiles the right class.

Inheritance (inheritance), indicating 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;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Constructed from the Inside out

The derived constructor first calls the base's default constructor before performing its own function.

Derived::D erived (...): Base () {...}

The destruction by the outer and inner

The destructor for derived first executes itself before the destructor of base is called.

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

The destructor for base Class must be virtual or it will appear undefined behavior

C + + NetEase cloud Classroom Development Engineer--combination and inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.