在头文件中使用 int

Using int in a header file?

本文关键字:int 文件      更新时间:2023-10-16

在过去的几个小时里,我一直在处理头文件,并且在输出存储在构造函数中的值时遇到问题。该值是一个 int,但它不允许我存储任何高于 7 的数字,当我使用函数输出它时,它会出现一个完全不同的数字。我在头文件中完成所有这些操作,并使用.cpp中的函数来输出数据。我对C++相当陌生,所以这可能是一个业余错误。任何帮助将不胜感激!!

头文件----

#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;
 }

纯粹的猜测,在这里。

在 C、C++ 和许多其他语言中,用前导 0 编写的整数是八进制的;也就是说,它们以 8 为基数而不是以 10 为基数。

如果您正在执行以下操作:

dateOfBirth = 070503;

然后,这将被解释为一个八进制数(十进制为 28995)。由于八进制数字只能有数字 0-7,因此以下内容是非法的:

dateOfBirth = 090503;

我建议你不要以这种形式对日期进行编码,如果这是你正在做的事情。