강의 대시보드 상속 기본
상속 기본
강의명 : [하루 10분|C++] 누구나 쉽게 배우는 C++ 프로그래밍 입문
수강 기한 : 무제한
진도율 : 0강/42강 (0%) | 시간 : 0분/331분
나가기
미수강
이전수업
다음수업

time.h

#include <iostream>
#ifndef TIMEH
#define TIMEH

class Time
{
private:
	int hours;
	int mins;

public:
	Time();
	Time(int, int);
	void addHours(int);
	void addMins(int);
	Time operator+(Time&);
	Time operator*(int);
	void show();
	~Time();
	friend Time operator*(int n, Time& t) {
		return t * n;
	}
	friend std::ostream& operator<<(std::ostream&, Time&);
};

class NewTime : public Time {
private:
	int day;
public:
	NewTime();
	NewTime(int, int, int);
	void print();
	
};
#endif // !TIMEH

func.cpp

#include "time.h"

Time::Time()
{
	hours = mins = 0;
}

Time::Time(int h, int m) {
	hours = h;
	mins = m;
}

void Time::addHours(int h) {
	hours += h;
};

void Time::addMins(int m) {
	mins += m;
	hours += mins / 60;
	mins %= 60;
};

Time Time::operator+(Time& t) {
	Time sum;
	sum.mins = mins + t.mins;
	sum.hours = hours + t.hours;
	sum.hours += sum.mins / 60;
	sum.mins %= 60;
	return sum;
};

void Time::show() {
	std::cout << "시간 : " << hours << std::endl;
	std::cout << "분 : " << mins << std::endl;
}

Time::~Time()
{
}

Time Time::operator*(int n) {
	Time result;
	long resultMin = hours * n * 60 + mins * n;
	result.hours = resultMin / 60;
	result.mins = resultMin % 60;
	return result;
}

std::ostream& operator<<(std::ostream& os, Time& t) {
	os << t.hours << "시간 " << t.mins << "분";
	return os;
}

NewTime::NewTime() : Time(){
	day = 0;
}

NewTime::NewTime(int h, int m, int d) : Time(h,m){
	day = d;
}

void NewTime::print() {
	std::cout << "일 : " << day << std::endl;
	show();
}

main.cpp

#include<iostream>
#include"time.h"
using namespace std;

int main() {
	NewTime temp1();
	NewTime temp2(3, 30, 2);
	temp2.print();

	return 0;
}

    목차 커뮤니티