OOP Final Fall-2023
OOP Final Fall-2023
You are designing a library management system that needs to efficiently allocate books based on their genres and
availability. The library receives a static 2D array representing the number of books available for different genres in
each section. Your task is to create a dynamic 2D array that organizes these books according to the following
conditions:
Each row in the static array represents a section of the library.
Each column in a row represents a genre of books available in that section.
The value at staticArray[section][genre] denotes the number of books available for that particular genre in
that section.
Your program needs to perform the following tasks:
Identify the sections where the total number of books available across all genres is greater than a given
threshold (thresholdBooks).
Create a Dynamic array for these identified sections, where each row corresponds to a section having more
books than the threshold.
Allocate memory dynamically for these sections and genres and populate the Dynamic array accordingly.
Ensure that memory is deallocated appropriately after use.
Write a C++ program that takes the static 2D array as input, applies the conditions mentioned above, and outputs
the 2D Dynamic array.
int main() {
int staticArray[4][3] = {
{10, 5, 7},
{3, 1, 8},
{6, 4, 9},
{2, 5, 3}
};
int dynamicArrayRows = 0;
int sections = 4;
int genres = 3;
int thresholdBooks = 15;
// Creating the dynamic array
int** dynamicArray = createDynamicArray(staticArray, sections, genres, thresholdBooks,
dynamicArrayRows);
// Populating the dynamic array
populateDynamicArray(staticArray, dynamicArray, sections, genres, thresholdBooks);
// Displaying the result
cout << "Dynamic Array for Sections with More than " << thresholdBooks << " Books:\n";
for (int i = 0; i < dynamicArrayRows; ++i) {
for (int j = 0; j < genres; ++j) {
cout << dynamicArray[i][j] << " ";
}
cout <<endl;
}
// Deallocating memory
deallocateDynamicArray(dynamicArray, dynamicArrayRows);
return 0;
}
Implement the following UML diagram using concept of abstract class and polymorphism without changing main
function.
Note: you can typecast base class object to derived class object in compare function
e.g; Image* imageObj = ( Image*)other;
if (*multimediaArray[i] == audio) {
cout << "The content is equal to the audio." << endl;
}
else {
cout << "The content is not equal to the audio." << endl;
}
cout << "------------------------" << endl;
}
return 0;
}
1. Read the following code, A double pointer array is declared named person.
Person** persons = new Person*[5];
Complete the code to resize the array to add 7 people to this array
class Person {
public:
string name;
int age;
Person(string n, int a) : name(n), age(a) {}
};
int main() {
int capacity = 5;
// Creating an array of 5 pointers to Person objects
Person** persons = new Person * [capacity];
// Adding 5 people
for (int i = 0; i < capacity; ++i) {
persons[i] = new Person("Person", 25 + i);
//Write your code for resizing array here, your code should run perfect with the already
written lines of code:
4.Find the error in this code and explain the reason: 5.Write output of the following code:
class Base { class Base {
public: public:
Base(string& baseData) : Base() {
baseData(baseData) {} cout << "Base Constr" << endl;
virtual void displayInfo() const { }
cout << "Base Class: " << baseData; ~Base() {
} cout << "Base Destr" << endl;
private: }
string baseData; };
}; class Derived1 : public Base {
class Derived : public Base { public:
public: Derived1() {
Derived(string baseData, string cout << "Derived1 Constr" << endl;
derivedData) }
: Base(baseData), derivedData(derivedData) {} ~Derived1() {
virtual void display() const { cout << "Derived1 Destr" << endl;
cout << "Derived Class: " << }
derivedData; };
} int main() {
private: Base* Basepointer = new Derived1;
string derivedData; {
}; Derived1 obj;
int main() { delete Basepointer;
Derived derivedObject("Base Data", }
"Derived Data"); }
Base base = derivedObject; Output:
base.display();
}
Answer: