c ++使用另一个类的函数

时间:2018-02-27 07:57:34

标签: c++ header-files

我对编程仍然很陌生,并且遇到了一个我确定非常基本的问题。

所以我正在尝试做的是使用我在一个.h文件中定义的函数,然后在另一个.cpp文件中写入属于该.h文件的.cpp文件。这可能吗?

Date.h文件:

#pragma once

#ifndef DATE_GUARD
#define DATE_GUARD

#include <iostream>

class Date
{
public:
    Date();
    Date(int);
    int getDay();
    int getMonth();
    int getYear();
    int getDate();
    bool leapYear();
    ~Date();
private:
    int theDate;
};

#endif

CPR.h文件

#pragma once
#include"Date.h"

class CPR
{
public:
    CPR();
    CPR(unsigned long); //DDMMYYXXXX
    Date getDate();
    int getFinalFour();
    bool validate();
    int getFirstCipher();
    int getSecondCipher();
    int getThirdCipher();
    int getFourthCipher();
    int getFifthCipher();
    int getSixthCipher();
    int getSeventhCipher();
    int getEighthCipher();
    int getNinthCipher();
    int getTenthCipher();
    ~CPR();

private:
    int CPRNummer;
    Date birthday;
};

Date.cpp文件

#include "Date.h"

Date::Date()
{
}

Date::Date(int aDate)
{
    theDate = aDate;
}

int Date::getDay()
{
    return theDate / 10000;
}

int Date::getMonth()
{
    return (theDate / 100) % 100;
}

int Date::getYear()
{
    return (theDate % 100);
}

bool Date::leapYear()
{
    if (getYear() % 4 != 0)
        return false;
    if (getYear() % 100 == 0 && getYear() % 400 != 0)
        return false;
    return true;
}

int Date::getDate()
{
    return theDate;
}

Date::~Date()
{
}

CPR.cpp文件(仅限重要部分)

#include "CPR.h"
#include "Date.h"
#include <iostream>

bool CPR::validate()
{
    if (getDay() < 1 || getDay() > 31)
        return false;

    if (getMonth() < 1 || getMonth() > 12)
        return false;

    if (getYear() < 1700 || getYear() > 2100)
        return false;

    if (getMonth() == 2 && leapYear() && getDay() > 29)
        return false;

    if (!leapYear() && getMonth() == 2 && getDay() > 28 || getMonth() == 4 && getDay() > 30 || getMonth() == 6 && getDay() > 30 || getMonth() == 9 && getDay() > 30 || getMonth() == 11 && getDay() > 30)
        return false;
    return true;
}

所以我试图在CPR.cpp中使用Date.cpp中的leapYear(),getMonth(),getDay()和getYear()函数。

0 个答案:

没有答案