Uploaded by Ankit Upadhyay

Test01

advertisement
Test 01
In this test, we will work with all concepts covered so far, such as: class declarations, dynamic
memory allocation, constructors, destructors, operator overloading, deep copying, friend
functions, and stream operations. To achieve this, we will create a module containing the
declaration and definition of a class that will store, display, and save a list of favorite colours.
Instructions
For this test, you will work with the provided main.cpp source code. Note that this file should
not, under no circumstances, be changed. You need to create your module in such a way that it
works properly with the main function as it is.
Your module should be called colours. In it, you should declare a class called Colours containing
a number of member variables and member functions as follows:
•
•
•
•
•
•
•
•
1
A private integer to store the number of colours in the list (make sure to pick a meaningful
name for your variable).
A private pointer to an array of char of size 16 to store the names of the favorite colours
in the list1. This pointer will allow us to dynamically create an array of arrays (also called
a bidimensional array) where one of the dimensions has a fixed size of 16.
A public constructor that takes no arguments and does the following: it initializes the
number of colours at zero, and the pointer to the bidimensional array at with a nullptr.
A public member function called create_list that takes one argument of the type integer.
This function should create a list of favorite colours, with the number of colours
determined by its argument. This function should ask the user to enter the colours one
by one. This function should return true if it successfully allocated memory for the
bidimensional array, and false otherwise. Hint: This will require dynamic allocation of a
bidimensional array, where one dimension is fixed at 16, and the other is determined at
run time. You can use something such as: ptr_ = new char[size][16];
An overloaded public constructor that takes one argument of type integer. This
constructor should call the create_list function above to create a list of favorite colours
with the size specified by the provided argument.
A public destructor that deallocates any memory that was manually allocated for the list
of favorite colours.
A function called display_list that takes no arguments and return void. This function
should simply print the list of favorite colours.
An overloaded assignment operator (=). This overloaded operator should be able to
create a deep copy of one object of the class Colours into another object of the same
To create a pointer to an array of chars, one can use the syntax: char (*ptr)[size], where ptr stands for the name
of the pointer, and size stands for the size of the array. Afterwards, to access a specific colour in this bidimensional
array, one can simply use: ptr[i]. Using ptr[i][j] selects the jth character of the ith colour.
•
class. Hint: Your argument should be const, and passed by reference. Your return type
should be passed by reference too. Also, to use strcpy on Visual Studio, add the
preprocessor directive #pragma warning(disable:4996) to your course.cpp file.
A public member function called save that takes one argument of the type char [],
containing a file name, and save the colours contained in your bidimensional array into
the file. Make sure to close your file stream after saving the data. This function returns
void.
You should also create a function called print, and declare it as a friend function of your class
Colours. This function should take as an argument a const reference to an object of the type
Colours, and print the list of favorite colours. I.e., it acts like the display_list member function.
This function returns void.
This module should contain a header file, colours.h, containing declarations of functions and new
types (classes), and an implementation file, colours.cpp, containing definitions of functions.
Make sure to add preprocessor directives (such as #ifndef, #define, etc.) to ensure that there is
no risk of double inclusion of header files.
Evaluation
header file: 1.0 mark
constructors and destructors: 1.0 mark
create_list: 1.5 marks
assignment operator overloading: 1.5 marks
save function: 1.0 mark
print function: 1.0 mark
Make sure to upload all source code files on Blackboard. Remember to abide to the Seneca
academic honesty rules!
Download