language: C++
all the instructions provided below.
Objectives Upon completion of this assignment, you will to be able to: - Be familiar with a simple user-defined container abstract data type. - Implement an array-based container. - Continue to practice modularity in programming. - Recognize some of the information-hiding issues and techniques to resolve these issues. - Continue to create classes in C++, noting similarities n this class and the class from the first assignment. - Review C++ arrays, pointers and references. - Review dynamic memory allocation. - Experience separation of declaration and definition files. The expected completion time for this assignment should be between 2 to 6 hours. Introduction In this assignment, we will be implementing an array as a behind-the-scenes structure of a basic container. We call this container a Group because its name does not imply what the container will necessarily look like. In fact, an abstract data type is defined by a client's point of view, the implementation design being left to the programmer. Our abstract definition of a Group of Students is that it all of the following must be true. - It is a container that holds Student objects as its elements in no particular order. - It has unlimited capacity. - It knows how many students are in the group and can tell if it is empty of all elements. - Student objects can be added to the group. - Student objects can be removed from the group. The reason for implementing it with an array is because at this time, that is the only data structure we know. Of course, not beyond the capacities of the computer that it is running on. Before starting to code the implementation, expert programmers often set up tbe minimum Once all this is done, compile the single filk, using the following statement: epp file that compilest, Make a copy of the Group ahpp and sall it Group. Spp. Open the cpp version in a text editor to make a few changes: (1) Remove the sifndef, edefine and eendif satemens from the lop and bouom of the file. (2) Remove the provious aiscloses and using statements and add the following oinclude statement exactly as is: if you are not. If there are no coenplaints from the compiler, then you are ready to start implementing the program. in one of the slandlard libriry locations. Note alw that the Group, hpp file has a few of Detailed Instructions its own finctude siatersents. The Group. Cpp inhtritr these Ainetves, and because one of them is the Student, hpp file, it inherits the Ainctudes from this file as well. So, in short, the siedle tinetude Matement is sefficient in the Group. cpp file. (3) Remove the key word class and the open and closing curly bracee that define it. Also The documentation file: Group, htet file, Gau doable-click on in, cotains all the docuremove any private of public accessibility key words as well. Et is recommenbed that you adjust the indenting aller doing this. (4) Remove all the member variatle definitions, iaduding the static convant called mentation that would be provided to a user programmer for the Greop class. This file is auCAPACITY, Aloo remove "- CAPACITY" as the defaull value of the iaput purameter. (5) For each of the member function sames abd the prefic "Group a s". The functicen name comes after the return type (excepa for constructon and detructors, where there is no rebirn type). Note that operatorn is not a member function, se do not add this grefix. Inurad, remove the key-werd; friend as that is declared in the hop venion. (6) Remave each of the semicolons from the functives and roplace them with open and tomatically geberated from documentation commeats that were plased into the Group . how file. See the Cosding Style Coteretione document (located in VIULeam->161 > Supplemental Information) for details about propor professional commenting. closed curly brokkets. (7) For each of the functions that have a netum bye, place a "return esosethingp" When implementing each of the fanstions in the Grow. Cpo proving, you must make wore that each oee behwere eactly as dewribed in the documentaaion file. stalement where esonething is replacod with: - 0 , if the return type is an integer. - false if the return type is a bootean valoe. - the variable called out foe the ogeratore function. the claw Note that the inuanee variables and the statie const variable are defled in the hpp file and do not need wo be redefined in the cop file. They are available to every function as if they were defitad in the same tile. conatructorn, destructer, copy constructer The single constructor serves as boch the default (no inpul parami) and regular (cupacity decibed by the user) versions, ta either case, somse memary seeds wo be allocated for the initial array. Note that the array's capacity is the number of spots wailatle for Student objects. The namber of elemens mus never exceed the capacity. When the coestructor to complesed, the list atribse is an array of size: INIT CAPACITY and no elements. The copy constroctor copies all the attribstes over froen the input parameter. However, a direct copy of the tist attribute will only copy the peinter, not the whole array. A new 11st seeds to be dyaamically created, and each of the elements from the input parameter copy is then copied to the aew object's array. Note that if the copy eonstructor is sot Preparation (d) Adjust the oinctude and using stasemens near the wop. The only necessary in- (1) Dosnnoad this paf file and atore it on your computer. formation appears to be: (2) Create a directory f folder of your computer specific to this abignenent, for example, cscr161/assn2 is a good aame. (3) Copyit the fimished and working version of Assignment 1's student, cpp to this dintctary. (4) Downlosd and store the Group.hpp file. DO NOT AL.TER (5) Downlood and stoec the Growp abtel and Student , htat files. DO NOT Al.TZR (6) Create and atore a nain. cpp file. Add atinclude "Giroup .hpp" to the regular libeiry inclusions. The file main. cpp contains a single main function that is specific for testing. Use main during development to test cach bit of code. It cannot be stressed enough that testing be done regularly after a sasall portion of Group code has been writien. (T) OPTLONAL. Dowiload and store nskefle. If you are using a linux based operating system, of doing your work on a lab machine, this file will make it easier to perform repeated separatices and updates. You are welcome to modify nakef the to suit your needs. Setting wp the files Alter the copy of Student.cpp from Asalgnment I, inte a header file The claas definition for the 5 tudent class has been teaed in assignment I and needs no further lestieg, so we can convent the class definitisa ifto a Ci+ houder fle, with woene ainc lude asstrean. wsing stditcout: f/ usetul for assessing code wing std: wing stdrastring: ff lots at otrangs using stditastreas /f far the operatorwe function wieg std: asteringstreati: if if to striag in inpletented minor modifications: (e) Create a sepueate defall constructoc. The reawe for this has to do with the fact that objects are never jus drcharnd, but also insantiated. If we crease an array of 1000 spots for Student objeets, then the default constructer will be called 1000 times, and for thene of us who used default values in a single constructor, 1000 unique ads will be ereated as well. (What a waste of perfectly good bd sumbers!) So let's ttake a spectal default constructor, that does nothing, i.e. (1) Caange the name of the file from Student, cp10 Student, hop. (2) Open the tile in a test cdiber to make the fothowing alterations: (a) Remove the aash function and all of its coild. (b) Add the follereing statenients so the top of the file: which does ahoolunely sothing, not even generate an id mumber. We can always remone the cout seatement after everything is fully tested. Foe now, it is belpcul wo sott out what is happening ence we sart coding the program and tracking what is happening when the program is fuet. (5) Update the exisding consinactor to allaw for the one or iwo input paramietern. In the newer verviun of this constructar a last name must be supplied, and a firut may be missing, in which case it is given the defauly valos of "unkhous", (e) Leave the desiructor, with its single odepat mesago. You may want to mention that it is the student deanuktor that is being called io differendiate is from the Greup destructar, - Lifadel stuecur. (h) Muas Student, hpp trough the sornpiler io check for afy syniat errors before Adetine stuecst. in procecding. (c) Add the following statement to the botiom of the file: Cruate a Group. cho tile Thas tile wall sontain the implementation for the Greup class. Note than the Greup. hep fle is the hrader ille asd does not cossain the inline implementation that 5 tudent, hpp dees. Since the two files (header and implenenation) are related, it is important that we make cxact cepies of the function nantes when we inglement the function definiains. coded that way, thes the implisit copy constructor just copbes the poimer and the new inserting and removing elements Group object has to share the array with the cepy. Similarly so the ceat check example, the eser secds so be able to add an clement and The destructor needs to release the memory that was reserved for the list array. Note that the constructor uses the keywond: new to dyaanically allocate the new array. In the remore an element. destructor, there must be a corresponding delete where the measory that was allocated insert zeeds bo be set free. Inserting an elemear iato the Group object means requires that the element stays there ancil Suppose, the constructir contains the statement: it is removed, of the program ends. How it is slored in the 1ist atribute is up to you as List = nex Student teapacity]; the programmer. An important eonsideratios is that the array has enough room to secte the clement. If it does not, then the prograntmer has io create a new array which is double the size of the original, and make that the new list attribute. Then old version of the array then the sestructor must concain the statement and its memory must be released. seletell tist; This is best accomplished in a private heiper function. Because the insert function changes the state of the Group object, the programmer must make sure that everything about the Group is stable'. The ahove ecmmand delees the array(which a pointer) and all of the allocated memory set up for the individual index locations in that array. Each of the elements cootained within the array must be deleted as well. However, because this code makes proper use of modularity, each elamen's deletion is taken care of automatically within the Student class destructor. The Group object is assumed to contain unique 5 tudent objects, so a particular stodent setters and setters can be identified by id and removed if they are in the group. We should not have a geter function called get. Iist. The attribute tist is an annay; obce we return a pointer to it, the user can do whatever they wart to alker the array elements. THIS ACTOON DEIFAIS the whole purpose of security of cur data structure, so the list helper functions attribute should sot be avallable to anyone (except the designer). Helper functions are used stristly by the programmer to assist in keeping code organized The mumber of elements may be useful to the user, so that can be provided. I see no reavon why the user would need to be concerned with the actual capacity. and to help the public function implementation. These functions are not visible to the user. therefore are not inverted into the docuinenation of the hpp file. The functios that dobbles the size of an array is a belper function. You are weicoene 00 add whatever other belper functioes you fied helpful. The user may be interested if there ace any elements in the list at IL. The is espty function testing makes sense, and is really easy to implement. The sain. cpp file cootains a single sain function where everything is tested. Becaose Since the array is not available direetly, it is helpful to allow a printout of all the elements in this class definition is relatively simple, you are expected to test every meember function: the group. Overooding the s operator works well for both the user and the developer. some of which may have more than one coesition. the imserim intiast bodore meppleting. //Group.hpp \#ifndef GROUP_H \#def ine GROUP_H \#include \#include "Student:hpp" using std: :ostream; class Group \{ private: Student * List; int num_elements; int capacity; public: static const int INIT_CAPACITY =3 Group(int size = INIT_CAPACITY); Group(const Group \©); int get_num_elements() const; bool is_empty(); void insert(const Student &s); bool remove(Student \& s); friend ostream \& operator ( ostream & out, const Group \& dis); \#end tf /*t Group. html Public Member Functions: Group (int size=INIT_CAPACITY) Group (const Group \©) a group () int get_num_elements () const bool is empty () void insert (const Student &s ) bool remove (Student \&s) Static Public Attributes static const int INIT_CAPACITY =3 Friends: ostream \& operatork (ostream \&out, const Group \&group) Objectives Upon completion of this assignment, you will to be able to: - Be familiar with a simple user-defined container abstract data type. - Implement an array-based container. - Continue to practice modularity in programming. - Recognize some of the information-hiding issues and techniques to resolve these issues. - Continue to create classes in C++, noting similarities n this class and the class from the first assignment. - Review C++ arrays, pointers and references. - Review dynamic memory allocation. - Experience separation of declaration and definition files. The expected completion time for this assignment should be between 2 to 6 hours. Introduction In this assignment, we will be implementing an array as a behind-the-scenes structure of a basic container. We call this container a Group because its name does not imply what the container will necessarily look like. In fact, an abstract data type is defined by a client's point of view, the implementation design being left to the programmer. Our abstract definition of a Group of Students is that it all of the following must be true. - It is a container that holds Student objects as its elements in no particular order. - It has unlimited capacity. - It knows how many students are in the group and can tell if it is empty of all elements. - Student objects can be added to the group. - Student objects can be removed from the group. The reason for implementing it with an array is because at this time, that is the only data structure we know. Of course, not beyond the capacities of the computer that it is running on. Before starting to code the implementation, expert programmers often set up tbe minimum Once all this is done, compile the single filk, using the following statement: epp file that compilest, Make a copy of the Group ahpp and sall it Group. Spp. Open the cpp version in a text editor to make a few changes: (1) Remove the sifndef, edefine and eendif satemens from the lop and bouom of the file. (2) Remove the provious aiscloses and using statements and add the following oinclude statement exactly as is: if you are not. If there are no coenplaints from the compiler, then you are ready to start implementing the program. in one of the slandlard libriry locations. Note alw that the Group, hpp file has a few of Detailed Instructions its own finctude siatersents. The Group. Cpp inhtritr these Ainetves, and because one of them is the Student, hpp file, it inherits the Ainctudes from this file as well. So, in short, the siedle tinetude Matement is sefficient in the Group. cpp file. (3) Remove the key word class and the open and closing curly bracee that define it. Also The documentation file: Group, htet file, Gau doable-click on in, cotains all the docuremove any private of public accessibility key words as well. Et is recommenbed that you adjust the indenting aller doing this. (4) Remove all the member variatle definitions, iaduding the static convant called mentation that would be provided to a user programmer for the Greop class. This file is auCAPACITY, Aloo remove "- CAPACITY" as the defaull value of the iaput purameter. (5) For each of the member function sames abd the prefic "Group a s". The functicen name comes after the return type (excepa for constructon and detructors, where there is no rebirn type). Note that operatorn is not a member function, se do not add this grefix. Inurad, remove the key-werd; friend as that is declared in the hop venion. (6) Remave each of the semicolons from the functives and roplace them with open and tomatically geberated from documentation commeats that were plased into the Group . how file. See the Cosding Style Coteretione document (located in VIULeam->161 > Supplemental Information) for details about propor professional commenting. closed curly brokkets. (7) For each of the functions that have a netum bye, place a "return esosethingp" When implementing each of the fanstions in the Grow. Cpo proving, you must make wore that each oee behwere eactly as dewribed in the documentaaion file. stalement where esonething is replacod with: - 0 , if the return type is an integer. - false if the return type is a bootean valoe. - the variable called out foe the ogeratore function. the claw Note that the inuanee variables and the statie const variable are defled in the hpp file and do not need wo be redefined in the cop file. They are available to every function as if they were defitad in the same tile. conatructorn, destructer, copy constructer The single constructor serves as boch the default (no inpul parami) and regular (cupacity decibed by the user) versions, ta either case, somse memary seeds wo be allocated for the initial array. Note that the array's capacity is the number of spots wailatle for Student objects. The namber of elemens mus never exceed the capacity. When the coestructor to complesed, the list atribse is an array of size: INIT CAPACITY and no elements. The copy constroctor copies all the attribstes over froen the input parameter. However, a direct copy of the tist attribute will only copy the peinter, not the whole array. A new 11st seeds to be dyaamically created, and each of the elements from the input parameter copy is then copied to the aew object's array. Note that if the copy eonstructor is sot Preparation (d) Adjust the oinctude and using stasemens near the wop. The only necessary in- (1) Dosnnoad this paf file and atore it on your computer. formation appears to be: (2) Create a directory f folder of your computer specific to this abignenent, for example, cscr161/assn2 is a good aame. (3) Copyit the fimished and working version of Assignment 1's student, cpp to this dintctary. (4) Downlosd and store the Group.hpp file. DO NOT AL.TER (5) Downlood and stoec the Growp abtel and Student , htat files. DO NOT Al.TZR (6) Create and atore a nain. cpp file. Add atinclude "Giroup .hpp" to the regular libeiry inclusions. The file main. cpp contains a single main function that is specific for testing. Use main during development to test cach bit of code. It cannot be stressed enough that testing be done regularly after a sasall portion of Group code has been writien. (T) OPTLONAL. Dowiload and store nskefle. If you are using a linux based operating system, of doing your work on a lab machine, this file will make it easier to perform repeated separatices and updates. You are welcome to modify nakef the to suit your needs. Setting wp the files Alter the copy of Student.cpp from Asalgnment I, inte a header file The claas definition for the 5 tudent class has been teaed in assignment I and needs no further lestieg, so we can convent the class definitisa ifto a Ci+ houder fle, with woene ainc lude asstrean. wsing stditcout: f/ usetul for assessing code wing std: wing stdrastring: ff lots at otrangs using stditastreas /f far the operatorwe function wieg std: asteringstreati: if if to striag in inpletented minor modifications: (e) Create a sepueate defall constructoc. The reawe for this has to do with the fact that objects are never jus drcharnd, but also insantiated. If we crease an array of 1000 spots for Student objeets, then the default constructer will be called 1000 times, and for thene of us who used default values in a single constructor, 1000 unique ads will be ereated as well. (What a waste of perfectly good bd sumbers!) So let's ttake a spectal default constructor, that does nothing, i.e. (1) Caange the name of the file from Student, cp10 Student, hop. (2) Open the tile in a test cdiber to make the fothowing alterations: (a) Remove the aash function and all of its coild. (b) Add the follereing statenients so the top of the file: which does ahoolunely sothing, not even generate an id mumber. We can always remone the cout seatement after everything is fully tested. Foe now, it is belpcul wo sott out what is happening ence we sart coding the program and tracking what is happening when the program is fuet. (5) Update the exisding consinactor to allaw for the one or iwo input paramietern. In the newer verviun of this constructar a last name must be supplied, and a firut may be missing, in which case it is given the defauly valos of "unkhous", (e) Leave the desiructor, with its single odepat mesago. You may want to mention that it is the student deanuktor that is being called io differendiate is from the Greup destructar, - Lifadel stuecur. (h) Muas Student, hpp trough the sornpiler io check for afy syniat errors before Adetine stuecst. in procecding. (c) Add the following statement to the botiom of the file: Cruate a Group. cho tile Thas tile wall sontain the implementation for the Greup class. Note than the Greup. hep fle is the hrader ille asd does not cossain the inline implementation that 5 tudent, hpp dees. Since the two files (header and implenenation) are related, it is important that we make cxact cepies of the function nantes when we inglement the function definiains. coded that way, thes the implisit copy constructor just copbes the poimer and the new inserting and removing elements Group object has to share the array with the cepy. Similarly so the ceat check example, the eser secds so be able to add an clement and The destructor needs to release the memory that was reserved for the list array. Note that the constructor uses the keywond: new to dyaanically allocate the new array. In the remore an element. destructor, there must be a corresponding delete where the measory that was allocated insert zeeds bo be set free. Inserting an elemear iato the Group object means requires that the element stays there ancil Suppose, the constructir contains the statement: it is removed, of the program ends. How it is slored in the 1ist atribute is up to you as List = nex Student teapacity]; the programmer. An important eonsideratios is that the array has enough room to secte the clement. If it does not, then the prograntmer has io create a new array which is double the size of the original, and make that the new list attribute. Then old version of the array then the sestructor must concain the statement and its memory must be released. seletell tist; This is best accomplished in a private heiper function. Because the insert function changes the state of the Group object, the programmer must make sure that everything about the Group is stable'. The ahove ecmmand delees the array(which a pointer) and all of the allocated memory set up for the individual index locations in that array. Each of the elements cootained within the array must be deleted as well. However, because this code makes proper use of modularity, each elamen's deletion is taken care of automatically within the Student class destructor. The Group object is assumed to contain unique 5 tudent objects, so a particular stodent setters and setters can be identified by id and removed if they are in the group. We should not have a geter function called get. Iist. The attribute tist is an annay; obce we return a pointer to it, the user can do whatever they wart to alker the array elements. THIS ACTOON DEIFAIS the whole purpose of security of cur data structure, so the list helper functions attribute should sot be avallable to anyone (except the designer). Helper functions are used stristly by the programmer to assist in keeping code organized The mumber of elements may be useful to the user, so that can be provided. I see no reavon why the user would need to be concerned with the actual capacity. and to help the public function implementation. These functions are not visible to the user. therefore are not inverted into the docuinenation of the hpp file. The functios that dobbles the size of an array is a belper function. You are weicoene 00 add whatever other belper functioes you fied helpful. The user may be interested if there ace any elements in the list at IL. The is espty function testing makes sense, and is really easy to implement. The sain. cpp file cootains a single sain function where everything is tested. Becaose Since the array is not available direetly, it is helpful to allow a printout of all the elements in this class definition is relatively simple, you are expected to test every meember function: the group. Overooding the s operator works well for both the user and the developer. some of which may have more than one coesition. the imserim intiast bodore meppleting. //Group.hpp \#ifndef GROUP_H \#def ine GROUP_H \#include \#include "Student:hpp" using std: :ostream; class Group \{ private: Student * List; int num_elements; int capacity; public: static const int INIT_CAPACITY =3 Group(int size = INIT_CAPACITY); Group(const Group \©); int get_num_elements() const; bool is_empty(); void insert(const Student &s); bool remove(Student \& s); friend ostream \& operator ( ostream & out, const Group \& dis); \#end tf /*t Group. html Public Member Functions: Group (int size=INIT_CAPACITY) Group (const Group \©) a group () int get_num_elements () const bool is empty () void insert (const Student &s ) bool remove (Student \&s) Static Public Attributes static const int INIT_CAPACITY =3 Friends: ostream \& operatork (ostream \&out, const Group \&group)