2021. 3. 10. 19:42 c언어
C언어 - 연산자 오버로딩
#include <iostream>
using namespace std;
//실습) 현재 시각을 나타내는 Time 클래스를 정의한다.
//- 시는 0~23, 분은 0~59, 초는 0~59 입력
// + , -연산자를 중복 정의하라.
//- 멤버 함수로 정의
//- 초 로 변환해서 연산
//- / , % 사용해서 시 / 분 / 초 로 변환
//- +연산시 24시가 넘을 경우, -연산시 0시 이전이 될 경우
// <, >, == 연산자를 중복 정의하라.
//- 멤버 함수로 정의
//- 초 로 변환해서 연산
// >> , << 연산자를 중복 정의하라
//- friend 함수로 정의
// 초를 시간으로 바꾸기
class cTime
{
private:
int hours; // 시간
int minute; // 분
int second; // 초
public:
cTime(int hours = 0, int minute = 0, int second = 0) : hours(hours), minute(minute), second(second) {} // 생성자
int totsum() { // 자기 값 초단위로 넣기
return ((((this->hours * 60) + this->minute) * 60) + this->second);
}
int totsum(const cTime& temp) { // 비교 할 값 초단위로 넣기
return ((((temp.hours * 60) + temp.minute) * 60) + temp.second);
}
cTime operator+(int d) {
int tot = totsum() + d;
while (tot > 86400)
tot -= 86400;
hours = (tot / 3600) % 24;
minute = (tot / 60) % 60;
second = tot % 60;
return cTime(hours, minute, second); } // 덧셈 연산자
cTime operator-(int d) {
int tot = totsum() - d;
while (tot < 0)
tot += 86400;
hours = (tot / 3600) % 24;
minute = (tot / 60) % 60;
second = tot % 60;
return cTime(hours, minute, second); } // 뺄셈 연산자
cTime operator++(int) { //클래스 후처리 ++ 증감연산자
cTime a(*this);
int tot = totsum() + 1;
while (tot < 0)
tot += 86400;
hours = (tot / 3600) % 24;
minute = (tot / 60) % 60;
second = tot % 60;
return a;
}
cTime operator--(int) { // 클래스 후처리 -- 증감연산자
cTime a(*this);
int tot = totsum() - 1;
while (tot < 0)
tot += 86400;
hours = (tot / 3600) % 24;
minute = (tot / 60) % 60;
second = tot % 60;
return a;
}
bool operator<(const cTime& o) { //클래스 비교연산자(<)
if (totsum() < totsum(o))
return 1;
else
return 0;
}
bool operator>(const cTime& o) { //클래스 비교연산자(>)
if (totsum() > totsum(o))
return 1;
else
return 0;
}
bool operator==(const cTime& o) { //클래스 비교연산자(==)
return totsum() == totsum(o);
}
friend ostream& operator<<(ostream& cout, const cTime& temp);
friend istream& operator>>(istream& cin, cTime& temp);
};
ostream& operator<<(ostream& cout, const cTime& temp) { // 클래스 출력 함수 (시:분:초)
cout << temp.hours << ":" << temp.minute << ":" << temp.second <<endl;
return cout;
}
istream& operator>>(istream& cin, cTime& temp) { // 클래스 입력 함수 (시 분 초)
cin >> temp.hours >> temp.minute >> temp.second;
int total = temp.totsum();
while (total < 0)
total += 86400;
temp.hours = (total / 3600) % 24;
temp.minute = (total / 60) % 60;
temp.second = total % 60;
return cin;
}
int main() {
cTime start(1); // 1:0:0 입력
cTime end; // 0:0:0 입력
end = start+1000;
cout << "end = start + 1000 : " << end << endl;
end = end-500;
cout << "end = end - 500 : " << end << endl;
cout << "start < end : " << (start < end) << endl;
cout << "start > end : " << (start > end) << endl;
cout << "cin>>";
cin >> start;
cout << "cout<<"<< start <<endl;
cout << "start++ = " << start++ << endl;
cout << "start = " << start << endl;
cout << "start-- = " << start-- << endl;
cout << "start = " << start << endl;
}
'c언어' 카테고리의 다른 글
C언어 - 이진탐색트리 배열 확인 가능 (0) | 2021.03.09 |
---|---|
C언어 - Command line argument 를 사용 (0) | 2021.03.08 |
C언어 - 이진탐색트리 과제 (0) | 2021.03.06 |
C언어 - 링크리스트 (0) | 2021.03.05 |
C언어 - 두 단어의 크기를 비교하기(숫자일경우 숫자로 비교) 2 (0) | 2021.03.05 |