`
sshzhangwg
  • 浏览: 69004 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

C++ 中定义和使用简单的类

 
阅读更多
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include<cstdlib>
using namespace std;

class Date
{
public:
    Date();//构造函数
    Date(const Date&);//拷贝构造函数
    Date strToDate(string);//将string转化为Date
    void setDate(string);//设置日期
    void printDate();//打印日期
private:
    int year;
    int month;
    int day;
    bool pass;
    bool checkFormat(string);//验证日期输入格式
    void validate();//验证日期数值有效性
    bool leapYear(int);//闰年判断
};

class A 
{
private:
	int age;
public:
	int get_age();
	void set_age();
};

int A::get_age()
{
  age = 28;
  return age;
}

void A::set_age() 
{
	age = 100;
}

Date::Date()
{
    year = 2006;
    month = day = 1;
    validate();
}

Date::Date(const Date& date)
{
    year = date.year;
    month = date.month;
    day = date.day;
    pass = date.pass;
}

Date Date::strToDate(string ds)
{
    // yyyy:mm:dd
    Date d;
    d.setDate(ds);
    return d;
}

void Date::setDate(string ds)
{
    if( !checkFormat(ds) )
    {
        cout << "日期格式错误" << endl;
        pass = false;
        return;
    }
    validate();
    if(pass)
    {
        year = atoi(ds.substr(0, 4).c_str());
        month = atoi(ds.substr(5, 2).c_str());
        day = atoi(ds.substr(8, 2).c_str());
    }
}

void Date::printDate()
{
    if (pass)
    {
        cout << year << "年"
             << month << "月"
             << day << "日" << endl;
    }
    else
    {
        cout << "日期错误" << endl;
    }
}

void Date::validate()
{
    pass = false;
    if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31)
    {
        return;
    }
    if (month == 2)
    {
        if (leapYear(year))
        {
            if (day <= 29)
            {
                pass = true;
            }
            return;
        }
        else
        {
            if (day <= 28)
            {
                pass = true;
            }
            return;
        }
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
        if (month <= 30)
        {
            pass = true;
        }
        return;
    }
    else
        pass = true;
}

bool Date::leapYear(int year)
{
    if (year % 100 == 0)
    {
        if (year % 400 == 0)
        {
            return true;
        }
    }
    else
    {
        if ((year % 4) == 0)
        {
            return true;
        }
    }
    return false;
}

bool Date::checkFormat(string ds)
{
    //yyyy:mm:dd
    if (ds.length() != 10
            || ds[4] != ':'
            || ds[7] != ':')
    {
        return false;
    }

    for (int i = 0; i < (int)ds.length(); i++ )
    {
        if( i != 4 && i != 7)
        {
            if (ds[i] < '0' || ds[i] > '9')
            {
                return false;
            }
        }
    }
    return true;
}


int _tmain(int argc, _TCHAR* argv[])
{
    // 测试是否能正确设置日期
    string str;
    cout << "--TEST:请输入一个日期<yyyy:mm:dd>:" << endl;
    cin >> str;
    Date d;
    d.setDate(str);
    d.printDate();

    //测试拷贝构造函数
    cout << "--TEST:调用拷贝构造函数" << endl;
    Date dc = d;
    dc.printDate();

    //测试字符串到日期转换的函数
    cout << "--TEST:将字符串’2012:03:28‘转化为Date类型并输入" << endl;
    Date dt = d.strToDate("2012:03:28");
    dt.printDate();
	
	A a;
	a.set_age();
	cout<<"您目前的年龄是:";
	cout<<a.get_age();
	system("pause"); 
    return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics