Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Object Oriented Programming Lab Lab 06

O b j e c t O rLab
i Mid
e n t e d
Task01 P r o gr a m m i n g L a b
Lab 06 Marks 10
Instructions
Work on this lab individually. You can use your books, notes, handouts etc. but you are not allowed to borrow anything from your
peer student.
Marking Criteria
Show your work to the instructor before leaving the lab to get some or full credit.
What you have to do
Program the following tasks in your Microsoft C++ compiler and then compile and execute them. The name of your files will be
according to the task given in this lab.

ADT: Collection

Create a class named Collection for which each object can hold integers.
The class should have following two private data members
1. An integer pointer named data that holds a reference of an array created dynamically according to the specified size.
2. A constant integer named size to hold the maximum size of the array.

Provide the implementation of following constructors and a destructor


1. A constructor which accepts an integer that represents the size of an array and initializes it to the "empty collection,"
i.e., a collection whose array representation contains all 0.
2. An additional constructor that receives an array of integers and the size of that array and uses the array to initialize a
collection object.
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.

Provide following member functions for the common set operations


1. getSize returns the size of collection.
2. setElement that inserts a new integer k at index i ( both passed as argument) into a collection, if possible.
3. countElement that counts and return the total occurrences of an integer key (passed as argument) in a collection, -1
otherwise.
Overload following operators
1. Assignment (=) which copies the data of one object to another. The copy should only be done if both the objects have
same sizes also avoid self-assignment.
2. Stream extraction to take input from user for the ‘data’ of a collection.
3. Stream insertion to display the contents of ‘data’ on the screen of a collection.
4. Addition (+) binary operator which perform the addition of two collections if possible and return the result.
5. Arithmetic assignment operator (–=) which subtract the right hand side collection from left if possible and assign the
result back to left hand side collection.
6. Minus (–) unary operator which return true if all the elements of a collection are negative, false otherwise.
7. Pre increment (++) and post decrement (--) to increment a collection by 1 and to decrement it by 1 respectively.
8. Not Equal (!=) operator which returns true if both the collections contain same data, false otherwise.
9. Subscript operator ([]) to check the bounds of a collection for both Lvalue and Rvalue.
10. Function operator (()) receives two parameters start_index and end_index and return the sub collection based on these
indices both inclusive.
Test the functionality of your created class by creating some of its objects. Also make calls to all the member functions of the class
to test their functionality.

☺ ☺ ☺ BEST OF LUCK ☺ ☺ ☺
Umair Babar, PUCIT – PU. Lahore. Page 1 of 1

You might also like