'연산자 오버로딩'에 해당되는 글 2건

  1. 2014.05.18 연산자 오버로딩

 

//연산자 오버로딩

#include <iostream>

using namespace std;

 

struct Complex {

        int real, image;

        Complex(int r, int i) : real(r), image(i) {}

        void print() { printf("%d + %di\n", real, image); }

 

        Complex operator+(const Complex& other) {

               return Complex(real + other.real, image + other.image);

        }

};

 

Complex add(Complex& c1, Complex& c2) {

        return Complex(c1.real + c2.real, c1.image + c2.image);

}

 

int main() {

        Complex c1(1, 1);             // 1+1i;

        Complex c2(2, 2);             // 2+2i;

 

        //Complex c3=add(c1, c2);

        Complex c3=c1 + c2;           //c1.operator+(c2);

        c3.print();

       

        return 0;

}

'Developer > C / C++' 카테고리의 다른 글

가변인자를 이용한 함수  (0) 2013.08.07
struct size  (0) 2013.08.05
전처리기  (0) 2013.08.02
#pragma pack  (0) 2013.08.02
const 와 포인터  (0) 2013.08.01
Posted by No names
,