Chinaunix首页 | 论坛 | 博客
  • 博客访问: 250586
  • 博文数量: 52
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1538
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-24 07:45
个人简介

生活就像海洋,只有意志坚强的人,才能到达彼岸。

文章存档

2013年(52)

分类: C/C++

2013-10-11 09:28:25

1、使用C++标准库
C++标准库并不是C++语言的一部分,是由C++语言编写而成的类库和函数的集合;C++标准库中定义的对象都位于std命名空间中,C++标准库的头文件都不带.h后缀;C++标准库涵盖了C库的功能,C库中头文件对应C++中的.另外C++标准库预定义了多数常用的数据结构,如:字符串,链表,队列,栈等。
代码示例:

点击(此处)折叠或打开

  1. #include <cstdio>
  2. using namespace std;
  3. int main()
  4. {
  5.     printf("Hello World!\n");
  6.     printf("Press any key to continue...");
  7.     getchar();
  8.     return 0;
  9. }



点击(此处)折叠或打开

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     cout<<"Hello World"<<endl;
  6.     int x;
  7.     int y;
  8.     cout<<"1st Parameter: ";
  9.     cin>>x;
  10.     cout<<"2nd Parameter: ";
  11.     cin>>y;
  12.     cout<<"Result: "<<x+y<<endl;
  13.     return 0;
  14. }


左移运算符<< 和右移运算符 >> 在C语言中只能用于整数运算,并且语义是确定不变的,然而在C++是怎么改变左移运算符和右移运算符的语义的?
2、操作符重载
操作符重载为操作符提供了不同的语义

点击(此处)折叠或打开

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. struct Complex
  5. {
  6.     int a;
  7.     int b;
  8. };
  9. int main(int argc, char *argv[])
  10. {
  11.     Complex c1 = {1, 2};
  12.     Complex c2 = {3, 4};
  13.     Complex c3 = c1 + c2;
  14.     cout << "Press the enter key to continue ...";
  15.     cin.get();
  16.     return EXIT_SUCCESS;
  17. }


上面代码我们经过编译,发现并不能编译通过,解决这个问题,我们特意定义一个函数,修改后的代码如下:

点击(此处)折叠或打开

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. struct Complex
  5. {
  6.     int a;
  7.     int b;
  8. };
  9. Complex add(const Complex& c1, const Complex& c2)
  10. {
  11.     Complex ret;
  12.     ret.a = c1.a + c2.a;
  13.     ret.b = c1.b + c2.b;
  14.     return ret;
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18.     Complex c1 = {1, 2};
  19.     Complex c2 = {3, 4};
  20.     Complex c3 = add(c1, c2);
  21.     cout<<"c3.a = "<<c3.a<<endl;
  22.     cout<<"c3.b = "<<c3.b<<endl;
  23.     cout << "Press the enter key to continue ...";
  24.     cin.get();
  25.     return EXIT_SUCCESS;
  26. }


这样我们就实现了两个复数的相加;可是我们能不能实现两个复数通过操作符+来实现两个复数的相加呢?在C++中支持了操作符重载,其本质是通过operator关键字可以利用函数扩展操作符,operator的本质是通过函数重载实现操作符重载。

点击(此处)折叠或打开

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. struct Complex
  5. {
  6.     int a;
  7.     int b;
  8. };
  9. Complex add(const Complex& c1, const Complex& c2)
  10. {
  11.     Complex ret;
  12.     ret.a = c1.a + c2.a;
  13.     ret.b = c1.b + c2.b;
  14.     return ret;
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18.     Complex c1 = {1, 2};
  19.     Complex c2 = {3, 4};
  20.     Complex c3 = add(c1, c2);
  21.     cout<<"c3.a = "<<c3.a<<endl;
  22.     cout<<"c3.b = "<<c3.b<<endl;
  23.     cout << "Press the enter key to continue ...";
  24.     cin.get();
  25.     return EXIT_SUCCESS;
  26. }


3、用operator关键字扩展操作符用于类:
在C++中的private声明使得类的成员不能被外界访问,但是通过friend关键字可以例外的开放权限,这就是C++中的类的友元。

点击(此处)折叠或打开

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. class Complex
  5. {
  6.     int a;
  7.     int b;
  8. public:
  9.     Complex(int a = 0, int b = 0)
  10.     {
  11.         this->a = a;
  12.         this->b = b;
  13.     }
  14.     int getA()
  15.     {
  16.         return a;
  17.     }
  18.     int getB()
  19.     {
  20.         return b;
  21.     }
  22.         friend Complex operator+ (const Complex& c1, const Complex& c2);
  23. };
  24.     Complex operator+ (const Complex& c1, const Complex& c2)
  25.     {
  26.         Complex ret;
  27.         ret.a = c1.a + c2.a;
  28.         ret.b = c1.b + c2.b;
  29.         return ret;
  30.     }
  31. int main(int argc, char *argv[])
  32. {
  33.         Complex c1(1, 2);
  34.         Complex c2(3, 4);
  35.         Complex c3 = c1 + c2;
  36.         cout << "Press the enter key to continue ...";
  37.         cin.get();
  38.         return EXIT_SUCCESS;
  39. }


重载左移运算符范例:

点击(此处)折叠或打开

  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;
  4. class Complex
  5. {
  6.     int a;
  7.     int b;
  8. public:
  9.     Complex(int a = 0, int b = 0)
  10.     {
  11.         this->a = a;
  12.         this->b = b;
  13.     }
  14.     int getA()
  15.     {
  16.         return a;
  17.     }
  18.     int getB()
  19.     {
  20.         return b;
  21.     }
  22.     friend Complex operator+ (const Complex& c1, const Complex& c2);
  23.     friend ostream& operator<< (ostream& out, const Complex& c);
  24. };
  25. ostream& operator<< (ostream& out, const Complex& c)
  26. {
  27.     out<<c.a<<" + "<<c.b<<"i";

  28.     return out;
  29. }
  30. Complex operator+ (const Complex& c1, const Complex& c2)
  31. {
  32.     Complex ret;
  33.     ret.a = c1.a + c2.a;
  34.     ret.b = c1.b + c2.b;

  35.     return ret;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39.     Complex c1(1, 2);
  40.     Complex c2(3, 4);
  41.     Complex c3 = c1 + c2;

  42.     cout<<c1<<endl;
  43.     cout<<c2<<endl;
  44.     cout<<c3<<endl;

  45.     cout << "Press the enter key to continue ...";
  46.     cin.get();
  47.     return EXIT_SUCCESS;
  48. }


4、小结
操作符重载是C++的强大特性之一,操作符重载的本质是通过函数扩展操作符的语义;operator关键字是操作符重载的关键,friend关键字可以对函数或类开发访问权限,操作符重载遵循函数重载的规则。
阅读(2370) | 评论(0) | 转发(0) |
0

上一篇:类的静态成员

下一篇:没有了

给主人留下些什么吧!~~