ehsan_faal
جمعه 15 خرداد 1394, 17:56 عصر
سلام دوستان.
فصل 7 کتاب دایتل مثال زیر رو برای قسمت ترکیب که از مباحث شی گراییه آورده.
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int=1,int=1,int=1990);
void print() const;
~Date();
private:
int month,year,day;
int checkDay(int);
};
#endif // DATE_H
#include "Date.h"
#include <iostream>
using namespace std;
Date::Date(int mn,int dy,int yr)
{
if (mn>0 && mn<=12)
{
month=mn;
}else
{
month=1;
cout<<"Month "<<mn<<" invalid. Set to month 1.\n";
}
year=yr;
day=checkDay(dy);
cout<<"Date Object constructor for date ";
print();
cout<<endl;
}
void Date::print() const
{
cout<< month<<'/'<<day<<'/'<<year;
}
Date::~Date()
{
cout<<"Date object destructor for date ";
print();
cout<<endl;
}
int Date::checkDay(int testDay)
{
static const int daysPerMonth[13]=
{0,31,28,31,30,31,30,31,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<<"Day " <<testDay<< " invalid.Set to Day 1.\n";
return 1;
}
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h"
class Employee
{
public:
Employee(char *,char *,int ,int ,int,int,int,int);
void print() const;
~Employee();
private:
char FirstName[25];
char LastName[25];
const Date BirthDate;
const Date HairDate;
};
#endif // EMPLOYEE_H
#include "Employee.h"
#include <iostream>
#include <cstring>
using namespace std;
Employee::Employee(char *fname,char *lname,int bmonth ,int bday,int byear,int hmonth,int hday,int hyear)
:BirthDate(bmonth,bday,byear), HairDate(hmonth,hday,hyear)
{
int length=strlen(fname);
length=(length<25?length:24);
strncpy(FirstName,fname,length);
FirstName[length]='\0';
length=strlen(lname);
length=(length<25?length:24);
strncpy(LastName,lname,length);
LastName[length]='\0';
cout<<"Employee object constructor: "
<<FirstName<<" "<<LastName<<endl;
}
void Employee::print() const
{
cout<<LastName<<" , "<<FirstName<<"\nHired: ";
HairDate.print();
cout<<"Birth date: ";
BirthDate.print();
cout<<endl;
}
Employee::~Employee()
{
cout<<"Employee object destructor: "
<<LastName<<" , "<<FirstName<<endl;
}
و این هم فایل:
#include <iostream>
#include "Employee.h"
using namespace std;
int main()
{
Employee e("Bob","Jones",7,24,1949,3,12,1988);
cout<<"\n";
e.print();
cout<<"\nTest Date constructor with invalid values:\n";
Date d(14,35,1994);
cout<<endl;
return 0;
}
سوال من اینه که توی فایل main چه جوری یه شی از Date ساخته و چیزی که از اون هم واسم عجیب تره اینه که بعد از ساخت شی d دوباره انگار سازنده شی e رو هم صدا میزنه.
توی خود کتاب چیزی در موردش ننوشته و توی سایتای خارجی هم نمیدونستم دقیقا باید دنبال چی بگردم، ممنون میشم مراحل رو از جایی که شی d میخواد ساخته بشه یه توضیحی بدید.
با تشکر
فصل 7 کتاب دایتل مثال زیر رو برای قسمت ترکیب که از مباحث شی گراییه آورده.
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int=1,int=1,int=1990);
void print() const;
~Date();
private:
int month,year,day;
int checkDay(int);
};
#endif // DATE_H
#include "Date.h"
#include <iostream>
using namespace std;
Date::Date(int mn,int dy,int yr)
{
if (mn>0 && mn<=12)
{
month=mn;
}else
{
month=1;
cout<<"Month "<<mn<<" invalid. Set to month 1.\n";
}
year=yr;
day=checkDay(dy);
cout<<"Date Object constructor for date ";
print();
cout<<endl;
}
void Date::print() const
{
cout<< month<<'/'<<day<<'/'<<year;
}
Date::~Date()
{
cout<<"Date object destructor for date ";
print();
cout<<endl;
}
int Date::checkDay(int testDay)
{
static const int daysPerMonth[13]=
{0,31,28,31,30,31,30,31,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<<"Day " <<testDay<< " invalid.Set to Day 1.\n";
return 1;
}
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h"
class Employee
{
public:
Employee(char *,char *,int ,int ,int,int,int,int);
void print() const;
~Employee();
private:
char FirstName[25];
char LastName[25];
const Date BirthDate;
const Date HairDate;
};
#endif // EMPLOYEE_H
#include "Employee.h"
#include <iostream>
#include <cstring>
using namespace std;
Employee::Employee(char *fname,char *lname,int bmonth ,int bday,int byear,int hmonth,int hday,int hyear)
:BirthDate(bmonth,bday,byear), HairDate(hmonth,hday,hyear)
{
int length=strlen(fname);
length=(length<25?length:24);
strncpy(FirstName,fname,length);
FirstName[length]='\0';
length=strlen(lname);
length=(length<25?length:24);
strncpy(LastName,lname,length);
LastName[length]='\0';
cout<<"Employee object constructor: "
<<FirstName<<" "<<LastName<<endl;
}
void Employee::print() const
{
cout<<LastName<<" , "<<FirstName<<"\nHired: ";
HairDate.print();
cout<<"Birth date: ";
BirthDate.print();
cout<<endl;
}
Employee::~Employee()
{
cout<<"Employee object destructor: "
<<LastName<<" , "<<FirstName<<endl;
}
و این هم فایل:
#include <iostream>
#include "Employee.h"
using namespace std;
int main()
{
Employee e("Bob","Jones",7,24,1949,3,12,1988);
cout<<"\n";
e.print();
cout<<"\nTest Date constructor with invalid values:\n";
Date d(14,35,1994);
cout<<endl;
return 0;
}
سوال من اینه که توی فایل main چه جوری یه شی از Date ساخته و چیزی که از اون هم واسم عجیب تره اینه که بعد از ساخت شی d دوباره انگار سازنده شی e رو هم صدا میزنه.
توی خود کتاب چیزی در موردش ننوشته و توی سایتای خارجی هم نمیدونستم دقیقا باید دنبال چی بگردم، ممنون میشم مراحل رو از جایی که شی d میخواد ساخته بشه یه توضیحی بدید.
با تشکر