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;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C 是一种面向对象的编程概念,用于定义对象的属性和方法。对象是的具体实例化,它具有定义的属性和方法。 在C语言定义通常需要使用结构体来完成,通过定义结构体变量和函数指针的组合,实现了的属性和方法。具体使用时,我们可以先定义一个结构体,然后在结构体定义所需的属性,如整型、字符型等,并且可以定义函数指针作为结构体成员以实现相关的方法。 使用和对象主要涉及到两个方面:定义和对象的使用定义主要包括属性和方法的定义。属性定义的数据成员,用于描述的特征;方法定义的成员函数,用于定义的行为。在定义时,我们可以使用不同的访问修饰符来限制属性和方法的访问权限,如public、private和protected等。 对象的使用主要包括创建对象和调用对象的方法。创建对象时,我们可以使用名加上括号来实例化一个对象,并可以通过赋值操作初始化对象的属性。调用对象的方法可以使用对象名加上点运算符,再加上方法名的方式进行调用。 在程序和对象的定义使用是相互依赖的。定义为对象的创建和使用提供了模板,而对象的使用则需要依赖定义。通过和对象的结合使用,我们可以实现程序的模块化和代码的重用,提高了程序的可维护性和扩展性。 总之,和对象的定义使用是面向对象编程的核心概念,它们可以帮助我们更好地组织程序,提高代码的可读性和可复用性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值