C++的类、操作符重载与派生类

1、 类的基本概念

定义一个时间类,它能表示时、分、秒,并提供以下操作。编写程序测试类的功能。

  • (1)Time (int h, int m, int s);//构造函数
  • (2) Time (Time const &x) //拷贝构造函数
  • (3) void set(int h, int m, int s);//设置时间
  • (4)Time & increment();//自动加1秒
  • (5)void display( );//显示时间
  • (6)bool equal(Time const & x);//判断两个时间是否相等
  • (7)bool less_than(Time const &x);//比较两个时间大小

2、 操作符重载函数

为Time类继续添加以下成员函数,并测试

  • (1) 赋值操作符重载函数    Time & operator = (Time const &x)
  • (2) 类型转换函数//将时间转换为秒数        operator int( )
  • (3) 可用作类型转换的构造函数   Time ( int ) //将int类型转换为Time格式,如Time(60)为0:1:0
  • (4) 时间的比较 bool operator = = (Time const & x); bool operator < (Time const & x)
  • (5) 增加/减少若干秒  Time operator + (int n) ; Time operator – (int n)
  • (6) 增加/减少1秒     Time & operator ++( ) ; Time& operator – -( )
  • (7) 计算两个时刻相差的秒数。int operator- ( Time const &x);

3、 派生类

利用时间类Time,定义一个带时区的时间类ExtTime。除了构造函数和时间调整函数外,ExtTime的其他功能与Time类似。用虚函数实现函数void display( );调用的动态绑定。

实现:

头文件time.h

#ifndef TIME_H
#define TIME_H
#include <string>

class Time
{
	int hour;
	int minute;
	int second;
	// 判断时间是否正确
	bool test_Time(int h, int m, int s);

public:
	// 构造函数
	Time();
	Time(int h, int m, int s);
	Time(Time const &x);
	Time(int s);

	// 操作函数
	void set(int h, int m, int s);
	Time & increment();
	virtual void display();
	bool equal(Time const &x);
	bool less_than(Time const &x);

	// 操作符重载
	Time & operator = (Time const &x);
	operator int();
	bool operator == (Time const &x);
	bool operator < (Time const &x);
	Time operator + (int n);
	Time operator - (int n);
	Time & operator ++ ();
	Time & operator -- ();
	int operator - (Time const &x);
};

// 派生类
class extTime :public Time
{
	std::string address;
public:
	// 构造函数
	extTime();
	extTime(const std::string &a, int h, int m, int s);
	extTime(const std::string &a, Time const &x);
	extTime(extTime const &x);

	//操作函数
	void set(const std::string &a, int h, int m, int s);
	void display();
};

#endif

类与派生类的实现time.cpp

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

bool Time::test_Time(int h, int m, int s)
{
	if (h <= 24 && h >= 0 && m <= 60 && m >= 0 && s <= 60 && s >= 0)
		return true;
	else
		return false;
}

Time::Time()
{
	hour = minute = second = 0;
}

Time::Time(int h, int m, int s)
{
	hour = h;
	minute = m;
	second = s;
}

Time::Time(Time const &x)
{
	hour = x.hour;
	minute = x.minute;
	second = x.second;
}

Time::Time(int s)
{
	second = s % 60;
	minute = (s / 60) % 60;
	hour = s / 3600;
}

void Time::set(int h, int m, int s) 
{
	if (test_Time(h, m, s)) {
		hour = h;
		minute = m;
		second = s;
	}
	else {
		std::cout << "The time you set is incorrect! Please set again." << std::endl;
	}
}

Time & Time::increment() 
{
	if (second == 60 && minute < 60) {
		minute++;
		second = 0;
	}
	else if (second == 60 && minute == 60) {
		hour++;
		minute = second = 0;
	}
	else {
		second++;
	}

	return *this;
}

void Time::display() 
{
	std::cout << hour << ":" << minute << ":" << second << std::endl;
}

bool Time::equal(Time const &x) 
{
	return ((hour == x.hour)&(minute == x.minute)&(second == x.second));
}

bool Time::less_than(Time const &x)
{
	if ((hour * 3600 + minute * 60 + second) < (x.hour * 3600 + x.minute * 60 + x.second))
		return true;
	else
		return false;
}

