Learn C++ step by step(2)
// using Dev C++ under WinXP
#include <iostream>
#include <cstdlib>
using namespace std; //standard c++ style about namespace
//define ADT Time
class Time
{
public:
Time(); //default constructor
void setTime(int,int,int);//set time for hours,minutes,seconds
void printMilitary(void);//print time using army´s style
void printStandard(void);//print time using standart style
private:
int hour; //0-23
int minute;//0-59
int second;//0-59
};
//constructor Time() initionalize every variable to 0
Time::Time(void)
{
hour=minute=second=0;
}
//set time
void Time::setTime(int h,int m,int s)
{
hour=(h>=0&&h<24)?h:0;
minute=(m>=0&&m<=59)?m:0;
second=(s>=0&&s<=59)?s:0;
}
//print using army´s style
void Time::printMilitary(void)
{
cout<<(hour<10?"0":"")<<hour<<":"
<<(minute<10?"0":"")<<minute<<":"
<<(second<10?"0":"")<<second<<endl;
}
//print using standard style
void Time::printStandard(void)
{
cout<<((hour==0||hour==12)?12:hour%12)<<":"
<<(minute<10?"0":"")<<minute<<":"
<<(second<10?"0":"")<<second
<<(hour<12?"AM":"PM")<<endl;
}
//testing for simple class Time
int main(void)
{
Time t;
cout<<"The initial military time is:";
t.printMilitary();
cout<<"\nThe initial standard time is:";
t.printStandard();
t.setTime(13,27,6);
cout<<"\n\nMilitary time after set time is:";
t.printMilitary();
cout<<"\n\nStandard time after set time is:";
t.printStandard();
t.setTime(99,99,99);
cout<<"\n\nAfter attempting invalid settings:\n"
<<"Military time is:";
t.printMilitary();
cout<<"Standard time is:";
t.printStandard();
cout<<endl;
system("pause");
return(0);
}
- · InstallShield6.3安装文件制作要点
- · GeoTiff探索成果总结
- · MapObject控件的使用之加入图层
- · 学好VC++的十大良好习惯
- · MapObject控件的使用之图层操作
- · C++的函数和模板函数
- · 使用c++实现Format函数
- · 读者——写者问题
- · MapObject控件的使用之符号绘制
- · 窗口的无效区域 演示程序
- · c++异常处理
- · 软件生态危机
- · 使用Sniffer截获流经本机网卡的IP数据包
- · 《深入浅出MFC》读书笔记(一,二)
- · 消息钩子函数入门篇
- · 利用HOOK拦截封包原理
- · 使用C++(I386+)编译一个纯二进制文件
- · 使用ScopeGuard在运行环境中监测内部变量
- · 如何学习java
- · 借助VMware实现单机使用WinDbg
- · 实现窗体自动隐藏(c代码)
- · 任意规模指派问题的C++类实现
- · Delphi数据库应用程序常见错误
- · 数据结构学习(C++)续——排序【4】选择排序
- · 一个类数据类型的STL例子
- · Windows内存机制解析(二)源代码
- · 平台+插件软件设计思想源代码说明
- · Winamp输入模块编写详解
- · 对错一点间
- · 资源释放
- · boost::any源代码分析
- · STL 简介,标准模板库[1]
- · STL 简介,标准模板库[2]
- · C++性能测试用例
- · 大学书信选1(关于编译器)
- · Loading HTML content from a Stream
- · 类设计系列 --- 析构函数篇
- · SDTimes故事: C++ Builder X: 复仇出击 (English)
