美文网首页C++精选
C++大作业一<<人员信息管理系统>>

C++大作业一<<人员信息管理系统>>

作者: 海曼 | 来源:发表于2016-08-16 14:48 被阅读776次

第一次作业

这个作业将让你去练习建立一些简单的类和使用C++的基本功能,包括: 封装,引用,动态内存开辟,简单构造函数和析构函数和const.

这个程序片段未􏰀供编或调试.做出合理的错误修正事你任务的一部分,或要求在类里明.

一般的,我们会给你最基本部分的代码.如果你需要它们,你总是可以在一个类里添加额外的变量或方法.这个作业被分为三个部分来显示了如何工作.

1.设计要求

第一部分) 构建简单的类

R1.1) 创建一个Person类,其模型在下面的代码结构里.

R1.2) 人类(Persons)应该有一些属性: name, email_address, birthdate as作为表示下面类(的属性)的􏰁述.

R1.3) 按下面的要求创建一个Date类.

R1.4) 每个类都应该有一个按<<运算符的输出的Print函数.这个函数应该􏰀供打印这个类的文本表示,用cout或其他流.

这些是非常简单的C++的类,它们的变量不是对象,但是只是C++部分的

内在类型.而且这个变量既不是值或指针(它们也不是引用).

//file Date.h

class Date{

public:

       Date();

       Date( int year, int month, int day );...

private:

       int _year;

       int _month;

       int _day;

};

//end file Date.h

//filePerson.h

class Person{

public:

       Person(void);

       Person(char * their_name, char * email, int day, int month, int year);char * GetName();

       char * GetEmailAddress();

       Date GetBirthDate();

       void Print();

private:

       char* name;

       char* email_address;

       Date date;

};

//end file Person.h

第二部分)构建一个容器类Set container.

R2.1) 建立一个set的模型PersonSet类,并且它只能保存Person的对象.

R2.2) 这个set应该存储person的地址(指针),以便可以获取原始对象(非

拷贝).

R2.3) set的存储应该用动态数组来保存Person的指针(用new来创建),但是set不应该有界限(数组大小),它们应该在成员进行添加或移除时,适当进行扩展..

R2.4) 成员不按任何特定顺序存储(无排序).

R2.5) set允许存储副本(相同对象).

R2.6) Add()函数应该当在添加的时候,并且需要的情况,进行扩展数组大小并且输出一串信息.

R2.7) Remove()函数应该在移除的时候,并且在需要的情况,可以进行缩小数组大小并输出一串信息.

R2.8) Add()函数应该带一个引用类型的参数(Person&).

R2.9) 迭代应该通过NextElement()函数来􏰀供.

R2.10) NextElement()和RemoveElement()应该通过引用返回

对于现在我们将去建立sets去只保存Person对象,因此类名是personSet

//file PersonSet.h

class PersonSet{

public:

//default constructor allocate appropriate heap storage store elements on

//heap array declared like this: new Person*[initial_size];

       PersonSet (int initial_size = 4);

//store element in the set if the set is full allocate more memory

       ~ PersonSet (void);

public:

       void Add(Person & element) ;

       Person & NextElement() ;

//从set中移除最后一个成员

//如果Set空的数据超过一半,释放一些内存Person & RemoveElement();

//从Set中的index索引处移除成员

//如果Set空的数据超过一半,释放一些内存

       Person & RemoveElement( int index );

       int Size(); //answer the number of elements in the set.void                  

       Print();//print the elements of the set

      //void Reset();

private:

       Person ** _elements;

      int  _capacity ;  //volume of the set

      int  _size ;      //number of elements in the set

      int _index ;

} ;

Growable Sets(可扩展的Set)

你的Set应该使用一个数组来存储成员并且数组应该使用new在堆上分配.

当set被创建时,它有一个指定的尺寸(指数组).如果它满了,必须从堆中开辟更多的内存.这个将会在某一次Add()函数的期间.

如果set中有太多空的位置了(数组),它应该去释放它的一些内存.这将发生在某一次remove函数调用的期间.

建议: 如果有一半以上空间未存储Person指针(也就是上面所说的空),那么它就去释放一些内存(一半内存).

你的set类应该在它扩展和缩小空间是用cout打印出一串信息.这个信息应该标明内存开辟或释放后的所能存储的最大值(capacity).

这里是容器的用于基本的扩展和缩小空间的实现.

