简体   繁体   English

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

[英]c++ using functions from another class

I'm still very new to programming, and have bumped into an issue that I'm sure is very basic. 我对编程还是很陌生,遇到了一个我确定是非常基本的问题。

So what I'm trying to do, is use functions that I defined in one .h-file, and then wrote out in the .cpp-file belonging to that .h-file, in another .cpp-file. 所以我想做的是使用我在一个.h文件中定义的函数,然后在属于该.h文件的.cpp文件中写出另一个在.cpp文件中的函数。 Is this possible? 这可能吗?

Date.h file: 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 file 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 file 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 file (only important part) 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;
}

So I'm trying to use the leapYear(), getMonth(), getDay() and getYear() functions from Date.cpp in CPR.cpp. 因此,我尝试使用CPR.cpp中Date.cpp中的jumpYear(),getMonth(),getDay()和getYear()函数。

In CPR.cpp , you need to CPR.cpp ,您需要

#include "Date.h"

and then, within CPR.cpp , you can create an object of type Date and use its members. 然后,在CPR.cpp ,您可以创建类型为Date的对象并使用其成员。

Try this. 尝试这个。 But your program seems still incorrect: 但是您的程序似乎仍然不正确:

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

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

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM