您好,欢迎光临本网站![请登录][注册会员]  
文件名称: C++第一次大作业
  所属分类: C/C++
  开发工具:
  文件大小: 2mb
  下载次数: 0
  上传时间: 2014-07-13
  提 供 者: wyb_4*****
 详细说明: 第一次作业 这个作业将让你去练习建立一些简单的类和使用C++的基本功能,包括:封装,引用,动态内存开辟, 简单构造函数和析构函数和const。 下面给的程序片段未经过编译或调试. 做出合理的错误修正是你任务的一部分。 一般的,我们会给你最基本部分的代码,如果你需要,你可以在一个类里添加额外的变量或方法,这个作业被分为三个部分,那么你们要按照步骤一步一步地完成。 1. 设计要求 第一部分) 构建简单的类 R1.1) 创建一个Person类, 其模型在下面的代码结构里。 R1.2) 人类(Persons)应该有一些属性: name, email_address, birthdate 作为表示人类的属性。 R1.3) 按下面的要求创建一个Date类。 R1.4) 每个类都应该可以使用输出运算符(<<)输出内容。 //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 //file Person.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 #include #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
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: Date Person
 输入关键字,在本站1000多万海量源码库中尽情搜索: