2019 SP Final
2019 SP Final
At the end of the game, the players’ bags may contain the pointers as follows (the actual situation will depend
on the numbers that turn up on each dice):
Diamond Gold
Gemstone Neckless
Silver
Coin
Diamond
players[1].bag
Ruby Gold Silver Ring
Gemstone Brik Pendent
players[2].bag
You job is only to write a destructor for the class TreasureHunt to deallocate all the allocated memory
of the game. Make sure that no object is deleted more than once and there are no memory leaks. You can
add more code utility methods to the class TreasureHunt to divide the work among functions.
In this question you will need to use inheritance, polymorphism, friend class and down casting.
You have designed a new music player application. Let’s call it OOPMP, short for OOP Music Player.
OOPMP will have a playlist in which songs can be added or removed.
Each song will have lyrics and name. As we cannot keep audio for lyrics, let’s assume that the lyrics are in
the form of a char*.
Songs will have further types as follow:
1. MP3: this will be an audio song, only containing lyrics, each MP3 song will also have a type (for
example pop, rock, classical etc.)
2. MP4: this will be a video song, they will have lyrics as well as video (again, as we cannot keep video
we will assume it to be a char* containing a description of the scenes.)
3. sMP4: short for secret MP4, this type of song will also have lyrics and video. It is called secret MP4
because only OOP music players can play their real lyrics and video. If anyone else tries to play it,
they will only hear bleep as lyrics and see glitches as videos.
Design and code your classes keeping in mind the above mentioned instructions and the following main
function and its desired output (on next page).
Size = 0
Size = 50000
};
int main() {
strwrap arr[3]={"What will", "be printed", "on the screen?"};
arr[0].whatprint1();
}
(c) Consider the following function template sort that takes an array of elements as input, sorts and prints it.
main1 function gives an example of how it is used for integer array and its output. Identify if this function
will work for an array of char* type, if not write a code such that the main2 function works and produces
desired output as shown below, i.e. sorts the strings in given array in ascending order of their length and prints
them.
void sort(T* arr, int n) //main1 output
{ void main () 89
int i, j; { 435
for (i = 0; i < n; ++i) int arr[5]={89,10154,687,435,8054}; 687
{ sort(arr, 5); 8054
for (j = 0; j < n-i-1; ++j) 10154
{ }
/*Comparing consecutive data
switching values if value
//main2 Desired
at j > j+1*/
void main2() output
if (arr[j] > arr[j+1]) {
{ xa
T temp= arr[j]; abc
char **cArray= new char*[3]; mnop
arr[j]= arr[j+1]; cArray[0]= new char[3];
arr[j+1]= temp; strcpy(cArray[0],"xa");
}
cArray[1]= new char[4];
}
strcpy(cArray[1],"abc");
}
cArray[2]= new char[5];
for (int i=0; i<n; i++) strcpy(cArray[2],"mnop");
cout<<arr[i]<<endl;
sort(cArray, 3);
}
}