1

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. Is this possible?

Date.h file:

#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

#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

#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)

#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.

T. Aalbaek
  • 23
  • 2
  • 10
  • 4
    You have to specify the object you want to call the member function on. Like e.g. `birthday.getDay()`. Just about any beginners book or tutorial should have shown you that. And if the tutorials or books you read haven't shown you that, then stop using them and [get some good books instead](https://stackoverflow.com/a/388282/440558). – Some programmer dude Feb 27 '18 at 07:59
  • @Someprogrammerdude thx. Your answer helped! Even though a bit rude, it helped. – T. Aalbaek Feb 27 '18 at 08:10
  • Rather handle each month in a separate `if` I would write `int limit; switch(birthday.getMonth()) { case 9: case 4: case 6: case 11: limit = 30; break; case 1: case 3: case 5: case 7: case 8: case 12: limit = 31; break; case 2: limit = birthday.leapYear() ? 29 : 28; default: limit = 0; break; }` and then test if outside `[1..limit]` – Martin Bonner supports Monica Feb 27 '18 at 10:06
  • and the odd sequence 9, 4, 6, 11 is from "30 days hath September, April, June and November..." – Martin Bonner supports Monica Feb 27 '18 at 10:07

2 Answers2

0

In CPR.cpp, you need to

#include "Date.h"

and then, within CPR.cpp, you can create an object of type Date and use its members.

Demosthenes
  • 1,515
  • 10
  • 22
  • -1: You are technically correct, but deeply misleading. `class CPR` has a member function `getDate()` and a member variable `birthday`, as such the OP is almost *certainly* trying to call `birthday.getDay();`. Thjere is also no need to `#include "Date.h"` in `CPR.cpp` because it is included in `CPR.h`. – Martin Bonner supports Monica Feb 27 '18 at 08:06
  • @MartinBonner you are absolutely correct. I called birthday.getDay(); and so on, and it worked. Simple mistake with a simple fix. – T. Aalbaek Feb 27 '18 at 09:47
0

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;
}
seccpur
  • 4,996
  • 2
  • 13
  • 21