C++primer Plus课本代码(第11章)

本文档详细介绍了《C++ Primer Plus》第11章中涉及的源代码,包括mytime和vector类的实现及使用示例,如mytime0到mytime3、usetime系列、vector头文件和实现文件,还有randwalk随机行走程序及stonewt石头重量类的改进。通过这些代码,读者可以深入理解C++中时间类和动态数组的实现及应用。
摘要由CSDN通过智能技术生成

11.1 mytime0.h

#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);
	Time Sum(const Time& t)const;
	void Show() const;
};

#endif

11.2 mytime0.cpp

#include<iostream>
#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;
}

Time Time::Sum(const Time& t)const
{
   
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

void Time::Show() const
{
   
	std::cout << hours << " hours, " << minutes << " minutes";
}

11.3 usetime0.cpp

#include<iostream>
#include"mytime0.h"

int main()
{
   
	using std::cout;
	using std::endl;
	Time planning;
	Time coding(2, 40);
	Time fixing(5, 55);
	Time total;

	cout << "planning time = ";
	planning.Show();
	cout << endl;

	cout << "coding time = ";
	coding.Show();
	cout << endl;

	cout << "fixing time = ";
	fixing.Show();
	cout << endl;

	total = coding.Sum(fixing);
	cout << "coding.Sum(fixing) = ";
	total.Show();
	cout << endl;

	return 0;
}

11.4 mytime1.h

#ifndef MYTIME1_H_
#define MYTIME1_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);
	Time operator+(const Time& t)const;
	void Show() const;
};

#endif

11.5 mytime1.cpp

#include<iostream>
#include"mytime1.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;
}

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

void Time::Show() const
{
   
	std::cout << hours << " hours, " << minutes << " minutes";
}

11.6 usetime1.cpp

#include<iostream>
#include"mytime1.h"

int main()
{
   
	using std::cout;
	using std::endl;
	Time planning;
	Time coding(2, 40);
	Time fixing(5, 55);
	Time total;

	cout << "planning time = ";
	planning.Show();
	cout << endl;

	cout << "coding time = ";
	coding.Show();
	cout << endl;

	cout << "fixing time = ";
	fixing.Show();
	cout << endl;

	total = coding + fixing;
	cout << "coding + fixing = ";
	total.Show(<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满城枪声皆无你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值