本文档详细介绍了《C++ Primer Plus》第11章中涉及的源代码,包括mytime和vector类的实现及使用示例,如mytime0到mytime3、usetime系列、vector头文件和实现文件,还有randwalk随机行走程序及stonewt石头重量类的改进。通过这些代码,读者可以深入理解C++中时间类和动态数组的实现及应用。
#ifndefMYTIME0_H_#defineMYTIME0_H_classTime{
private:int hours;int minutes;public:Time();Time(int h,int m =0);voidAddMin(int m);voidAddHr(int h);voidReset(int h =0,int m =0);
Time Sum(const Time& t)const;voidShow()const;};#endif
#include<iostream>#include"mytime0.h"intmain(){
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;return0;}
11.4 mytime1.h
#ifndefMYTIME1_H_#defineMYTIME1_H_classTime{
private:int hours;int minutes;public:Time();Time(int h,int m =0);voidAddMin(int m);voidAddHr(int h);voidReset(int h =0,int m =0);
Time operator+(const Time& t)const;voidShow()const;};#endif