拾人牙慧,浅记一些C++的类
这两天没事又翻了翻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
一、类的初始化--构造函数。
#include<iostream> #include<memory> #include<unistd.h> using namespace std; class Tree { public: int data; int data_arr[3]; double hello; //无参数构造函数 //编译系统对象分配内存空间,并自动调用该构造函数->由构造函数完成成员的初始化工作 Tree(void) { hello=9.823; data=22; } //一般构造函数 Tree(double a) { hello=a; } //复杂构造函数 Tree(const Tree &c) { hello=c.hello; } //等号运算符重载构造函数 Tree &operator=(const Tree &rt) { if(this==&rt) { return *this; } this->hello=rt.hello; this->data=rt.data; return *this; } private: int price; int num; }; int main() { //一般构造 Tree a(233.33); cout<<a.hello<<endl; //复杂构造 Tree b(a); cout<<b.hello<<endl; //等号运算构造 Tree c=a; cout<<c.hello<<endl; }
二、深、浅拷贝--防止DELETE二次错误。
//深拷贝和浅拷贝 #include<iostream> #include<memory> #include<string.h> #include<stdio.h> using namespace std; class Tree { public: Tree(char *pN) { m_pName = new char[strlen(pN) + 1]; if(m_pName) { strcpy(m_pName ,pN); } } Tree(Tree &p) { m_pName=new char[strlen(p.m_pName)+ 1]; if(m_pName) { strcpy(m_pName ,p.m_pName); } } ~Tree() { delete m_pName; } private: char *m_pName; }; int main() { Tree man("lujun"); Tree woman(man); }
三、友元---我的世界你不懂,你的世界我能懂
#include<iostream> #include<memory> #include<string.h> #include<stdio.h> using namespace std; class TV { public: friend class Control; TV():volume(20){} void Show(TV &t)const; private: int volume; }; class Control { public: bool VolumeUp(TV &t); bool VolumeDown(TV &t); void Show(TV &t)const; }; bool Control::VolumeUp(TV &t) { t.volume++; } bool Control::VolumeDown(TV &t) { t.volume--; } void Control::Show(TV&t)const { cout<<"经遥控器调整的音量大小为:"<<t.volume<<endl; } void TV::Show(TV&t)const { cout<<"TV自身音量大小为:"<<t.volume<<endl; } int main() { 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); }
四、基类、派生、虚、多态----剪不断理还乱
#include <iostream> using namespace std; //原始书籍类 class Quote { public: string isbn() const; virtual double net_price(int n) const; //返回实际销售价格 Quote(void) { price=9.987; } protected: double price; }; string Quote::isbn() const { string aa="abcd,world"; cout<<aa<<endl; return aa; } double Quote::net_price(int n) const { cout<<n+20<<endl; return n+20; } //打折书籍类 class BulkQuote:public Quote { public: BulkQuote()=default; double net_price(int n) const ; //返回改动后的价格+自动覆盖 }; double BulkQuote::net_price(int n) const { cout<<n+10<<endl; cout<<"n_price="<<n*price<<endl; return n+10; } void PrintAll(const Quote &book); //根据实际传的类类型,进行动态鉴别 int main() { 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); } void PrintAll(const Quote &book) { book.net_price(1000); };
五、类模板、容器模板--真心方便
#include <iostream> #include <vector> #include <list> using namespace std; template<typename T1,typename T2> class Tree { private: T1 I; T2 J; public: Tree(T1 a,T2 b); void show(); }; template <typename T1,typename T2> Tree<T1,T2>::Tree(T1 a,T2 b):I(a),J(b){} template <typename T1,typename T2> void Tree<T1,T2>::show() { cout<<"I="<<I<<",J="<<J<<endl; } template <typename X> void print(X v) { typename X::iterator itor; for (itor = v.begin(); itor != v.end(); ++itor) { cout<<*itor<<endl; } } int main() { 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
#include <iostream> #include <tuple> #include <vector> #include <list> using namespace std; int main() { tuple<string,vector<double>,int,list<int> > aa("头条",{1.1,3.4},42,{3,4,5,6,7,9}); auto item=make_tuple("play",3,99.239);
cout<<get<0>(item)<<endl; }