• C++--第12课


    第12课 - 操作符重载 - 上

    1. 使用C++标准库

    C++标准库并不是C++语言的一部分。C++标准库是由C++语言编写而成的类库和函数的集合。C++标准库中定义的类和对象都位于std命名空间中。C++标准库的头文件都不带.h后缀。C++标准库涵盖了C库的功能。C库中<name.h>头文件对应C++中的<cname>。

    C++标准库预定义了多数常用的数据结构,如:字符串,链表,队列,栈等:

    <bitset>

    <deque>

    <list>

    <map>

    <queue>

    <set>

    <stack>

    <vector>

    例子:

    #include <cstdio>

    using namespace std;

    int main()

    {

             printf("Hello World! ");

             printf("Press any key to continue...");

             getchar();

             return 0;

    }

    现实中的显示器对应于C++标准课中的显示器对象。

    现实中的键盘对象对应于C++标准库中的键盘对象。

    #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;

    }

    2. 操作符重载

    左移运算符 << 和 右移运算符 >> 在C语言中只能用于整数运算,并且语义是确定不变的。在C++中有了新的定义。

    操作符重载为操作提供了不同的语义。

     #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 = c1 + c2;    加法不能用在结构体。

        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;

    }

    思考:

    add函数可以解决Complex变量相加的问题,但是Complex是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同。为什么不能让 + 操作符也支持复数相加呢?

    3. C++中操作符重载的本质

    C++中通过operator关键字可以利用函数扩展操作符。

    operator的本质是通过函数重载实现操作符重载。

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    struct Complex

    {

        int a;

        int b;

    };

    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<<"c3.a = "<<c3.a<<endl;

        cout<<"c3.b = "<<c3.b<<endl; 

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    运行结果和之前的例子的运行结果一样。

    l  用operator关键字扩展的操作符可以用于类吗?

    4. C++中的友元

    private声明使得类的成员不能被外界访问

    但是通过friend关键字可以例外的开放权限

    #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; 

    //  cin>>c3.a; 

    //  cin>>c3.b;

    //  主函数main不能被设置成为有函数的,所以无法访问类的私有空间。

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    5. 代码—重载左移运算符

    #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)

    //ostream就是我们用的cin 和cout 关键字的类型。

    {

        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;

    }

    运行结果:

    1 + 2i

    3 + 4i

    4 + 6i

    小结:

    操作符重载是C++的强大特性之一。

    操作符重载的本质是通过函数扩展操作符的语义。

    operator关键字是操作符重载的关键。

    friend关键字可以对函数或类开发访问权限。

    操作符重载遵循函数重载的规则。

  • 相关阅读:
    ACM_“IP地址”普及(进制转换)
    goj 堆箱子咯(栈)
    ACM_夏天到了,又到了出游的季节
    ACM_迟到的祝福(四)
    ACM_梦中的函数
    ACM_ZHANGZHANG喜欢手表
    ACM_出题人这样不好吧
    ACM_Mystery
    ACM_Repeating Characters
    P1160 队列安排 luogu洛谷
  • 原文地址:https://www.cnblogs.com/free-1122/p/11336199.html
Copyright © 2020-2023  润新知