You are on page 1of 4

CS217 Object Oriented Programming

Spring 2020 FAST-NU, Lahore


Assignment 2 – Operator Overloading
Create a class Set which represents a mathematical set. A set is a well-defined
collection of distinct objects. Here, we’ll work with sets of integers. The class will contain
number of elements in the set and a dynamic array of elements. There is no limit on the
size of a set – user can add unlimited elements in the set. {2,3,5,7,11} is an example of
a set with 5 elements.
Provide at least the following member functions:
• Default constructor
• Parameterized constructor
• Copy constructor
• Destructor
After providing the necessary member functions, overload the following operators:

1. =
set2 = set1 Copy all elements of set1 into set2. Don’t forget to implement
the self-assignment check.
set3 = set2 = set1 Copy all elements of set1 into set2 and set3

2. +
set3 = set1 + set2 Take union of set1 and set2 and store the result in set3
set2 = set1 + 4 Add all elements of set1 and an element 4 in set2

3. +=
set2 += set1 Take union of set1 and set2 and store the result in set2
set1 += 4 Add an element into set1. This function will be used to add
new elements into the set.

4. −
set3 = set1 - set2 Remove all elements of set2 from set1 and store the
remaining elements in set3
set2 = set1 - 5 Remove 5 (if exists) from set1 and store the remaining
elements in set2
5. −=
set2 -= set1 Remove all elements of set1 from set2
set1 -= 5 Remove 5 (if exists) from set1

6. *
set3 = set1 * set2 Multiply each element of set1 with every element of set2 and
store all the resulting elements in set3. Therefore, if size of
set 1 is 3 and that of set2 is 4, number of elements in set3
will be 12.
set2 = set1 * 3 Multiply every element of set1 with 3 and store the resulting
elements in set2

7. *=
set2 *= set1 Multiply each element of set1 with every element of set2 and
store all these elements in set2. Therefore, if size of set 1 is 3
and that of set2 is 4, number of elements in set2 will be 12.
set1 *= 3 Multiply every element of set1 with 3

8. ++ (Pre-increment)

++set1 Increment all elements of set1


set2 = ++set1 Increment all elements of set1. After increment, copy set1
into set2.

9. ++ (Post-increment)

set1++ Increment all elements of set1


set2 = set1++ Copy all elements of set1 into set2. After copying, increment
all elements of set1.

10. −− (Pre-decrement)

--set1 Decrement all elements of set1


set2 = --set1 Decrement all elements of set1. After decrement, copy set1
into set2.
11. −− (Post-decrement)
set1-- Decrement all elements of set1
set2 = set1-- Copy all elements of set1 into set2. After copying, decrement
all elements of set1.

12. ==
set1 == set2 Return true if two sets are equal

13. |=
set1 |= set2 Return true if two sets are equivalent

14. !=
set1 != set2 Return true if number of elements in both sets are different

15. <
set1 < set2 Return true if number of elements in set1 are less than the
number of elements in set2

16. <=
set1 <= set2 Return true if number of elements in set1 are less than or
equal to the number of elements in set2

17. >
set1 > set2 Return true if number of elements in set1 are greater than the
number of elements in set2

18. >=
set1 >= set2 Return true if number of elements in set1 are greater than or
equal to the number of elements in set2

19. &&
set1 && set2 Return true if both sets are non-empty i.e. return true if both
sets contain at least one element
20. | |
set1 || set2 Return true if at least one of the sets is non-empty i.e. return
true if at least one of the sets contain at least one element

21. !
!set1 Return true if set1 is empty

22. [ ]
set1[2] = 8 Replaces third element in the set with 8
set1[size] = 8 Adds 8 into the set

21. float conversion operator


float fVal = set1 Return the mean of all the elements of the set and store it in
fVal

22. int conversion operator


int iVal = set1 Return the median of all the elements of the set and store it
in iVal

23. << (Stream insertion operator)


cout << set1 Prints the whole set exactly in this format: { 2 , 3 , 5 , 7 , 11 }

24. >> (Stream extraction operator)


cin >> set1 First, take number of elements of the set as input from the
user. After that, take all elements of the set as input.

Important
• Just as a reminder, there is no repetition of elements in a set
• At every instant, the elements in the set are stored in sorted order
• There should be no dangling pointers and memory leaks in your program
• Make sure to do deep copy where necessary
• Make sure to handle all boundary cases e.g. user might try to run set1[2] = 8
but there is no index 2 in your set (your set has less than 3 elements)

You might also like