Design and code a class

  

Problem

Design and code a class that will be used to encapsulate a collection of “segment” objects.

Write the code for a class called a “disk” which may contain any number of segment objects, and a string (maximum of 2 characters) indicating the mode (i.e. “w” (write), or “a” (append)) that this disk may be accessed with.

You must modify the attached “segment” class to include the following constructors, each calling their appropriate initialize( ) function(from the attached segment files):

segment( )

segment(const char [ ][2000], int)

As well, you must change the following functions:

int match(const char [ ]);

voidget_word(char [ ], int);

charget_char(int, int);

which were not declared as const functions previously, into constant member functions, by adding the keyword const after their declaration in both the “segment.h” and “segment.cpp” source files.

A “disk” has the following publicly accessible MEMBER functions:

disk(intnum_of_segments, const char *mode)

This constructor allocates enough memory for the disk to storea series of up to “num_of_segments” segments, and sets thedisk’s access mode to the constant string stored in “mode”.

If the string in “mode” is anything other than “w”, or “a”,the disk’s mode must be set to “w” (write).

disk( )

This constructor allocates enough memory for the disk to store

up to twenty (20) segments, and initializes the disk’s access

mode to “w” (write).

const char* get_mode( ) const

This function returns the disk’s access mode as a constant string.

segmentget_segment(intpos) const

This function returns the segment at position “pos”

(where a “pos” value of 0 means the first segment).

The function returns a default segment if “pos” does not reference

one of the disk’s properly initialized segments.

intget_segment_count( ) const

This is used to return an integer value representing the number of

segment objects that have been properly initialized.

const segment* get_all_segments( ) const

This function returns the array containing all of the disk’s

data segments.

int access(const char fname[ ])

This function will either write “w”, or append “a” data

(depending on the access mode) to the file whose name is

stored in “fname”, and return the number of charcters processed

(please see below for details about each access mode).

write:

For this operation, the function must write all of the data in eachof the class’s segments (one segment per line) followed by a newline.

Also, each word in the segment must be separated by a space ‘ ‘ when written to the file spaces and newlines each count as a single (1) character when written).

NOTE: This operation overwrites any data which the file may have

contained.

(see example below).

Assume a “disk” object consists of the following 3 segments:

words: 1 2 3 4 5 6 7 8 9 10

segment1: “”, “” “This”, “is”, “OOP244”, “”, “”, “”, “”, “”

segment2: “Isn’t”, “this”, “”,  “”, “easy?”, “”, “”, “”, “”, “”

segment3: “”, “”, “”, “Yes!”,””, “”, “”, “”, “”, “”

Then the function would write the following to the file:

This is OOP244

Isn’t this easy?

Yes!

and return a value of: 37

15 for (line 1) +

17 for (line 2) +

5 for (line 3)

for a total of 37 charcters written to the file.

BE CAREFUL HERE!

Pay careful attention to the number of spaces and newlines written with respect to each segment!

append:

Exactly the same as “write”, except that data is appended to the end of the file instead of being overwritten.

operator+= 

You must also be able to add segments to a disk by overloading the

+= operator which adds segments (on the right side of the operator)to the disk (on the left side of the operator), unless the disk is already full in which case nothing is done.

NOTE: This operator must return the newly modified “disk” object by value.

NOTE: Because “proper copies” of “disk” objects must be made, it will be necessary for you to include a copy constructor as well an equal operator overload to handle situations when “disk” objects are returned by value or when they are assigned to one another.

You must also code a destructor to remove any memory which may have been dynamically allocated.

A main program (a3main.cpp), which will assume that your “disk” class is declared in a file named “disk.h” and should be compiled and linked with the code for the “disk” (which presumably will be in “disk.cpp”), will test your class to see if it works properly.

Note: The file assign1.c (which searches through the constant null-terminated string “core” looking for the characters ‘1’ and ‘0’ which, when taken 8 at a time and convert to their numeric value in decimal, encode a meaningful ascii character, and inserts that character into the array “data”) attached will need to be executed as part of the solution.

a3main.cpp and assign1.c must not be modified or the solution will be invalidated.

"Get 15% discount on your first 3 orders with us"
Use the following coupon
"FIRST15"

Order Now