1、命名空间
namespace cq{}
using namespace cq;
2、使用继承
class Man: public Person { }
3、使用父类的方法
Man::Man(char*name) :Person(name, 12){cout << "Man name is:" << this->name << " and age is:" <<this->age<< endl;}
4、析构函数
(1)若用指针新建对象
要用delete销毁,销毁后调用~析构
(2)若不用指针新建对象
在碰到第一个}后,,调用~析构
5、调用父类的方法
(1)直接调用
Person::eat();
(2)对象调用
Man*m = new Man("ganer");m->Person::eat();
6、虚函数和纯虚函数的区别
(1)虚函数:virtual void eat();
在继承中使用的时候,加virtual可与java使用方法一致
(2)纯虚函数:virtual void run()=0;
与java中抽象方法使用一样。
7、运算符的重载
void operator+=(Point p) {this->x += p.x;this->y += p.y;}
Point p(10, 10);p += Point(6, 6);cout << p.x << endl;
8、伪函数
class H {public:void operator()() {cout << "In op" << endl;}};
H h;h();
9、友元
class A {friend class B;private:int num=9;};class B: public A {public:void show() {cout << num << endl;}};
10、容器的使用
(1)List
list<string>l;l.push_back("kaishi");l.push_back("jiesu");list<string>::iterator it;for( it=l.begin();it!=l.end();it++){cout<<*it<<endl;}
(2)Map
map<string, string> m;m.insert(pair<string,string>("hello","haha"));m.insert(pair<string,string>("jiayou","hehe"));cout << m.at("hello") << endl;
11、字符串的操作
string s1="haha ";string s2="hehe ";string s=s1+s2;cout<<s<<endl;stringstream st;st<<"mingtian ";st<<"huijia ";cout<<st.str()<<endl;
12、文件的操作
ofstream of("data.txt");of << "Hello";of.close();ifstream inf("data.txt");stringbuf sb;inf >> &sb;cout << sb.str();ifstream ins("data.txt");char c;ins >> c;cout << c << endl;
每一发奋努力的背后,必有加倍的赏赐。