简体   繁体   中英

Using int in a header file?

I have been working on a header file for the past couple hours and am having an issue outputting a value that is stored in the constructor. The value is an int but it won't let me store any number above 7 and when I output it using a function it comes out a totally different number. I am doing this all within a header file and using a function in the .cpp to output the data. I'm fairly new to C++ so it's probably an amateur mistake. Any help would be appreciated!!

Header File ----

#ifndef PATIENT_DEMO_CLASS
#define PATIENT_DEMO_CLASS

// system defined preprocessor statement for cin/cout operations
#include <iostream.h>

// programmer defined preprocessor statement for setreal operation
#include "textlib.h"

// programmer defined preprocessor statement for String
#include "tstring.h"

class PatientDemographicInformation
{
    private:
int patientDateOfBirth;

public:
// constructor
PatientDemographicInformation(int dateOfBirth);

// returns the patient's age
int getPatientAge( );
};
PatientDemographicInformation::PatientDemographicInformation(int dateOfBirth)
{
    patientDateOfBirth = dateOfBirth;
}

int PatientDemographicInformation::getPatientAge( )
{
   return patientDateOfBirth;
}
#endif

.cpp ----

#include <iostream.h>
#include <tstring.h>
#include "PatientDemographicInformation.h"

int main( )
{
    PatientDemographicInformation john(11161990);

    cout << john.getPatientAge() << endl;

    return 0;
 }

Pure guess, here.

In C, C++ and many other languages, integers written with a leading 0 are octal ; that is, they are in base 8 rather than base 10.

If you're doing something like:

dateOfBirth = 070503;

then that will be interpreted as an octal number (28995 in decimal). Since octal numbers can only have digits 0-7, the following will be illegal:

dateOfBirth = 090503;

I suggest you don't encode dates in this form, if that's what you are doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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