C + + Object-oriented advanced programming (next) first week note Geekband

Source: Internet
Author: User

1.Conversion function conversion Functions

Class fraction{

Public

operator double () const { //conversion is not possible to change members in a class, usually plus const

Return (double) (m_numerator/m_denominator);

}//converts a fraction object to a double type, double () cannot have parameters, return type does not have to be written

Private

int m_numerator;

int m_denominator;

};

Fraction f (3,5);

Double d=4+f;//call operator double () to convert F to 0.6

2.non-explict It one arguement construct

you can turn something else into something like this.

Class fraction{

Public

fraction (int num,int den=1): M_numerator (num), M_denominator (DEN) {}

Operator Double Const {

Return (double) (m_numerator/m_denominator);

}

Fraction operator+ (constfraction& f) {

Return

Fraction (... );

Private

int m_numerator;

int m_denominator;

};

Fraction f(3,5);

fractiond2=f+4;// calls non_explict ctor to convert 4 to fraction, and then calls operator+

Template

CALSS vector//template, each element in the container is a ool value

{

Public

typedef __bit_reference Reference;

Protected

Reference operator[] (size type N) {//represents something that was actually passed back.

return * (Begin () +diffenece_type (n));

}

struct __bit_reference

{

unsigned int* p;

unsigned int mask;

...

Public

operator BOOL () const {return! (*P * mask));}

};

3.pointer-like classes,

Template

Class shared_ptr{

Public

t& operator* () const

{

return *px;

}

t* operator-> () const{

return px;

}

shared_ptr (t* P):p x (p) {}

Private

t* px;//Pointer to T, what type are accepted

long* PN;

};

Lass foo{

...

void method (void) {...}

};

shared_ptr sp (new Foo);

Foo f (*SP);

Sp->method ();//Call method()

Px->method ();// Call this function from this object

The arrow symbol has a special behavior: The result of the action, the arrow symbol will continue to function

The smart pointer must have a general pointer inside, and the smart pointer must take the * and the symbol

Point-class This type

4 iterators

Used primarily to traverse containers

Template

struct _list_iterator//-linked list iterator

{

typedef _list_iterator Self;

typedef PTR POINTRT;

typedef REF Reference;

typedef _list_node* LINX_TYPE;//Pointers to nodes

Link_type node;

BOOL operator = = (const self& x) Const {return node = = X.node;}

BOOL operator! = (const self& x) const {return node! = X.node;}

Reference operator* () const {return (*node}.data;}

Pointer operator-> Const {return & (operator* ());}

self& operator++ () {node = (Link_type) ((*node). next); return *this;}

Self operator++ (int.) {self TMP = *this; ++*this; return tmp;}

self& operator--() {node = (Link_type) ((*node). Prev;return *this;}

Self operator--(int.) {self TMP = *this;--*this; return tmp;}

};

5 Function-like Class

Template

struct Identity:public unart_function{

Const t& operator () (const t& x) const {return x;}

};

Template

struct selectst{//take out the first one

Const TypeName pair::first_type& operator () (const pair& x) const

{

return x.first;

}

};

Template

Struct select2nd:publicunart_function{//takes out a second

Const TypeName pair::second_type& operator () (const pair& x) const

{

return x.second;

}

};

Template

struct pair:publicunart_function{

T1 first;

T2 second;

Pair (): First (T1 ()), second (T2 ()) {}

Pair (const t1& a,const t2& B): First (a), second (b) {}

};

6.classtemplate and fumction template function templates

template//class template for complex numbers

Class complex{

Public

Complex (T r=0,t i=0): Re (r), Im (i) {}

Complex & operator + = (const complex&);

T Real () const {return re;}

T imag () const {return im;}

Private

T Re,im;

Friend complex& __DOAPL (Complex*,const complex&);

};

{

Complex C1 (2.5,1.5);

Complex C2 (2,6);

}

Function templates:

Class stone{

Public

Stone (int w,int h,int We): _w (W), _h (h), _weight (We) {}

BOOL operator<< (const stone& RHS) Const {

return _weight < Rhs._weight;

}

Private

int _w,_h,_weight;

};

Template

Inline const t& min (const t& A, const t& b)

{return B;}

The function template does not have to specify the type when it is used , it can be launched by the arguments passed at the time of the call

Member template member Templates

Template

struct pair{

typedef T1 FIRST_TYPE;

typedef T2 SECOND_TYPE;

T1 first;

T2 second;

Pair (): First (T1 ()), second (T2 ()) {}

Pair (const T1 &a,const T2 &b)

: First (a), second (b) {}

Template//Member templates

When the pair (const pait &p)//T1 and T2 types are determined, U1 and U2 can also be determined

: First (P.first), second (P.second) {}

}

7. Template Special specialization

Template

struct hash ();

Template<>

struct hash{

size_t operator () (char x) const {return x;} Overload

};

Template<>

struct hash{

size_t operator () (int x) const {return x;}

};

Template<>

struct hash{

size_t operator () (long x) const {return x;}

};

C + + Object-oriented advanced programming (next) first week note Geekband

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.