aaaa項目的實現
//Date.h文件
#ifndef _Date_H
#define _Date_H
class Date
{
public:
Date();
Date(int year,int month,int day);
void print()const;
private:
int _year;
int _month;
int _day;
};
#endif
//Date.cpp文件
#include "stdafx.h"
#include "Date.h"
#include "process.h"
Date::Date(){
}
Date::Date(int year,int month,int day){
_year=year;
_month=month;
_day=day;
}
void Date::print()const{
printf("出生日期是:%d—%d—%d",_year,_month,_day);
}
//Person.h
#include "Date.h"
#ifndef _Person_H
#define _Person_H
class Person{
public:
Person();
Person(char* name,char* email_address,int year,int month,int day );
public:
char* get_Name()const;
char* get_email_address()const;
Date getBirthDate()const;
void print()const;
private:
char* _name;
char* _email_address;
Date date;
};
#endif
//Person.cpp文件
#include "stdafx.h"
#include "Person.h"
#include "Date.h"
Person::Person(){
}
Person::Person(char* name,char* email_address,int year,int month,int day )
:date(year,month,day) {
_name=name;
_email_address=email_address;
}
char* Person::get_Name()const{
return _name;
}
char* Person::get_email_address()const{
return _email_address;
}
Date Person::getBirthDate()const{
return date;
}
void Person:: print()const{
printf("姓名:%s,郵箱:%s",_name,_email_address);
printf(",");
date.print();
}
//PersonSet.h文件
#ifndef _PersonSet_H
#define _PersonSet_H
#include "Person.h"
class PersonSet{
public:
PersonSet(int initial_size);
~PersonSet();
public:
void add(Person& element);
Person& nextElement();
Person& removeElement();
Person& removeElement(int index);
int size();
void print();
void reset();
private:
Person** _elements;
int _capacity;
int _size;
int _index;
};
#endif
//PersonSet.cpp文件
#include "stdafx.h"
#include "PersonSet.h"
#include "Person.h"
#include "Date.h"
PersonSet::PersonSet(int i_size){
_size=0;
_capacity=i_size;
_index=1;
_elements=new Person*[_capacity];
}
PersonSet::~PersonSet(){
delete [] _elements;
}
void PersonSet::add(Person& element){
if(_size==_capacity){
Person** temp= _elements;
_elements=new Person*[_capacity*2];
for(int i=0;i<_size;i++){
_elements[i]=temp[i];
}
_capacity*=2;
delete []temp;
}
_elements[_size++]=&element;
_index=_size;
}
Person& PersonSet::nextElement() {
return *(_elements[(_index)++]);
}
Person& PersonSet::removeElement(){
_size--;
Person* p = _elements[_size];
if(_size<_capacity/2){
Person** temp=_elements;
_elements=new Person*[_capacity/2];
for(int i=0;i<_size;i++){
_elements[i]=temp[i];
}
_capacity/=2;
delete [] temp;
}
return *p;
}
Person& PersonSet::removeElement(int index){
_size--;
Person* p = _elements[index];
if(_size<_capacity/2){
Person** temp=_elements;
_elements=new Person*[_capacity/2];
for(int i=0;i<_size;i++){
_elements[i]=temp[i];
}
_capacity/=2;
delete [] temp;
}
return *p;
}
int PersonSet:: size(){
return _size;
}
void PersonSet::print(){
for(int i=0;i<_size;i++){
// printf("%s%s\n",(*_elements[i]).get_Name(),(*_elements[i]).get_email_address());
(*_elements[i]).print();
printf("\n");
}
}
void PersonSet::reset(){
_index=0;
}