본문 바로가기

프로그램언어/C++

연산자2

연산자 함수의 여러 가지 유형

 

대입연산자

대입연산자는 인자로 받은 값을 이용하여 멤버변수의 값을 변경하고, 중첩된 후속 연산에 사용될 수 있도록 자기자신을 레퍼런스 타입으로 리턴하면 됩니다. 인자를 클래스 타입으로 받는 경우 const 레퍼런스 타입으로 받도록 했고, 기본 데이터 타입으로 받는 경우는 변수 타입으로 받도록 했습니다.

class Point {

public:

 

  Point &operator=(const Point &pt);

  Point &operator+=(const Point &pt);

  Point &operator-=(const Point &pt);

  Point &operator*=(int mag);

  Point &operator/=(int div);

};

#include <iostream>

using namespace std;

 

class Point {

public:

    Point(void);

    Point(int x, int y);

    void Show(void);

    Point& operator=(const Point& pt);

    Point& operator+=(const Point& pt);

    Point& operator-=(const Point& pt);

    Point& operator*=(int mag);

    Point& operator/=(int div);

private:

    int x, y;

};

 

Point::Point(void) {
    x = y = 0;
}

Point::Point(int x, int y) {

    this->x = x

    this->y = y

}

void Point::Show() {

    cout << "(" << x << ", " << y << ")" << endl;

}

Point& Point::operator=(const Point& pt) {

    x = pt.x;

    y = pt.y;

    return *this

}

Point& Point::operator+=(const Point& pt) {

    x += pt.x;

    y += pt.y;

    return *this

}

Point& Point::operator-=(const Point& pt) {

    x -= pt.x;

    y -= pt.y;

    return *this

}

Point& Point::operator*=(int mag) {

    x *= mag

    y *= mag

    return *this

}

Point& Point::operator/=(int div) {

    x /= div

    y /= div

    return *this

}

int main() {

    Point p1(10, 20), p2(2, 3);

 

    p2 = p1;

    p2.Show();
}
실행 결과
(10, 20)

#include <iostream>

using namespace std;

 

class Point {

public:

    Point(void);

    Point(int x, int y);

    void Show(void);

    Point& operator=(const Point& pt);

    Point& operator+=(const Point& pt);

    Point& operator-=(const Point& pt);

    Point& operator*=(int mag);

    Point& operator/=(int div);

private:

    int x, y;

};

 

Point::Point(void) { 
    x = y = 0;
}

Point::Point(int x, int y) {

    this->x = x

    this->y = y

}

void Point::Show() {

    cout << "(" << x << ", " << y << ")" << endl;

}

Point& Point::operator=(const Point& pt) {

    x = pt.x;

    y = pt.y;

    return *this

}

Point& Point::operator+=(const Point& pt) {

    x += pt.x;

    y += pt.y;

    return *this

}

Point& Point::operator-=(const Point& pt) {

    x -= pt.x;

    y -= pt.y;

    return *this

}

Point& Point::operator*=(int mag) {

    x *= mag

    y *= mag

    return *this

}

Point& Point::operator/=(int div) {

    x /= div

    y /= div

    return *this

}

int main() {

    Point p1(10, 20), p2(2, 3);

    p2 += p1;

    p2.Show();

 

    p2 -= p1;

    p2.Show();

 

    p2 *= 5;

    p2.Show();

 

    p1 /= 5;

    p1.Show();
}
실행 결과
(12, 23)
(2, 3)
(10, 15)
(2, 4)

 

산술연산자

 

산술연산자는 자기자신과 인자로 받은 값을 이용하여 연산을 하고, 그 결과를 지역변수에 저장해서 리턴하면 됩니다. 지역변수는 연산자 함수의 수행이 종료되는 순간 메모리에서 삭제되지만, 그 값은 연산자 함수로 호출한 쪽에 복사되어 전달됩니다.

