cari

Rumah  >  Soal Jawab  >  teks badan

c++ - 友元函数运算符重载参数表中如何写多个参数一起进行操作

PHP中文网PHP中文网2817 hari yang lalu601

membalas semua(1)saya akan balas

  • 大家讲道理

    大家讲道理2017-04-17 13:48:26

    一般不会(a + b).area(); 这样调用,用起来不自然。
    而是会实现类似下面的一个重载函数

    1

    2

    3

    4

    5

    6

    7

    8

    9

    <code>    friend ostream &operator<<(ostream &os, const Triangle &b) {

            float s, area;

            s = (b.x + b.y + b.z) / 2;

            area = sqrt(s * (s - b.x) * (s - b.y) * (s - b.z));

    //        cout << "area is " << area << endl;

            os << "area is " << area <<endl;

            return os;

        }

        </code>

    这样,打印的时候只要 cout << a + b << endl;即可
    代码稍微改了下,具体的算法没看,

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    <code>#include <iostream>

    #include <math.h>

     

    using namespace std;

     

    class Triangle {

    private:

     

        int x, y, z;

    public:

     

        Triangle() { }

     

        void area() {

            float s, area;

            s = (x + y + z) / 2;

            area = sqrt(s * (s - x) * (s - y) * (s - z));

            cout << "area is " << area << endl;

        }

     

        friend ostream &operator<<(ostream &os, const Triangle &b) {

            float s, area;

            s = (b.x + b.y + b.z) / 2;

            area = sqrt(s * (s - b.x) * (s - b.y) * (s - b.z));

    //        cout << "area is " << area << endl;

            os << "area is " << area <<endl;

            return os;

        }

     

        friend Triangle operator+(Triangle left, Triangle right) {

            Triangle b;

            b.x = left.x + right.x;

            b.y = left.y + right.y;

            b.z = left.z + right.z;

            return b;

        }

     

        void input() {

            cin >> x >> y >> z;

        }

     

        void output() {

            cout << "triangle three horizon length is " << x << "  " << y << "  " << z << endl;

        }

    };

     

    int main() {

     

        Triangle a, b, c;

        a.input();

        b.input();

        c.input();

        (a + b).output();

    //    (a + b).area();

        cout << a + b + c <<endl;

        return 0;

    }     

       

      </code>

    PS:
    我更新了下,这里其实没必要用友元函数,直接如下用就行

    Triangle operator+(Triangle other)
    {

    1

    2

    3

    4

    5

    <code>Triangle ret;

    ret.x = this->x + other.x;

    ret.y = this->y + other.y;

    ret.z = this->z + other.z;

    return ret;</code>

    }

    你用friend来处理的话,返回值也是一个Triangle,可以递归的再去加另外一个Triangle,就实现多个Triangle连加的形式

    balas
    0
  • Batalbalas