C++有关问题,帮小弟我看看,分大大地有
C++问题,帮我看看,分大大地有
//Date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int=1,int=1,int=1900);
void print() const;//此处为打印函数,声明成const类,有点问题
~Date();
private:
int month;
int day;
int year;
int checkDay(int) const;//验证输入是否合法
};
#endif
//Date.cpp
#include <iostream>
using namespace std;
#include "Date.h "
Date::Date(int mn,int dy,int yr)
{
if(mn> 0 && mn <=12)
month=mn;
else
{
month=1;
cout < < "invalid month( " < <mn < < ")set to 1.\n ";
}
year=yr;
day=checkDay(dy);
cout < < "Date object construtor for date ";
print();//构造函数调用了const成员函数
cout < <endl;
};//end Date construtor
void Date::print() const
{
cout < <month < < '/ ' < <day < < '/ ' < <year;
}
Date::~Date()
{
cout < < "Date object destrutor for date ";
print();//析构函数调用const函数成员
cout < <endl;
}
int Date::checkDay(int testDay) const
{//这个函数可以省略不看,没有问题,就是个验证日期是否合法的
static const int daysPerMonth[13]=
{0,31,28,31,30,31,30,31,30,31,30,31};
if(testDay> 0 && testDay <=daysPerMonth[month])
return testDay;
if(month==2 && testDay==29 && (year%400==0||(year%4==0 && year%100!=0)))
return testDay;
cout < < "invalid day ( " < <testDay < < ")set to 1.\n ";
return 1;
}//end function checkDay
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h "//include Date class definition
class Employee
{
public:
Employee(const char * const,const char * const,const Date &,const Date &);//这个也不太明白,为什么连用两个const,概念有点模糊
void print() const;
~Employee();
private:
char firstName[25];
char lastName[25];
const Date birthDate;
const Date hireDate;
};//end class Employee
#endif
//Employee.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "Date.h "
#include "Employee.h "
Employee::Employee(const char * const first,const char * const last,
const Date &dateOfBirth,const Date &dateOfHire)
:birthDate(dateOfBirth),hireDate(dateOfHire)
//还有次处的两个const,不太清楚
{
int length=strlen(first);
length=(length <25 ? length:24);
strncpy(firstName,first,length);
firstName[length]= '\0 '; //复制字符串没问题可以不看
length=strlen(last);
length=(length <25 ? length:24);
strncpy(lastName,last,length);
lastName[length]= '\0 ';
//Date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int=1,int=1,int=1900);
void print() const;//此处为打印函数,声明成const类,有点问题
~Date();
private:
int month;
int day;
int year;
int checkDay(int) const;//验证输入是否合法
};
#endif
//Date.cpp
#include <iostream>
using namespace std;
#include "Date.h "
Date::Date(int mn,int dy,int yr)
{
if(mn> 0 && mn <=12)
month=mn;
else
{
month=1;
cout < < "invalid month( " < <mn < < ")set to 1.\n ";
}
year=yr;
day=checkDay(dy);
cout < < "Date object construtor for date ";
print();//构造函数调用了const成员函数
cout < <endl;
};//end Date construtor
void Date::print() const
{
cout < <month < < '/ ' < <day < < '/ ' < <year;
}
Date::~Date()
{
cout < < "Date object destrutor for date ";
print();//析构函数调用const函数成员
cout < <endl;
}
int Date::checkDay(int testDay) const
{//这个函数可以省略不看,没有问题,就是个验证日期是否合法的
static const int daysPerMonth[13]=
{0,31,28,31,30,31,30,31,30,31,30,31};
if(testDay> 0 && testDay <=daysPerMonth[month])
return testDay;
if(month==2 && testDay==29 && (year%400==0||(year%4==0 && year%100!=0)))
return testDay;
cout < < "invalid day ( " < <testDay < < ")set to 1.\n ";
return 1;
}//end function checkDay
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h "//include Date class definition
class Employee
{
public:
Employee(const char * const,const char * const,const Date &,const Date &);//这个也不太明白,为什么连用两个const,概念有点模糊
void print() const;
~Employee();
private:
char firstName[25];
char lastName[25];
const Date birthDate;
const Date hireDate;
};//end class Employee
#endif
//Employee.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "Date.h "
#include "Employee.h "
Employee::Employee(const char * const first,const char * const last,
const Date &dateOfBirth,const Date &dateOfHire)
:birthDate(dateOfBirth),hireDate(dateOfHire)
//还有次处的两个const,不太清楚
{
int length=strlen(first);
length=(length <25 ? length:24);
strncpy(firstName,first,length);
firstName[length]= '\0 '; //复制字符串没问题可以不看
length=strlen(last);
length=(length <25 ? length:24);
strncpy(lastName,last,length);
lastName[length]= '\0 ';