연산자를 호출해도 멤버변수가 변경되는 일은 없어야 하므로 const 멤버함수로 선언하는 것이 좋습니다. 그리고  서로 다른 타입 간의 연산을 하는 곱셈과 나눗셈은 교환법칙이 성립하도록 하기 위해 멤버함수로도 구현하고, friend로 선언된 전역함수로도 구현했습니다.

class Point {

public:

 

  Point operator+(const Point &pt) const;

  Point operator-(const Point &pt) const;

  Point operator*(int mag) const;

  Point operator/(int div) const;

  friend Point operator*(int mag, const Point &pt);

  friend Point operator/(int div, const Point &pt);

};

#include <iostream>

using namespace std;

class Point {

public:

  Point(void);

  Point(int x, int y);

  void Show(void);

  Point operator+(const Point &pt) const;

  Point operator-(const Point &pt) const;

  Point operator*(int mag) const;

  Point operator/(int div) const;

  friend Point operator*(int mag, const Point &pt);

  friend Point operator/(int div, const Point &pt);

private:

  int x, y;

};

Point::Point(void) { x=y=0; }

Point::Point(int x, int y) {

  this->x = x;

  this->y = y;

}

void Point::Show() {

  cout << "(" << x << ", " << y <<")" << endl;

}

Point Point::operator+(const Point &pt) const {

  return Point(x + pt.x, y + pt.y);

}

Point Point::operator-(const Point &pt) const {

  return Point(x - pt.x, y - pt.y);

}

Point Point::operator*(int mag) const {

  return Point(x * mag, y * mag);

}

Point Point::operator/(int div) const {

  return Point(x / div, y / div);

}

Point operator*(int mag, const Point &pt) {

  return Point(pt.x * mag, pt.y * mag);

}

Point operator/(int div, const Point &pt) {

  return Point(pt.x / div, pt.y / div);

}

int main() {

  Point p1(10, 20), p2(2,3), p3;

  p3 = p2 + p1;

  p3.Show();

  p3 = p2 - p1;

  p3.Show();

  p3 = p2 * 2;

  p3.Show();

  p3 = p1 / 5;

  p3.Show();

  p3 = 2 * p1;

  p3.Show();

  p3 = 2 / p1;

  p3.Show();

  return 0;

}

 

#include <iostream>

using namespace std;

class add

{

  int money;

public:

  void set(int MONEY) {

  money=MONEY;

  }

  int operator + (add &A) {

  return (this->money + A.money);

  }

  int operator - (add &A) {

  return (this->money - A.money);

  }

  int operator * (add &A) {

  return (this->money * A.money);

  }

  int operator / (add &A) {

  return (this->money / A.money);

  }

};

int main()

{

  add a, b;

  a.set(100);

  b.set(20);

  cout << "100 + 20 = " << (a+b) << endl;

  cout << "100 - 20 = " << (a-b) << endl;

  cout << "100 * 20 = " << (a*b) << endl;

  cout << "100 / 20 = " << (a/b) << endl;

  return 0;

}

 

 

관계연산자

 

관계연산자는 자기자신과 인자로 받은 값을 비교하여 true 또는 falsebool형으로 리턴하면 됩니다. 관계연산자 역시 연산자를 호출해도 멤버변수가 변경되는 일은 없어야 하므로 const멤버함수로 선언하는 것이 좋습니다. 

#include <iostream>

using namespace std;

class Point {

public:

  Point(void);

  Point(int x, int y);

  void Show(void);

  bool operator==(const Point &pt) const;

  bool operator!=(const Point &pt) const;

private:

  int x, y;

};

Point::Point(void) {

  x = y = 0;

}

Point::Point(int x, int y) {

  this->x = x;

  this->y = y;

}

void Point::Show(void) {

  cout << "(" << x << ", " << y << ")" << endl;

}

bool Point::operator==(const Point &pt) const

{

  return (x == pt.x && y == pt.y);  // x, y가 모두 같으면 true

}

bool Point::operator!=(const Point &pt) const

{

  return (x != pt.x || y != pt.y);

  // x, y 중 하나라도 다르면 true

}

