C++ primer plus(第六版)编程练习答案 第11章 使用类

一、程序清单

mytime0.h

// mytime0.h -- Time class before operator overloading
#ifndef MYTIME0_H_
#define MYTIME0_H_

class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h, int m = 0);
	void AddMin(int m);
	void AddHr(int h);
	void Reset(int h = 0, int m = 0);
	const Time Sum(const Time & t) const;
	void Show() const;
};
#endif

mytime0.cpp 

// mytime0.cpp  -- implementing Time methods
#include 
#include "mytime0.h"

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

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

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

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

void Time::Reset(int h, int m)
{
	hours = h;
	minutes = m;
}

const Time Time::Sum(const Time & t) const
{
	Time sum;
	sum.minutes = minutes + t.minutes;

你可能感兴趣的:(C++,primer,plus(第六版)编程练习答案,c++,开发语言,算法)