你可以用这些代码来作为你实现的基本代码.

void AddElement( Person& aPerson )

{

// Relocate the array space

       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++] = &aPerson;

}

Person& RemoveElement()

{

       size--;

       Person* p = _elements[size];//shrink the container

       if(size < capacity/2)

       {

             cout << "shrinking\n";

             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;

}

迭代:

NextElement()函数为简单的迭代器􏰀供了一个方法.

NextElement()函数应该在set每次调用时都返回下一个成员.

特别的,如果NextElement()函数在一个尺寸为n的set中被调用了n次,

那么它应该也就迭代通过了set的所有成员(访问了所有成员)..

如果你有一个n尺寸的set,如果在第n次之后(n+1)再调用NextElement()函数,将会从开头再次迭代.要实现这个或许要添加一个私有成员变量,在set中是index,来保持当前元素和每次NextElement()调用时的位置(track) ,它可以让index每次增加并且在适当的时候返回第0个索引位置.

(也就是说NextElement靠私有成员_index来控制位置的.)...

第三部分)添加const修饰

应该在参数的地方添加const修饰符,或函数应该是const保护.

对你完成程序的第一部分和第二部分进行如下改变.

R3.1)这个将要求你像Print()函数那样作为const来定义.

例如:

void Print() const { ... }

R3.2) .

改变一下SetOfPersons类里的public接口,以便可以让它们需要的时候

使用const修饰符.

//file SetOfPersons.hclass PersonSet

{

public:

       PersonSet (int initial_size = 4) ;

       ~ PersonSet (void) ;

       void Add(Person & element) ;

       Person & NextElement()const ;

       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;

} ;

R3.3)在任何地方的constconst类里放置const的标识符,都应该有const修饰符.

2.测试要求第四部分)测试规格

R4.1)在完成了你的类之后,你应该可以在执行下面的主程序

//file main.cpp

#include <iostream>

#include<string.h>

#include "Date.h"

#include "Person.h"

#include " PersonSet.h"

using namespace std;

int main()

{

//declare some const persons

       Person *p1 = new Person("Lou", "lou@chat.ca", 20, 6, 1960);

       Person *p2 = new Person("Frank", "f123@chat.ca", 20, 3, 1967);

       Person *p3 = new Person("Ann", "ann@chat.ca", 20, 8, 1960);

       PersonSet boys, girls;

       boys.Add( *p1);

//test to see if the same object is retrieved from the set.

       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("John", "f123@chat.ca", 20, 3, 1967)));

       girls.Add(*(new Person("Sue", "f123@chat.ca", 20, 3, 1967)));

       boys.Add(*(new Person("Frank", "frank@chat.ca", 25, 4, 1958)));

       girls.Addd(*(new Person("Mary", "mary@chat.ca", 25, 4, 1955)));

       boys.Add(*(new Person("John", "johnchat.ca", 12, 12, 1970)));

 //print all the boys using the removeSomeElement() method and delete them

       int numberOfBoys = boys.Size();

       cout << "number of boys = " << numberOfBoys << "\n";

       for(int i = 0; i<numberOfBoys;i++)

      { 

            Person & boy = boys.RemoveElement();

            boy.Print(); 

            delete &boy;

       }

//print the girls using the << operator of the SetOfPersons class

       cout << "number of girls = " << girls.size() << "\n";

       girls.Print();

//print of the girls birthdays and using the someElement() method

       int numberOfGirls = girls.Size();

       girls.Reset();

       for(int i = 0; i<numberOfGirls;i++)

      {

             girl.NextElement().GetBirthDate() .Print();

       } 

//delete all the girls from the heap

       int numberOfGirls = girls.Size();

      for(int i = 0; i<numberOfGirls;i++)

      {

            Person & her = girls.RemoveElement();

            delete &her;

      }

//end of main;

最后代码的结果要求为:

R4.2) 修改主程序以便它可以正确无误的演示扩展和缩小.

R4.3) 你应该􏰀供或修改主程序来测试所有要求 并且打印出执行结果.

R4.4) 你的代码不应该有任何的内存泄露或多次释放.

R4.5) 所有的堆对象应该在main函数返回之前释放.

R4.5) 你能在添加一个新元素到数组中之前来检查界限.

相关文章

网友评论

    本文标题:C++大作业一<<人员信息管理系统>>

    本文链接:https://www.haomeiwen.com/subject/mbkpsttx.html