int main(void)

{

  Point p1(10, 20), p2(20, 30);

  if(p1 == p2)

  cout << "p1 == p2" << endl;

  else

  cout << "p1 != p2" << endl;

  return 0;

}

 

 

#include <iostream>

using namespace std;

class Point {

public:

  Point(void);

  Point(int x, int y);

  void Show(void);

  bool operator==(const Point &pt) const;

  bool operator!=(const Point &pt) const;

private:

  int x, y;

};

Point::Point(void) { x=y=0; }

Point::Point(int x, int y) {

  this->x = x;

  this->y = y;

}

void Point::Show() {

  cout << "(" << x << ", " << y <<")" << endl;

}

bool Point::operator==(const Point &pt) const

{

  return (x == pt.x && y == pt.y);

  //x, y가모두같으면true

}

bool Point::operator!=(const Point &pt) const

{

  return (x != pt.x || y != pt.y);

  //x, y 중하나라도다르면true

}

int main() {

Point p1(10, 20), p2(5,15), p3(10, 20);

if(p1==p2){

  p1.Show(); p2.Show();

  cout << "두개의값이모두같습니다." << endl;

  } else {

  p1.Show(); p2.Show();

  cout << "두개의값이서로다릅니다." << endl;

  }

if(p1==p3) {

  p1.Show(); p3.Show();

  cout << "두개의값이모두같습니다." << endl;

} else {

  p1.Show(); p3.Show();

  cout << "두개의값이서로다릅니다." << endl;

}

if(p1!=p2) {

  p1.Show(); p2.Show();

  cout << "두개의값중에하나라도같은값이없습니다."<< endl;

} else {

  p1.Show(); p2.Show();

  cout << "두개의값중에하나라도같은값이존재합니다.”<< endl;

}

if(p1!=p3) {

  p1.Show(); p3.Show();

  cout << "두개의값중에하나라도같은값이없습니다."<< endl;

} else {

  p1.Show(); p3.Show();

  cout << "두개의값중에하나라도같은값이존재합니다."<< endl;

}

return 0;

}

#include <iostream>

using namespace std;

class student

{

protected:

    int kor, math, eng;

public:

    void setstudent(int k, int m, int e){

        kor=k; math=m; eng=e;

    }

    void outstudent();

    int operator > (student S);

    int operator == (student S);

    int operator != (student S) {

       return !(*this==S);

    }

    int operator < (student S) {

        return !(*this==S || *this>S);

    }

    int operator >= (student S) {

        return !(*this==S || *this>S);

    }

    int operator <= (student S) {

        return !(*this>S);

    }

};

int student::operator > (student S) {

    if(kor>S.kor)
        return 1;

    if(kor==S.kor)
        if(math >
S.math)
            return 1;

    if(math==S.math)
        if(eng >S.eng)
            return 1;

        else
            return 0;

}

int student::operator == (student S) {

    if(kor==S.kor && math==S.math && eng==S.eng)

        return 1;

    else
        return 0;

}

void student::outstudent(void)

{

    cout<<"국어" << kor << " 수학" << math ;

    cout << " 영어" << eng << endl;

}

int main()

{

    student a, b, c;

    a.setstudent(75,75,75);

    b.setstudent(80,80,80);

    c.setstudent(85,85,85);

 

    cout<< "A학생: ";

    a.outstudent();

    cout<< "B학생: ";

    b.outstudent();

    cout<< "C학생: ";

    c.outstudent();

    if(a>b)

         cout << "A B보다 크다" << endl;

    else

        cout << "A B보다 작거나 같다." << endl;

     if(a>=c)

         cout << "A C보다 크거나 같다." << endl;

    else

        cout << "A C보다 작다" << endl;

}

 

 

'프로그램언어 > C++' 카테고리의 다른 글

템플릿  (0) 2020.08.20
클래스 예제2  (0) 2020.08.10
연산자1  (0) 2020.08.09
클래스5 문제  (0) 2020.08.08
클래스5  (0) 2020.08.08