Monday, 25 December 2017
ADT: Generic Collection
Write a generic class named Collection for which each object can hold elements of any valid data type and zero as default value.
1. The class should have following two private data members.
1. A pointer named data that holds an array of any valid data type allocated dynamically according to the specified size.
2. An integer named size that holds the size of the array (amount of memory allocated to data).
2. Provide the implementation of following constructors and a destructor
1. A default constructor which creates an array of five elements and initializes it to the so-called "empty collection," i.e., a
collection whose array representation contains all zeroes.
2. A constructor which accepts an integer as argument to represent the size of an array and initializes it to the so-called
"empty collection," i.e., a collection whose array representation contains all zeroes.
3. A copy constructor to initialize a collection object with already existing object.
4. A destructor to free any memory resources occupied by the collection object.
3. Provide following member functions for the common operations
1. getSize returns the size of collection.
2. setElement that inserts a new element k at index i (both passed as argument) into a collection, if possible, otherwise give
an appropriate error message.
3. findElement accepts an element key as argument and return true, if the key element exist in the collection, false otherwise
if the key does not exist.
4. countElement accepts an element key as argument and count and return the total occurrences of it in a collection, -1
otherwise, if the key does not exist.
4. Provide the implementation of following overloaded operators
1. Assignment (=) which copies the data of one object to another. The assignment should be done even if the sizes of both
objects are not same and avoid self-assignment.
2. Stream insertion (<<) to display the contents of data on the screen of a collection.
3. Stream extraction (>>) to take input from user for the data of a collection.
4. Arithmetic assignment (+) binary which perform the addition of two collections (left hand side and right hand side) if
possible and return the result.
5. Comparison (==) that determines whether two collections are equal or not. The operator should returns true if both the
collections are equal, false otherwise.
6. Subscript ([]) for both lvalue and rvalue of non-const objects
7. Subscript ([]) for rvalue of const objects
8. Unary minus (-) return true if all the elements of a collection are non-zeroes, false otherwise.
9. Unary Not (!) assigns zero to all the elements of the object i.e. convert the collection into its “empty” form.
10. Function (()) receives two parameters as argument start_index and end_index and return the new sub collection which
contains all the values exist in the left hand side object from start_index to end_index both inclusive, if possible.
5. Once you have written the class, write main function and test its functionality by creating some objects of Collection for integer,
float and char data types.
Link of code...
https://www.dropbox.com/s/krvjdos8plynwy5/ADT_GenericCollection.cpp?dl=0
Subscribe to:
Post Comments (Atom)
ADT: RationalNumber
Define a class for rational numbers. A rational number is “ratio-nal” number, composed of two integers with division indicated. The division...
-
Design a class called NumDays. The class’s purpose is to store a value that represents a number of work hours and convert it to a number of ...
-
Define a class for rational numbers. A rational number is “ratio-nal” number, composed of two integers with division indicated. The division...
-
LCM is most early technique that is taught in school. it helps us in Calculation of Fractions. here I have written code for finding LCM of ...
No comments:
Post a Comment