C++语言基础 例程 案例:Time类的设计

贺老师的教学链接  本课讲解

#include <iostream>
using namespace std;
class CTime
{
private:
    unsigned short int hour;    // 时
    unsigned short int minute;  // 分
    unsigned short int second;  // 秒
public:
    CTime(int h=0,int m=0,int s=0);
    void setTime(int h,int m,int s);
    //输入输出运算的重载
    friend istream &operator>>(istream &in,CTime &t);
    friend ostream &operator<<(ostream &out,const CTime &t);
    //比较运算符(二目)的重载
    bool operator > (CTime &t);
    bool operator < (CTime &t);
    bool operator >= (CTime &t);
    bool operator <= (CTime &t);
    bool operator == (CTime &t);
    bool operator != (CTime &t);
    //二目运算符的重载
    CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
    CTime operator-(CTime &c);//对照+理解
    CTime operator+(int s);//返回s秒后的时间
    CTime operator-(int s);//返回s秒前的时间
    //一目运算符的重载
    CTime operator++(int);//后置++,下一秒
    CTime operator++();//前置++,下一秒
    CTime operator--(int);//后置--,前一秒
    CTime operator--();//前置--,前一秒
    //赋值运算符的重载
    CTime operator+=(CTime &c);
    CTime operator-=(CTime &c);
    CTime operator+=(int s);//返回s秒后的时间
    CTime operator-=(int s);//返回s秒前的时间
};

//构造函数
CTime::CTime(int h,int m,int s):hour(h),minute(m),second(s) {}
// 设置时间
void CTime::setTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}

// 重载输入运算符>>
istream &operator>>(istream &in,CTime &t)
{
    char ch1,ch2;
    while(1)
    {
        cout<<"请输入时间(hh:mm:ss) ";
        cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;
        if (ch1==':' && ch2==':')
            if (t.hour>-1 && t.hour<24 && t.minute>-1 && t.minute<60 && t.second>-1 && t.second<60) break;
        cerr<<"时间格式不正确! 请重新输入\n";
    }
    return cin;
}

// 重载输出运算符<<
ostream &operator<<(ostream &out, const CTime &t)
{
    out<<t.hour<<':'<<t.minute<<':'<<t.second;
    return out;
}
时间: 2024-07-30 08:49:09

C++语言基础 例程 案例:Time类的设计的相关文章

C++语言基础 例程 案例:MyVector类的设计

贺老师的教学链接  本课讲解 //MyVector类的设计 #include <iostream> using namespace std; class MyVector //定义向量类 { public: MyVector(int m); //构造函数,共有m个元素的向量,元素值预置为0 MyVector(const MyVector &v); //复制构造函数 ~MyVector(); //析构函数:释放动态数组所占用的存储空间 friend istream &operat

C++语言基础 例程 虚基类及应用

贺老师的教学链接  本课讲解 虚基类应用举例 #include <iostream> #include <cstring> using namespace std; class Person { public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; } protected: //保护成员 char name[20]; char sex; int age; }; class Te

C++语言基础 例程 案例:bmp文件格式剖析

贺老师的教学链接  本课讲解 附:二进制文件查看器及示例bmp文件 http://pan.baidu.com/s/1dDjf5uD 用程序读出BMP文件信息 //readbmp.h #ifndef READBMP_H_INCLUDED #define READBMP_H_INCLUDED typedef unsigned char BYTE; typedef unsigned short int UINT; typedef short int WORD; typedef int DWORD; t

C++语言基础 例程 案例:一个接口,多种方法

贺老师的教学链接  本课讲解 用指针输出几何体 #include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); friend ostream & operator<<(ostream &,const Point &); protected: double x,y; }; Point::Point(double a,double b):x(

《C++语言基础》实践参考——类族的设计

返回:贺老师课程教学链接 项目要求 [项目4 - 类族的设计]    按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积.体积并输出并且完成要求的计算任务:    (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试:    (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试:    (3)再以C

C++语言基础 例程 基类与派生类的转换

贺老师的教学链接  本课讲解 指向基类对象的指针变量也可以指向派生类对象 #include <iostream> #include <string> using namespace std; class Student//声明Student类 { public: Student(int, string,float); void display( ); private: int num; string name; float score; }; Student::Student(in

C++语言基础 例程 派生类的构造函数和析构函数

贺老师的教学链接  本课讲解 一个简单派生类的定义 #include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<

C++语言基础 例程 派生类的声明与构成

贺老师的教学链接  本课讲解 派生类 #include <iostream> #include<string> using namespace std; class Student//声明基类Student { public: void sets(int n,string nam,char s); void show( ); protected: //保护部分 int num; string name; char sex ; }; void Student::sets(int n,

C++语言基础 例程 Time类的设计

贺老师的教学链接  本课讲解 Time类的初步实现与测试 #include <iostream> using namespace std; class Time { public: Time(): hour(0), minute(0), sec(0){} Time(int h, int m, int s):hour(h), minute(m), sec(s){} void set_time( ); void show_time( ); void add_a_sec(); //增加1秒钟 voi