java clock函数_实训C++语言设计——Clock模拟时钟系统

本文展示了如何使用C++设计一个名为CClock的类,通过操作符重载实现对时间的加减操作。通过类的构造函数、成员函数和友元函数,实现了CClock类型与整数之间的隐式转换和运算,包括++、+、-等操作。同时,文章提供了完整的代码示例,展示如何在实际程序中应用这些操作。
摘要由CSDN通过智能技术生成

平台:VC++ 2005 测试通过!

.vcproj

这是使用应用程序向导生成的 VC++ 项目的主项目文件。

它包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。

StdAfx.h, StdAfx.cpp

这些文件用于生成名为 twod.pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。

这些都是使用应用程序向导生成的 VC++ 文件故不列出

我只列出程序主要部分!

/*

本例中,通过CClock类型的实现演示了C++中

操作符重载,类型的显式,隐式转换.

1. 为CClock类型提供++,+,-三个操作符重载函数,

使得++,+,-三个操作符可用于CClock类型的变量

的运算.

2. 对于客户代码中的t3 = t1 * 5; t3 = 6 * t3;要实现

此功能有两种途径:

2.1 通过CClock类型的类型转换函数

CClock(unsigned long i);  将unsigned long转换为CClock

operator unsigned long();将CClock转换为unsigned long

编译器将隐式地调用上述转换函数来支持

t3 = t1 * 5; t3 = 6 * t3;运算,具体过程是:

编译器隐式地调用operator unsigned long(),将

t1 * 5,6 * t3中的t1,t3自动转换为unsigned long

然后执行*法运算,然后,再调用CClock(unsigned long i)

将结果转换为CClock类型赋值给t3.

2.2 通过实现CClock类型的*法重载函数,来支持CClock

类型与unsigned long类型的*法运算.实现*法重载的

方式又有两种:

2.2.1 实现CClock类型的*法重载"成员函数"

CClock operator *(unsigned long m);此函数

只支持诸如:t1 * 5方式的运算,而5*t1运算是

不支持的.

2.2.2 实现CClock类型的*法重载"友元函数"

friend CClock operator *(unsigned long m, CClock c);

friend CClock operator *(CClock c, unsigned long m);

可支持t1 * 5运算,及5*t1运算.

*/

#include

using namespace std;

class CClock {

//下面是三个全局friend函数

friend CClock operator+(int i, const CClock& c);

friend CClock operator+(const CClock& c, int i);

//friend CClock operator +(const CClock& c1, const CClock& c2);

*/

public:

/*constructor作为ADT conversions

此处的构造函数CClock可以

把unsigned long类型的变量

转换为CClock类型.在C++中凡是

只带一个参数的构造函数

都定义了一个隐式的转换*/

CClock(unsigned long i);

CClock(const CClock& rc);

void  print() const;      //formatted printout

void  tick();             //add one second

/*重载++,-两个操作符,使它们可

用于CClock的变量*/

CClock operator++(){ tick(); return *this; }

CClock operator++(int){ tick(); return *this; }

CClock operator +(const CClock& c);

// CClock operator +(int i);

/*CClock operator -(const CClock&rc);

//CClock operator *(unsigned long m);

/*定义一个由CClock类型转换为unsigned long类型

变量的函数*/

operator unsigned long(){ return this->_totsecs;};

private:

unsigned long  _totsecs; //时间总长

unsigned long  _secs, _mins, _hours, _days;//秒,分,小时,天

};

CClock::CClock(unsigned long i) {

/*将时间转换为秒,分,小时,天表示*/

_totsecs = i;

_secs = _totsecs % 60;

_mins = (_totsecs / 60) % 60;

_hours = (_totsecs  / 3600) % 24;

_days = _totsecs  / 86400;

}

CClock::CClock(const CClock& rc){

_totsecs = rc._totsecs;

_secs = rc._secs;  _mins = rc._mins;

_hours = rc._hours; _days = rc._days;

}

void CClock::tick(){

CClock  temp(++_totsecs);

_secs = temp._secs;  _mins = temp._mins;

_hours = temp._hours;  _days = temp._days;

}

CClock CClock::operator +(const CClock& rc){

/*注意此函数返回值要求是CClock,

而return语句的内容是unsigned long

类型的this->_totsecs + rc._totsecs,隐式

类型转换发生*/

return (this->_totsecs + rc._totsecs);

}

/*

CClock CClock::operator -(const CClock& rc){

return (this->_totsecs - rc._totsecs);

}*/

/*CClock CClock::operator *(unsigned long m){

_totsecs = _totsecs * m;

return(this->_totsecs);

}

*/

/*CClock operator+(CClock c1, CClock c2){

//注意此处c1._totsecs + c2._totsecs是unsigned long

//但函数的返回值类型是CClock,编译器自动调用了类CClock的

//构造函数进行了ADT Conversion处理

return (c1._totsecs + c2._totsecs);

}

CClock CClock::operator-(CClock c){

return (_totsecs - c._totsecs);

}

CClock operator*(unsigned long m, CClock c)

{

return (m * c._totsecs);

}

CClock operator*(CClock c, unsigned long m)

{

return (c * m);

}

*/

/*CClock operator+(const CClock& c1, const CClock& c2){

return (c1._totsecs + c2._totsecs);

}

*/

CClock operator+(int i, const CClock& c){

return (i + c._totsecs);

}

/*CClock CClock::operator +(int i){

return ( i + _totsecs);

}*/

CClock operator+(const CClock& c, int i){

return (i + c._totsecs);

}

void CClock::print() const

{

cout << _days << " d :" << _hours << " h :"

<< _mins << " m :" << _secs << " s" << endl;

}

// ClockPro.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "CClock.h"

int _tmain(int argc, _TCHAR* argv[])

{

CClock  t1(59), t2(172799); //172799 = 2 days - 1 sec

CClock t3(0);

CClock c1(900), c2(400);

cout << "initial times are" << endl;

t1.print();  t2.print();

int i;

cin >> i;

(t1 + t2).print();

(i + t1).print();

//(t1 + i).print();

cout << "after one second times are" << endl;

/*(++t1).print();  (++t2).print();

t3 = t1 + t2;     t3.print();

t3 = t3 - t1;      t3.print();

/*程序中并没有实现*操作符的重载

此处的*运算是如何实现的呢?*/

/*t3 = t1 * 5;       t3.print();

t3 = 6 * t3;      t3.print();

cout << "/n c1 and c2 /n";

c1.print();   c2.print();

*/

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值