`
_jia_shun
  • 浏览: 24703 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

C++大学基础教程_10_3_组成:对象作为类的成员

 
阅读更多
//Date.h

#ifndef DATE_H
#define DATE_H

class Date
{
public:
	Date(int = 1,int = 1,int = 1900);//参数分别为月日年
	~Date();
	void print() const ;
private:
	int month;
	int day;
	int year;
	int checkDay(int) const ;//检验每月的天数是否符合逻辑
};
#endif

 

//Date.cpp

#include "Date.h"
#include <iostream>
using namespace std;

Date::Date(int mh,int dy,int yr)
{
	if(mh >0 && mh <= 13)
		month = mh;
	else{
		month = 1;
		cout << "Invalid month (" << mh << ") set to 1.\n";
	}

	year = yr;
	day = checkDay(dy);
	cout << "Date object constructor for date ";
	print();
}

Date::~Date()
{
	cout << "Date object destructor for date ";
	print();
}

void Date::print() const
{
	cout << month << '/'  << day << '/' << year << endl;
}

int Date::checkDay(int testDay) const
{
	//平年中的每月天数1-12月分别对应daysPerMonth[1]-[12]
	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 << "Invalid day (" << testDay << ") set to 1.\n";
	return 1;
}

 

//Employee.h

#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

#include "Date.h"

class Employee
{
public:
	//构造函数参数分别为名,姓,出生日期,开始工作的日期
	Employee(const char * const,const char * const,const Date &,const Date &);
	~Employee();
	void print() const;
private:
	char firstName[25];
	char lastName[25];
	const Date birthDate;
	const Date hireDate;
};
#endif

 

//Employee.cpp

#include "Employee.h"
#include "Date.h"
#include <iostream>
#include <cstring>/*using std::strlen  using std::strncpy*/
using namespace std;

Employee::Employee(const char * const first,const char * const last,
	                            const Date &dateOfBirth,const Date &dateOfHire)
								: birthDate(dateOfBirth),
								  hireDate(dateOfHire)      //成员初始化器
{
	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';

	cout << "Employee object constructor: " 
		   << firstName << " " << lastName << endl;
}

Employee::~Employee()
{
	cout << "Employee object destructor:"
		   << firstName << " " << lastName << endl;
}

void Employee::print() const
{
	cout << firstName << "," << lastName << endl;
	cout << " Hired:";
	hireDate.print();
	cout << "BirthDay:";
	birthDate.print();
}

 

//main.cpp

#include "Employee.h"
#include <iostream>
using namespace std;

int main()
{
	{//这个大括号仅仅为了测试析构函数是否正常工作,可以去掉,,
	Date birth(7,24,1959);
	Date hire(3,12,1988);
	Employee manager("Bob","Lanester",birth,hire);
	cout << endl;
	manager.print();
	cout << "\nTest Date constructor with invalid values:\n";
	Date lastDayOff(14,35,1976);
	}
	system("pause >> cout");
	return 0;
}

 

 

  • 大小: 136.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics