【C/C++学习】C++语言学习积累

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;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值