상세 컨텐츠

본문 제목

Overload ( operator overloading) 예제

개발생활/C C++

by 한국인맛집 2017. 5. 24. 09:35

본문

반응형

연산자 오버로딩 예제 


사칙연산(+-*/) ,(+=,-=,*=,/=) , 비교연산 (<>) , 전위 후위 상승 연산자 (++ --) 구현 완료


좌표값과 좌표값 사칙연산가능

상수값과 좌표값 사칙연산가능

비교연산 좌표값과 좌표값간의 비교연산 가능




#include<cstdio>

#include<cstring>

#include<ctime>

#include<cmath>




class Point3 {


public:

int x;

int y;

int z;


Point3(int x, int y,int z) {

this->x = x;

this->y = y;

this->z = z;

}



//print point

void print_point() {

printf("(%d , %d, %d) \n", this->x,this->y,this-> z);

}

//vector ABS  벡터의 절대값

float volume() {

return (float)sqrt(x*x + y*y+z*z);

}


bool operator<(Point3& p) {

return volume() < p.volume();

}

bool operator>(Point3& p) {

return volume() > p.volume();

}



// point + number

Point3 operator+(int n) {

return Point3(x+n,y+n,z+n);

}

Point3 operator*(int n) {

return Point3(x * n, y * n, z * n);

}

Point3 operator/(int n) {

return Point3(x / n, y / n, z / n);

}

Point3 operator-(int n) {

return Point3(x - n, y - n, z - n);

}



// point + point

Point3 operator+(Point3& p) {

return Point3(x + p.x, y + p.y, z + p.z);

}

Point3 operator-(Point3& p) {

return Point3(x - p.x, y - p.y, z - p.z);

}

Point3 operator*(Point3& p) {

return Point3(x * p.x, y * p.y, z * p.z);

}

Point3 operator/(Point3& p) {

return Point3(x / p.x, y / p.y, z / p.z);

}



Point3 operator++() {

x = x + 1;

y = y + 1;

z = z + 1;

return (*this);

}

//Prefix operator

Point3 operator++(int) {

Point3 p = Point3(x, y,z);

x = x + 1;

y = y + 1;

z = z + 1;

return p;

}

//Postfix operator

Point3 operator--() {

Point3 p = Point3(x, y,z);

x = x - 1;

y = y - 1;

z = z - 1;

return (*this);

}

// Prefix operator

Point3 operator--(int) {

Point3 p = Point3(x, y, z);

x = x - 1;

y = y - 1;

z = z - 1;

return p;

}

//Postfix operator


Point3 operator+=(int a) {

Point3 p = Point3(x, y, z);

x = x + a;

y = y + a;

z = z + a;

return p;

}

Point3 operator-=(int a) {

Point3 p = Point3(x, y,z);

x = x - a;

y = y - a;

z = z - a;

return p;

}

Point3 operator*=(int a) {

Point3 p = Point3(x, y,z);

x = x * a;

y = y * a;

z = z*a;

return p;

}

Point3 operator/=(int a) {

Point3 p = Point3(x, y,z);

x = x / a;

y = y / a;

z = z / a;

return p;

}


};


//global operator overloading

Point3 operator+(int n,Point3& p) {

return Point3(p.x + n, p.y + n,p.z+n);

}

Point3 operator-(int n, Point3& p) {

return Point3(p.x - n, p.y - n,p.z-n);


}

Point3 operator*(int n, Point3& p) {

return Point3(p.x * n, p.y * n,p.z*n);


}

Point3 operator/(int n, Point3& p) {

return Point3(p.x / n, p.y / n,p.z/n);


}



#include"Header.h"


int main() {

Point3 p1 = Point3(10, 20,10);

Point3 p2 = Point3(30, 40,30);


p1 = p1 * 2;

p1.print_point();

p1 = p1 + 2;

p1.print_point();

p1 = p1 - 2;

p1.print_point();


p1 = p1 / 2;

p1.print_point();


Point3 p3 = p1 + p2;


p3.print_point();

p3 = p1-p2;

p3.print_point();

p3 = p1*p2;

p3.print_point();

p3 = p1/p2;

p3.print_point();


fgetc(stdin);

return 0;

}



Header.h




반응형

'개발생활 > C C++' 카테고리의 다른 글

C/C++ multi Thread  (0) 2017.10.17
static used in class C++  (31) 2017.09.05
Cmd command  (0) 2017.08.22
Template / Single LinkedList 구현 과정.  (124) 2017.06.07
C++ 게시판만들기.  (124) 2017.06.06
Class Single Linked List  (0) 2017.06.06

관련글 더보기