Time & Time::operator = (Time const &x)
{
	//防止自身赋值
	if (&x == this)
		return *this;

	hour = x.hour;
	minute = x.minute;
	second = x.second;

	return *this;
}

Time::operator int()
{
	return (hour * 3600 + minute * 60 + second);
}

bool Time::operator == (Time const &x)
{
	return ((hour == x.hour)&(minute == x.minute)&(second == x.second));
}

bool Time::operator < (Time const &x)
{
	if ((hour * 3600 + minute * 60 + second) < (x.hour * 3600 + x.minute * 60 + x.second))
		return true;
	else
		return false;
}

Time Time::operator + (int n)
{
	int temp1 = (int)*this + n;
	Time temp2(temp1);
	return temp2;
}

Time Time::operator - (int n)
{
	int temp1 = (int)*this - n;
	Time temp2(temp1);
	return temp2;
}

Time & Time::operator ++ ()
{
	int temp1 = (int)*this;
	temp1 ++;
	Time temp2(temp1);
	*this = temp2;
	return *this;
}

Time & Time::operator -- ()
{
	int temp1 = (int)*this;
	temp1 --;
	Time temp2(temp1);
	*this = temp2;
	return *this;
}

int Time::operator - (Time const &x)
{
	Time temp;
	temp = x;
	return (int)*this - (int)temp;
}

extTime::extTime() :Time()
{
	address = "BeiJing";
}

extTime::extTime(const std::string &a, int h, int m, int s) : Time(h, m, s)
{
	address = a;
}

extTime::extTime(const std::string &a, Time const &x):Time(x)
{
	address = a;
}

extTime::extTime(extTime const &x) : Time(x)
{
	address = x.address;
}

void extTime::set(const std::string &a, int h, int m, int s)
{
	Time::set(h, m, s);
	address = a;
}

void extTime::display()
{
	std::cout << address << "-";
	Time::display();
}

测试文件testClass.cpp

// 测试类Time的正确性

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

int main()
{
	using std::cout;
	using std::endl;
	
	Time T1(11, 11, 11);
	cout << "时间T1:";
	T1.display();

	Time T2(T1);
	cout << "时间T2:";
	T2.display();

	T1.set(11, 11, 60);
	cout << "设置时间T1:";
	T1.display();

	T1 = T1.increment();
	cout << "时间T1自动加1秒:";
	T1.display();

	cout << "T1与T2是否相等? ";
	cout << T1.equal(T2) << endl;

	cout << "T1是否小于T2? ";
	cout << T1.less_than(T2) << endl;

	// 测试赋值操作符重载函数
	T1 = T2;

	// 测试类型转换函数
	int i = 1;
	int z = i + T1;
	cout << "z = " << z << endl;

	// 测试可用作类型转换的构造函数
	Time T3(66);
	cout << "时间T3:";
	T3.display();

	// 测试比较运算符重载函数
	cout << "T3 == T1? " << (T3 == T1) << endl;
	cout << "T3 < T1? " << (T3 < T1) << endl;

	// 测试增加减少若干秒
	T3 = T3 + 88;
	cout << "T3增加88秒为:";
	T3.display();
	T3 = T3 - 88;
	cout << "T3减少88秒为:";
	T3.display();

	// 测试增加较少一秒
	++T3;
	cout << "T3增加1秒为:";
	T3.display();
	--T3;
	cout << "T3减少一秒为:";
	T3.display();

	// 测试计算两个时刻相差秒数
	cout << "T1与T3相差秒数:";
	int difference = (T1 - T3);
	cout << difference << endl;

	// 测试派生类的构造函数
	extTime T4;
	T4.display();

	extTime T5("NewYork", 6, 7, 8);
	T5.display();

	extTime T6("BeiJing", T2);
	T6.display();

	extTime T7(T6);
	T7.display();
	T7.set("Tokyo", 14, 15, 16);
	T7.display();

	// 测试虚函数行为
	Time *testVirtual;
	testVirtual = &T2;
	testVirtual->display();
	testVirtual = &T5;
	testVirtual->display();

	getchar();
	return 0;
}