W14 chap13
Transcript of W14 chap13
13. 연산자 오버로딩
차례
• 객체 연산• 연산자 오버로딩
2/14
객체 연산
• 허수 클래스를 정의하고, 객체를 생성하여 허수 덧셈을실행하기
클래스 이름: ImaginaryNumber멤버 변수:
실수부와 허수부의 실수 (허수부의 실수는 0이 아닌 실수)멤버 함수:
생성자 1: 실수부가 0, 허수부가 1로 초기화생성자 2: 실수부와 허수부를 매개변수로 전달하여 값을 지정실수부 값과 허수부 값을 각각 전달받는 함수실수부 값과 허수부 값을 객체 외부로 전달하는 각각의 함수허수 형태로 문자열을 작성하는 함수
3/14
소스 13-1 (ch13_ImaginaryNumber.h)#ifndef _IMAGINARY_#define _IMAGINARY_
#include <iostream>#include <string>using namespace std;
class ImaginaryNumber{public:
ImaginaryNumber();ImaginaryNumber(const double a, const double b);void SetA(const double a);void SetB(const double b);double GetA();double GetB();string GetImaginaryNumber();
private:double a; //실수부double b; //허수부 (b≠0)
};
#else#endif 4/14
허수의 덧셈
멤버 함수 추가
ImaginaryNumber AddImaginary(const ImaginaryNumber ima);자기자신과 매개변수를 더해서 결과값을 리턴함
멤버 함수 정의ImaginaryNumber ImaginaryNumber::AddImaginary(const ImaginaryNumberima){
ImaginaryNumber res;
res.a=this->a+ima.a;res.b=this->b+ima.b;
return res;}
객체 덧셈을 위한 함수 정의
5/14
허수 덧셈 테스트소스 13-5 (ch13_02.cpp)
#include "ch13_ImaginaryNumber.h"
int main(){
ImaginaryNumber ima1(4.2,5.1);ImaginaryNumber ima2;ImaginaryNumber ima3;
ima2.SetA(7.2);ima2.SetB(9.6);
ima3=ima1.AddImaginary(ima2); //ima1과 ima2의 덧셈 결과가 리턴, ima3에 할당
cout << "( " << ima1.GetImaginaryNumber() << " ) + ";cout << "( " << ima2.GetImaginaryNumber() << " ) = ";
cout << ima3.GetImaginaryNumber() << endl;
return 0;} 6/14
연산자 오버로딩 1
• 연산자 오버로딩• 연산 대상에 대한 연산자 재정의
3+4정수의 덧셈 연산 가능함!!!
class TEST{
….};
TEST a, b;a+b연산 불가능, TEST의 객체를 대상으로 하는 덧셈 연산 오버로딩해야함
string str1(“computer”), str2(“science”);str1+str2 연산 가능, string 객체를 대상으로 하는 덧셈 연산이 오버로딩되어 있음
7/14
연산자 오버로딩 2
• 연산자 오버로딩 함수 프로토타입함수반환형 operator 연산자 (연산대상);
ImaginaryNumber 클래스에 덧셈 연산 함수를 정의하면~
함수 선언 :ImaginaryNumber operator+(const ImaginaryNumber object);
함수 정의 :ImaginaryNumber ImaginaryNumber::operator+(const ImaginaryNumber object) //연산자 오버로딩에 의해 추가된 연산자 함수{
ImaginaryNumber res;
res.a=this->a+object.a;res.b=this->b+object.b;
return res;} 8/14
소스 13-6 (ch13_03.cpp)허수의 덧셈 객체 연산자 오버로딩 테스트~
#include "ch13_ImaginaryNumber.h"
int main(){
ImaginaryNumber ima1(2.7,6.3);ImaginaryNumber ima2;ImaginaryNumber ima3;
ima2.SetA(7.2);ima2.SetB(9.6);
ima3=ima1+ima2; //연산자 오버로딩으로 인해 덧셈 연산자 사용 가능!!
cout << "( " << ima1.GetImaginaryNumber() << " ) + ";cout << "( " << ima2.GetImaginaryNumber() << " ) = ";
cout << ima3.GetImaginaryNumber() << endl;
return 0;}
9/14
증감 연산자 오버로딩
• 증감 연산자• 선 증감 : ++a, --a• 후 증감 : a++, a--
클래스형 operator++( ); //선 증감 연산자 오버로딩클래스형 operator++(int dummy); //후 증감 연산자 오버로딩
ImaginaryNumber 클래스의 증가 연산자 오버로딩
ImaginaryNumber operator++(void);ImaginaryNumber operator++(int dummy);
10/14
소스 13-9 (ch13_ImanginaryNumber.cpp)
ImaginaryNumber ImaginaryNumber::operator++(void) //연산자 오버로딩에의해 추가된 연산자 함수{
this->a++;return *this;
}
ImaginaryNumber ImaginaryNumber::operator++(int dummy){
this->b++;return *this;
}
11/14
오버로딩 가능한 연산자
12/25
+ ^ = *= |= == || ->
- & < /= << != ++ [ ]
* | > %= >> <= -- ( )
/ ~ += ^= >>= >= ->* new
% ! -= &= <<= && , delete
실습 – 시간 클래스 정의하기
클래스 이름: Time멤버 변수: 시, 분, 초를 나타내는 부호 없는 정수와 초 단위로 시간을 나타냄멤버 함수 :
생성자1: 매개변수 없이 멤버 변수를 0으로 초기화생성자2: 시, 분, 초의 멤버 변숫값을 매개변수로 전달하여 초기화초 단위로 시간을 나타내는 멤버 변숫값을 계산하는 멤버 함수 CalSecond( )각 멤버 변숫값을 설정하는 멤버 함수각 멤버 변숫값을 외부로 전달할 수 있는 멤버 함수'00시 00분 00초' 형태로 외부에 전달하는 멤버 함수
13/14
소스 13-10 (ch13_Time.h)
#ifndef _TIME_#define _TIME_
#include <iostream>#include <string>#define HOUR_SEC 3600#define MIN_SEC 60using namespace std;
class Time{public :
Time();Time(const int hour, const int min, const int sec);void SetHour(const int hour);void SetMin(const int min);void SetSec(const int sec);int GetHour();int GetMin();int GetSec();int CalSec();string ShowTime();
private :int hour, min, sec;int t_sec;
};
#else#endif
14/14
TIME 클래스에 <=, >= 오버로딩하기
멤버 함수 선언
bool operator<=(Time timeObj);bool operator>=(Time timeObj);
멤버 함수 정의bool Time::operator<=(Time timeObj){
this->CalSec();timeObj.CalSec();
if (this->t_sec<=timeObj.t_sec)return true;
elsereturn false;
}
bool Time::operator>=(Time timeObj){
this->CalSec();timeObj.CalSec();
if (this->t_sec>=timeObj.t_sec)
return true;else
return false;}
15/14
16/14
소스 13-16 (ch13_06.cpp) Time 클래스 테스트
#include "ch13_Time.h"
int main(){
Time t1(7,30,20);
cout << t1.ShowTime() << endl;cout << "시간 - 초단위 : " << t1.CalSec() << endl;
Time t2(4,50,23);
if (t1>=t2)cout << t1.ShowTime() << "이 " << t2.ShowTime() << "보다 크거나 같다!!" << endl;
elsecout << t2.ShowTime() << "이 " << t1.ShowTime() << "보다 크거나 같다!!" << endl;
if (t1<=t2)cout << t2.ShowTime() << "이 " << t1.ShowTime() << "보다 크거나 같다!!" << endl;
elsecout << t1.ShowTime() << "이 " << t2.ShowTime() << "보다 크거나 같다!!" << endl;
return 0;}
연산자 오버로딩과 FRIEND
#include <iostream>
using namespace std;
class NUMBOX{private:
int num1, num2;public:
NUMBOX(int num1, int num2) : num1(num1), num2(num2) { }
void ShowNumber() {
cout << "num1: " << num1 << ", num2: " << num2 << endl;
}NUMBOX operator+(int num){
return NUMBOX(num1+num, num2+num);}friend NUMBOX operator+(int num, NUMBOX&
ref);};
17/25
NUMBOX operator+(int num, NUMBOX& ref){
return ref + num;}
int main(){
NUMBOX nb1(10, 20);NUMBOX result = 10 + nb1 + 40;
nb1.ShowNumber();result.ShowNumber();
}