zoukankan      html  css  js  c++  java
  • 操作符重载举例

    一.输入输出

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Complex
     5 {
     6 public:
     7     Complex()
     8     {
     9         real = 0;
    10         imag = 0;
    11     }
    12     
    13     Complex(double r)
    14     {
    15         real = r;
    16         imag = 0;
    17     }
    18 
    19     Complex(double r, double i)
    20     {
    21         real = r;
    22         imag = i;
    23     }
    24 
    25     Complex operator+(const Complex &c) const
    26     {
    27         Complex s;
    28         s.real = real + c.real;
    29         s.imag = imag + c.imag;
    30         return s;
    31     }
    32 
    33     Complex operator-(const Complex &c)
    34     {
    35         Complex s;
    36         s.real = real - c.real;
    37         s.imag = imag - c.imag;
    38         return s;
    39     }
    40 
    41     double real, imag;
    42 };
    43 
    44 istream& operator>>(istream &in, Complex &c)
    45 {
    46     return in >> c.real >> c.imag;
    47 }
    48 ostream& operator<<(ostream& out, Complex &c);
    49 int main()
    50 {
    51     Complex s;
    52     cin >> s;//1 2
    53     cout << s.real << " " << s.imag << endl;//1 2
    54     return 0;
    55 }

    二.赋值

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Point
     5 {
     6 public:
     7 
     8     Point()
     9     {
    10         x = 0;
    11         y = 0;
    12     }
    13 
    14     Point(int a, int b)
    15     {
    16         x = a;
    17         y = b;
    18     }
    19 
    20     Point& operator=(const Point& p)
    21     {
    22         x = p.x;
    23         y = p.y;
    24         return *this;
    25     }
    26     int x, y;
    27 };
    28 
    29 ostream& operator<<(ostream &out, Point p)
    30 {
    31     return out << p.x << " " << p.y << endl;
    32 }
    33 
    34 
    35 
    36 int main()
    37 {
    38     Point p1, p2, p3(3, 4);
    39     p1 = p2 = p3;
    40     cout << p1 << p2;
    41     /*
    42     3 4
    43     3 4
    44     */
    45     return 0;
    46 }
     1 #include <iostream>
     2 using namespace std;
     3 
     4 class String
     5 {
     6 public:
     7     String(const char *s)
     8     {
     9         length = strlen(s);
    10         p = new char[length + 1];
    11         strcpy(p, s);
    12     }
    13 
    14 private:
    15     int length;
    16     char *p;
    17 };
    18 
    19 void fun(String &aa)
    20 {
    21     String c("789");
    22     c = aa;
    23 }
    24 
    25 
    26 int main()
    27 {
    28     String a("1"), b("x");
    29     b = a;//内存泄漏,b.p没有释放
    30     fun(a);//a.p将指向一个未定义区域,指针悬浮
    31     a = a;//检测两边是否相等
    32     return 0;
    33 }
     1 const String& String::operator=(const String &right)
     2 {
     3     if (this != &right)//防止自我赋值
     4     {
     5         delete[] p;//防止内存泄漏
     6         length = right.length;//取新字符串长度
     7         p = new char[length + 1];//分配内存
     8         for (int i = 0; i < length; i++)
     9         {
    10             p[i] = right.p[i];//复制
    11         }
    12     }
    13     return *this;
    14 }
    1 Complex& operator=(string str)
    2 {
    3     char *p = new char[str.length + 1];
    4     strcpy(p, str.c_str());
    5     r = atof(strtok(p, "+"));
    6     i = atof(strtok(NULL, "i"));
    7     return *this;
    8 }

    三.下标操作符

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class CharArray
     5 {
     6 public:
     7     CharArray(int len)
     8     {
     9         length = len;
    10         p = new char[length];
    11     }
    12 
    13     char& operator[](int i)
    14     {
    15         static char ch = 0;
    16         if (i >= 0 && i < length)
    17         {
    18             return p[i];
    19         }
    20         else
    21         {
    22             cout << "
    out" << endl;
    23             return ch;
    24         }
    25     }
    26 
    27 private:
    28     int length;
    29     char *p;
    30 };
    31 
    32 int main()
    33 {
    34     CharArray str1(6);
    35     char *str2 = "abcdefg";
    36     for (int i = 0; i < 7; i++)
    37     {
    38         str1[i] = str2[i];//str2[6]保存在ch里
    39     }
    40     for (int i = 0; i < 7; i++)
    41     {
    42         cout << str1[i];
    43     }
    44     cout << endl;
    45     /*
    46     out
    47     abcdef
    48     out
    49     g
    50     */
    51     return 0;
    52 }

    四.函数调用操作符

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class F
     5 {
     6 public:
     7     double operator()(double x, double y) const
     8     {
     9         return 2 * x + y;
    10     }
    11 };
    12 
    13 int main()
    14 {
    15     F f;
    16     cout << f(1.5, 3.2) << endl;//6.2
    17     return 0;
    18 }
     1 #include <iostream>
     2 using namespace std;
     3 
     4 class  Array
     5 {
     6 public:
     7     Array()
     8     {
     9         num = new int[1];
    10     }
    11     Array(int i, int j)
    12     {
    13         row = i;
    14         col = j;
    15         num = new int[row * col];
    16     }
    17     int* operator[](int i)
    18     {
    19         return &num[i * col];
    20     }
    21     int operator()(int i, int j)
    22     {
    23         return num[i * col + j];
    24     }
    25     ~Array()
    26     {
    27         delete[] num;
    28     }
    29 
    30 private:
    31     int *num, row, col;
    32 };
    33 
    34 int main()
    35 {
    36     Array a(3, 4);
    37     for (int i = 0; i < 3; i++)
    38     {
    39         for (int j = 0; j < 4; j++)
    40         {
    41             a[i][j] = 1;
    42         }
    43     }
    44     cout << a(1, 1) << endl; // 1
    45     return 0;
    46 }

    五.自增/自减

    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
        A()
        {
            value = 0;
        }
        A(int t)
        {
            value = t;
        }
        A operator++()
        {
            value++;
            return *this;
        }
        A operator++(int)
        {
            A t;
            t.value = value++;
            return t;
            /*
            A a = *this;
            ++(*this);
            return a;
            */
        }
        int value;
    };
    
    ostream& operator<<(ostream &out, A a)
    {
        return out << a.value << endl;
    }
    
    int main()
    {
        A a(10);
        cout << a++; // 10
        cout << ++a; // 12
        return 0;
    }

    五.转型操作符

    声明中不能包含形参和返回类型,但函数体中必须包含return语句,用来返回转型结果。

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     A(float num)
     8     {
     9         value = num;
    10     }
    11     operator int()
    12     {
    13         return static_cast<int>(value) + 10;
    14     }
    15 
    16     float value;
    17 };
    18 
    19 int main()
    20 {
    21     A a(8.0);
    22     int i = a;
    23     cout << i << endl;//18
    24     return 0;
    25 }

    六.内存管理

    new, new[], delete, delete[]

    void *类::operator new(size_t size);第一个参数必须是size_t,数值等于将被创建的对象大小

    void 类::operator delete(void *p);第一个参数必须是void *

  • 相关阅读:
    DZY Loves Sequences
    Boredom easy dp
    floyd算法 poj1125
    poj3259 Bellman_Ford算法
    poj1860 Bellman_Ford算法
    Java 获取资源文件路径
    CLion 2020.1.2 激活
    Kotlin学习笔记
    Kotlin Hello World
    WebStorm 2020.1.2 激活
  • 原文地址:https://www.cnblogs.com/wanderingzj/p/5296154.html
Copyright © 2011-2022 走看看