这两天没事又翻了翻Primer,发现自己上岁数了,记单词能力开始下降,索引把一些简单的例子记下来,把一些肥肉剔除,剩一下骨头,方便今后Ctrl+F。
在此感谢:
http://ticktick.blog.51cto.com/823160/194307/
http://www.cnblogs.com/gaojun/archive/2010/09/10/1823354.html
http://www.cnblogs.com/uniqueliu/archive/2011/08/02/2125590.html
一、类的初始化--构造函数。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<iostream>#include<memory>#include<unistd.h>usingnamespacestd;classTree{public:        intdata;        intdata_arr[3];        doublehello;        //无参数构造函数        //编译系统对象分配内存空间,并自动调用该构造函数->由构造函数完成成员的初始化工作        Tree(void)        {           hello=9.823;           data=22;        }        //一般构造函数        Tree(doublea)        {           hello=a;         }        //复杂构造函数        Tree(constTree &c)        {      hello=c.hello;        }        //等号运算符重载构造函数        Tree &operator=(constTree &rt)        {           if(this==&rt)           {                 return*this;           }           this->hello=rt.hello;           this->data=rt.data;           return*this;        }private:        intprice;        intnum;};intmain(){  //一般构造  Tree a(233.33);   cout<<a.hello<<endl;  //复杂构造  Tree b(a);  cout<<b.hello<<endl;  //等号运算构造  Tree c=a;  cout<<c.hello<<endl;} | 
二、深、浅拷贝--防止DELETE二次错误。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | //深拷贝和浅拷贝#include<iostream>#include<memory>#include<string.h>#include<stdio.h>usingnamespacestd;classTree{public:        Tree(char*pN)        {           m_pName = newchar[strlen(pN) + 1];           if(m_pName)           {                        strcpy(m_pName ,pN);            }        }            Tree(Tree &p)        {                m_pName=newchar[strlen(p.m_pName)+ 1];                if(m_pName)                {                        strcpy(m_pName ,p.m_pName);                }        }        ~Tree()        {           deletem_pName;        }private:        char*m_pName;};intmain(){    Tree man("lujun");        Tree woman(man);} | 
三、友元---我的世界你不懂,你的世界我能懂
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<iostream>#include<memory>#include<string.h>#include<stdio.h>usingnamespacestd;classTV{public:        friendclassControl;        TV():volume(20){}        voidShow(TV &t)const;private:    intvolume;};classControl{public:        boolVolumeUp(TV &t);        boolVolumeDown(TV &t);         voidShow(TV &t)const;};boolControl::VolumeUp(TV &t){   t.volume++;}boolControl::VolumeDown(TV &t){   t.volume--;}voidControl::Show(TV&t)const{        cout<<"经遥控器调整的音量大小为:"<<t.volume<<endl;}voidTV::Show(TV&t)const{    cout<<"TV自身音量大小为:"<<t.volume<<endl;}intmain(){        Control c1;        TV t1;        c1.VolumeUp(t1);        c1.Show(t1);        c1.VolumeUp(t1);        c1.Show(t1);        c1.VolumeDown(t1);        c1.Show(t1);        t1.Show(t1);} | 
四、基类、派生、虚、多态----剪不断理还乱
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream>usingnamespacestd;//原始书籍类classQuote{public:        string isbn() const;        virtualdoublenet_price(intn) const;  //返回实际销售价格        Quote(void)        {           price=9.987;        }protected:        doubleprice;};string Quote::isbn() const{   string aa="abcd,world";   cout<<aa<<endl;   returnaa;}doubleQuote::net_price(intn) const{   cout<<n+20<<endl;   returnn+20;}//打折书籍类classBulkQuote:publicQuote{public:        BulkQuote()=default;        doublenet_price(intn) const;   //返回改动后的价格+自动覆盖};doubleBulkQuote::net_price(intn) const{        cout<<n+10<<endl;        cout<<"n_price="<<n*price<<endl;        returnn+10;}voidPrintAll(constQuote &book);  //根据实际传的类类型,进行动态鉴别intmain(){        Quote Father,*f;        BulkQuote Son,*s;        Father.isbn();        Father.net_price(100);        cout<<endl;        Son.isbn();        Son.net_price(100);        cout<<endl;        PrintAll(Father);        PrintAll(Son);}voidPrintAll(constQuote &book){    book.net_price(1000);}; | 
五、类模板、容器模板--真心方便
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <iostream>#include <vector>#include <list>usingnamespacestd;template<typenameT1,typenameT2>classTree{private:        T1 I;        T2 J;public:        Tree(T1 a,T2 b);        voidshow();};template<typenameT1,typenameT2>Tree<T1,T2>::Tree(T1 a,T2 b):I(a),J(b){}template<typenameT1,typenameT2>voidTree<T1,T2>::show(){        cout<<"I="<<I<<",J="<<J<<endl;}template<typenameX>voidprint(X v){   typenameX::iterator itor;   for(itor = v.begin(); itor != v.end(); ++itor)   {          cout<<*itor<<endl;   }}intmain(){        Tree<int,int> t1(33,55);        t1.show();        Tree<int,string> t2(99,"wenzhang");        t2.show();        Tree<double,string> t3(3.414,"mayili");        t3.show();        vector<int> v1;        v1.push_back(2);        v1.push_back(33);        v1.push_back(44);        print(v1);} | 
六、tuple类型--自己也能MongoDB
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream>#include <tuple>#include <vector>#include <list>usingnamespacestd;intmain(){        tuple<string,vector<double>,int,list<int> > aa("头条",{1.1,3.4},42,{3,4,5,6,7,9});    autoitem=make_tuple("play",3,99.239);<br>        cout<<get<0>(item)<<endl; } | 
原文:http://www.cnblogs.com/zacard-orc/p/3636959.html