【C++第八课】---操作符重载上
一、关于C和C++标准库的使用
我们都知道C++为了向下兼容C,和多库都是基于C来开发的,那么对于C++的库的使用需要注意什么地方呢?
1、C++标准库并不是C++语言的一部分
2、C++ 标准库是由C++语言编写而成的类库和函数的集合
3、C++标准库中定义的类和对象都位于std命名空间中
4、C++标准库的头文件都不带.h 后缀
5、C++ 标准库涵盖了C库的功能
6、C库中<name.h>头文件对应 头文件对应C++ 中的<cname>
7、C++标准库预定义了多数常用的数据结构,如:字符串, 链表,队列,栈等。
下面举两个例子说明使用C库和C++库的区别
#include <iostream>using namespace std;class Complex{private:int a;int b;public:Complex(int i = 0,int j = 0){a = i;b = j;}Complex operator+ (const Complex& c2);friend ostream& operator<< (ostream& out,const Complex& c);};Complex Complex::operator+ (const Complex& c2){Complex ret;ret.a = a + c2.a;ret.b = b + c2.b;return ret;}ostream& operator<< (ostream& out,const Complex& c){out<<c.a<<" + "<<c.b<<"i";return out;}int main(int argc,char** argv){Complex a(1,2);Complex b(1,2);Complex c = a + b;cout<<a<<endl;cout<<b<<endl;cout<<c<<endl;return 0;}
1、当无法修改左操作数的类时,使用全局函数进行重载
2、=, [], ()和->操作符只能通过成员函数进行重载
待续。。