1、使用C++标准库
C++标准库并不是C++语言的一部分,是由C++语言编写而成的类库和函数的集合;C++标准库中定义的对象都位于std命名空间中,C++标准库的头文件都不带.h后缀;C++标准库涵盖了C库的功能,C库中头文件对应C++中的.另外C++标准库预定义了多数常用的数据结构,如:字符串,链表,队列,栈等。
代码示例:
-
#include <cstdio>
-
using namespace std;
-
int main()
-
{
-
printf("Hello World!\n");
-
printf("Press any key to continue...");
-
getchar();
-
return 0;
-
}
-
#include <iostream>
-
using namespace std;
-
int main()
-
{
-
cout<<"Hello World"<<endl;
-
int x;
-
int y;
-
cout<<"1st Parameter: ";
-
cin>>x;
-
cout<<"2nd Parameter: ";
-
cin>>y;
-
cout<<"Result: "<<x+y<<endl;
-
return 0;
-
}
左移运算符<< 和右移运算符 >>
在C语言中只能用于整数运算,并且语义是确定不变的,然而在C++是怎么改变左移运算符和右移运算符的语义的?
2、操作符重载
操作符重载为操作符提供了不同的语义
-
#include <cstdlib>
-
#include <iostream>
-
using namespace std;
-
struct Complex
-
{
-
int a;
-
int b;
-
};
-
int main(int argc, char *argv[])
-
{
-
Complex c1 = {1, 2};
-
Complex c2 = {3, 4};
-
Complex c3 = c1 + c2;
-
cout << "Press the enter key to continue ...";
-
cin.get();
-
return EXIT_SUCCESS;
-
}
上面代码我们经过编译,发现并不能编译通过,解决这个问题,我们特意定义一个函数,修改后的代码如下:
-
#include <cstdlib>
-
#include <iostream>
-
using namespace std;
-
struct Complex
-
{
-
int a;
-
int b;
-
};
-
Complex add(const Complex& c1, const Complex& c2)
-
{
-
Complex ret;
-
ret.a = c1.a + c2.a;
-
ret.b = c1.b + c2.b;
-
return ret;
-
}
-
int main(int argc, char *argv[])
-
{
-
Complex c1 = {1, 2};
-
Complex c2 = {3, 4};
-
Complex c3 = add(c1, c2);
-
cout<<"c3.a = "<<c3.a<<endl;
-
cout<<"c3.b = "<<c3.b<<endl;
-
cout << "Press the enter key to continue ...";
-
cin.get();
-
return EXIT_SUCCESS;
-
}
这样我们就实现了两个复数的相加;可是我们能不能实现两个复数通过操作符+来实现两个复数的相加呢?在C++中支持了操作符重载,其本质是通过operator关键字可以利用函数扩展操作符,operator的本质是通过函数重载实现操作符重载。
-
#include <cstdlib>
-
#include <iostream>
-
using namespace std;
-
struct Complex
-
{
-
int a;
-
int b;
-
};
-
Complex add(const Complex& c1, const Complex& c2)
-
{
-
Complex ret;
-
ret.a = c1.a + c2.a;
-
ret.b = c1.b + c2.b;
-
return ret;
-
}
-
int main(int argc, char *argv[])
-
{
-
Complex c1 = {1, 2};
-
Complex c2 = {3, 4};
-
Complex c3 = add(c1, c2);
-
cout<<"c3.a = "<<c3.a<<endl;
-
cout<<"c3.b = "<<c3.b<<endl;
-
cout << "Press the enter key to continue ...";
-
cin.get();
-
return EXIT_SUCCESS;
-
}
3、用operator关键字扩展操作符用于类:
在C++中的private声明使得类的成员不能被外界访问,但是通过friend关键字可以例外的开放权限,这就是C++中的类的友元。
-
#include <cstdlib>
-
#include <iostream>
-
using namespace std;
-
class Complex
-
{
-
int a;
-
int b;
-
public:
-
Complex(int a = 0, int b = 0)
-
{
-
this->a = a;
-
this->b = b;
-
}
-
int getA()
-
{
-
return a;
-
}
-
int getB()
-
{
-
return b;
-
}
-
friend Complex operator+ (const Complex& c1, const Complex& c2);
-
};
-
Complex operator+ (const Complex& c1, const Complex& c2)
-
{
-
Complex ret;
-
ret.a = c1.a + c2.a;
-
ret.b = c1.b + c2.b;
-
return ret;
-
}
-
int main(int argc, char *argv[])
-
{
-
Complex c1(1, 2);
-
Complex c2(3, 4);
-
Complex c3 = c1 + c2;
-
cout << "Press the enter key to continue ...";
-
cin.get();
-
return EXIT_SUCCESS;
-
}
重载左移运算符范例:
-
#include <cstdlib>
-
#include <iostream>
-
using namespace std;
-
class Complex
-
{
-
int a;
-
int b;
-
public:
-
Complex(int a = 0, int b = 0)
-
{
-
this->a = a;
-
this->b = b;
-
}
-
int getA()
-
{
-
return a;
-
}
-
int getB()
-
{
-
return b;
-
}
-
friend Complex operator+ (const Complex& c1, const Complex& c2);
-
friend ostream& operator<< (ostream& out, const Complex& c);
-
};
-
ostream& operator<< (ostream& out, const Complex& c)
-
{
-
out<<c.a<<" + "<<c.b<<"i";
-
-
return out;
-
}
-
Complex operator+ (const Complex& c1, const Complex& c2)
-
{
-
Complex ret;
-
ret.a = c1.a + c2.a;
-
ret.b = c1.b + c2.b;
-
-
return ret;
-
}
-
int main(int argc, char *argv[])
-
{
-
Complex c1(1, 2);
-
Complex c2(3, 4);
-
Complex c3 = c1 + c2;
-
-
cout<<c1<<endl;
-
cout<<c2<<endl;
-
cout<<c3<<endl;
-
-
cout << "Press the enter key to continue ...";
-
cin.get();
-
return EXIT_SUCCESS;
-
}
4、小结
操作符重载是C++的强大特性之一,操作符重载的本质是通过函数扩展操作符的语义;operator关键字是操作符重载的关键,friend关键字可以对函数或类开发访问权限,操作符重载遵循函数重载的规则。
阅读(2430) | 评论(0) | 转发(0) |