Date类
#ifndef Date_hpp
#define Date_hpp
#include <stdio.h>
class Date
{
public:
Date();
Date(int day, int month, int year);
void Print() const ;
private:
int _year;
int _month;
int _day;
};
#endif /* Date_hpp */
#include "Date.hpp"
#include<iostream>
using namespace std;
Date::Date(){}
Date::Date( int day, int month, int year )
{
_year = year;
_month = month;
_day = day;
}
void Date::Print() const
{
cout<<"出生日期:"<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
Person类
#ifndef Person_hpp
#define Person_hpp
#include <stdio.h>
#include"Date.hpp"
class Person
{
public:
Person(void);
Person(char * their_name, char * email, int day, int month, int year);
char * GetName() const ;
char * GetEmailAddress() const ;
Date GetBirthDate() const ;
void Print() const ;
~Person();
private:
char* name;
char* email_address;
Date date;
};
#endif /* Person_hpp */
#include "Person.hpp"
#include<string.h>
#include<iostream>
using namespace std;
Person::Person(void){}
Person::Person(char * their_name, char * email, int day, int month, int year):date(day,month,year)
{
name = new char [strlen(their_name)+1];
strcpy(name,their_name);
email_address = new char[strlen(email)+1];
strcpy(email_address,email);
}
char * Person::GetName() const
{
return name;
}
char * Person::GetEmailAddress() const
{
return email_address;
}
Date Person::GetBirthDate() const
{
return date;
}
void Person::Print() const
{
cout<<"姓名:"<<name<<endl;
cout<<"邮箱地址:"<<email_address<<endl;
date.Print() ;
}
Person::~Person()
{
if(name != NULL)
{
delete name;
name = NULL;
}
if( email_address != NULL)
{
delete email_address;
email_address = NULL;
}
cout<<"Person析构函数!"<<endl;
}
PersonSet类
#ifndef PersonSet_hpp
#define PersonSet_hpp
#include"Date.hpp"
#include"Person.hpp"
#include <stdio.h>
class PersonSet
{
public:
PersonSet (int initial_size = 4);
~ PersonSet (void);
void Add(Person* element);
Person & NextElement();
Person & RemoveElement();
Person & RemoveElement( int index );
int Size() const;
void Print() const;
private:
Person ** _elements;
int _capacity; //volume of the set
int _size; //number of elements in the set
int _index;
};
#endif /* PersonSet_hpp */
#include "PersonSet.hpp"
#include<iostream>
using namespace std;
PersonSet::PersonSet (int initial_size)
{
_elements = new Person*[initial_size];
_capacity = initial_size;
_size = 0;
_index = 0;
}
PersonSet::~ PersonSet (void)
{
if(_elements != NULL)
{
delete []_elements;
}
cout<<"PersonSet析构函数!"<<endl;
}
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;
_size++;
}
Person& PersonSet::NextElement() //调用时先判断_index的值是否大于_size;
{
if(_index >= _size)
{
_index = 0;
}
return *_elements[_index++];
}
// 从set中移除最后一个成员,如果Set空的数据超过一半,释放一些内存
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;
}
//从Set中的index索引处移除成员。如果Set空的数据超过一半, 释放一些内存
Person& PersonSet:: RemoveElement( int index ){
Person *p = _elements[_index];
for(int i = index; i < _size; i++)
{
_elements[i] = _elements[i + 1];
}
_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;
}
int PersonSet::Size() const //answer the number of elements in the set.
{
return _size;
}
void PersonSet::Print() const //print the elements of the set
{
for(int i = 0; i < _size; i++)
{
_elements[i]->Print();
}
}
main.cpp
#include"Date.hpp"
#include"Person.hpp"
#include"PersonSet.hpp"
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
// Person p1("Lou", "lou@chat.ca", 20, 6, 1960);
// Person p2("Frank", "f123@chat.ca", 20, 3, 1967);
// Person p3("Ann", "ann@chat.ca", 20, 8, 1960);
//
// p2.Print();
// cout<<p1.GetName()<<endl;
// cout<<p1.GetEmailAddress()<<endl;
// p1.GetBirthDate().Print();
Person *p1 = new Person((char*)"Lou", (char*)"lou@chat.ca", 20, 6, 1960);
Person *p2 = new Person((char*)"Frank", (char*)"f123@chat.ca", 20, 3, 1967);
Person *p3 = new Person((char*)"Ann", (char*)"ann@chat.ca", 20, 8, 1960);
// PersonSet per;
// per.Add(p1);
// per.Add(p2);
// per.Add(p3);
// per.Print();
//
// per.RemoveElement();
// per.Print();
// per.RemoveElement(1);
// per.Print();
// int size = per.Size();
// for(int i = 0; i < size; i++)
// {
// per.NextElement().Print();
// }
PersonSet boys, girls;
boys.Add( p1);
if (p1 != &boys.RemoveElement() )
{
cout << "ERROR: the objects are different \n";
}
else
{
cout << "Good, the objects are the same \n";
}
boys.Add( p1);
boys.Add( p2);
girls.Add( p3);
boys.Add((new Person((char*)"John",(char*) "f123@chat.ca", 20, 3, 1967)));
girls.Add((new Person((char*)"Sue",(char*) "f123@chat.ca", 20, 3, 1967)));
boys.Add((new Person((char*)"Frank",(char*)"frank@chat.ca", 25, 4, 1958)));
girls.Add((new Person((char*)"Mary",(char*) "mary@chat.ca", 25, 4, 1955)));
boys.Add((new Person((char*)"John", (char*)"johnchat.ca", 12, 12, 1970)));
boys.Add((new Person((char*)"John",(char*) "f123@chat.ca", 20, 3, 1967)));
girls.Add((new Person((char*)"Sue",(char*) "f123@chat.ca", 20, 3, 1967)));
boys.Add((new Person((char*)"Frank",(char*)"frank@chat.ca", 25, 4, 1958)));
girls.Add((new Person((char*)"Mary",(char*) "mary@chat.ca", 25, 4, 1955)));
boys.Add((new Person((char*)"John", (char*)"johnchat.ca", 12, 12, 1970)));
int numberOfBoys = boys.Size();
cout << "number of boys = " << numberOfBoys << endl;
for(int i = 0; i<7; i++)
{
Person & boy = boys.RemoveElement();
// boy.Print();
delete &boy;
}
cout << "number of boys = " << boys.Size() << endl;
boys.Print();
// Person & boy = boys.RemoveElement();
// boy.Print();
// delete &boy;
// for(int i = 0; i<numberOfBoys; i++)//delete all the boys from the heap
// {
// Person & boy = boys.NextElement();
// boy.Print();
// delete &boy;
// }
cout << "number of girls = " << girls.Size() << "\n";
int numberOfGirls = girls.Size();
for(int i = 0; i<numberOfBoys; i++)
{
boys.NextElement().Print();
}
for(int i = 0; i<numberOfGirls; i++)//delete all the girls from the heap
{
Person & her = girls.RemoveElement();
delete &her;
}
return 0;
}