c++简单时钟代码实现

#include"Clock.h"

#include<iostream>
using namespace std;
int main()
{
	int h, m, s;
	cin >> h >> m >> s;
	Clock c1(h, m, s), c2(c1);
	c1.SetTime(10, 20, 30);
	c1.getTime(h, m, s);
	cout << "c1:" << h << ":" << m << ":" << s << endl;
	c2.ShowTime();
	system("pause");
	return 0;
}
#include "Clock.h"
#include<iostream>
using namespace std;
Clock::Clock()
{
	h = m = s = 0;
}
Clock::~Clock()
{

}
Clock::Clock(int h, int m, int s)
{
	this->h = h;
	this->m = m;
	this->s = s;
}
Clock::Clock(Clock& c)
{
	h = c.h;
	m = c.m;
	s = c.s;
}
void Clock::SetTime(int h, int m, int s)
{
	this->h = h;
	this->m = m;
	this->s = s;
}

void Clock::getTime(int& h, int& m, int& s)
{
	h = this->h;
	m = this->m;
	s = this->s;
}


void Clock::ShowTime()
{
	cout << h << ":" << m << ":" << s << endl;
}
#pragma once


class Clock
{

public:
	Clock(int h, int m,int s);
	Clock();
	Clock(Clock &c);
	~Clock() ;
	int h, m, s;
	void SetTime(int h, int m, int s);
	void getTime(int& h, int& m, int& s);
	void ShowTime();
	
};

以上是出自慕课程序设计基础(C&C++)中第八章编程实战的内容

以上是简单时钟板块,这里要注意按原慕课中代码在Visual Stdio 上生成会出错,原因是重复声明了h,m,s要在private中删去int h,m,s.

2.行走的简单时钟

代码类似

#pragma once


class Clock
{

public:
	Clock(int h, int m,int s);
	Clock();
	Clock(Clock &c);
	~Clock() ;
	int h, m, s;
	void SetTime(int h, int m, int s);
	void getTime(int& h, int& m, int& s);
	void ShowTime();
	void addOneSecond();
};
#include"Clock.h"
#include<Windows.h>
#include<iostream>
using namespace std;
int main()
{
	Clock c;
	while (1)
	{
		system("cls");
		c.addOneSecond();
		c.ShowTime();
		Sleep(1000);
	}
	return 0;
}
#include "Clock.h"
#include<iostream>
using namespace std;
Clock::Clock()
{
	h = m = s = 0;
}
Clock::~Clock()
{

}
Clock::Clock(int h, int m, int s)
{
	this->h = h;
	this->m = m;
	this->s = s;
}
Clock::Clock(Clock& c)
{
	h = c.h;
	m = c.m;
	s = c.s;
}
void Clock::SetTime(int h, int m, int s)
{
	this->h = h;
	this->m = m;
	this->s = s;
}

void Clock::getTime(int& h, int& m, int& s)
{
	h = this->h;
	m = this->m;
	s = this->s;
}


void Clock::ShowTime()
{
	cout << h << ":" << m << ":" << s << endl;
}

void Clock::addOneSecond()
{
	s++;
	if (s >= 60)
	{
		m++;
		s = 0;
		if (m >= 60)
		{
			m = 0;
			h++;
			if (h >= 12)
			{
				h = 0;
			}


		}
	}
		
	   
}


	
		





  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值