Lab 01 - OOP Refresher
Lab 01 - OOP Refresher
Spring 2025
LAB-01 Issue Date: February 7, 2025
ALERT!
1. Write a function template that finds and returns the second largest element in a given array of
generic type. The function should take an array and its size as input parameters and determine the
second largest element without modifying or sorting the original array.
Sample Execution 1:
Inputs: Array = [10, 20, 4, 45, 99] size = 5
Output: 45
Sample Execution 2:
Inputs: Array = [7, 7, 7, 7] size = 4
Output: Error: No second largest element found.
Sample Execution 3:
Inputs: Array = [3.5, 7.2, 8.9, 8.9, 5.1] size = 5
Output: 7.2
2. Write a function template to shift the elements of an array K times to the left. The function should
take an array, its size, and K (number of shifts) as input parameters and modify the array in-place
without using extra space.
Sample Execution 1:
Inputs: Array = [1, 2, 3, 4, 5], size = 5, K=2
Output: [3, 4, 5, 1, 2]
Sample Execution 2:
Inputs: Array = ['A', 'B', 'C', 'D'], size = 4, K=3
Output: ['D', 'A', 'B', 'C']
Sample Execution 3:
Inputs: Array = [10, 20, 30, 40, 50], size = 5, K=7
Output: [30, 40, 50, 10, 20]
Write a driver program to create multiple arrays of user defined size, populate the arrays with user
given data and test the functionality of both the function templates.
When you program in C++, failing to deallocate a pointer can lead to memory leaks, which may
eventually cause a program to crash. Languages like Java and C# have built-in Garbage Collection
Mechanisms that automatically reclaim unused memory, relieving programmers of the responsibility
of manual memory management. However, C++ introduces its own solution: Smart Pointers. These
automatically handle memory deallocation when an object is destroyed.
A Smart Pointer is essentially a wrapper class around a raw pointer, with operators like * and →
overloaded. Although smart pointer objects are just like regular pointers, they provide the additional
benefit of automatic memory management, ensuring that memory is properly deallocated when the
object is no longer in use.
The concept involves creating a class that encapsulates a pointer, a destructor, and overloaded operators
such as * and →. Since destructors are automatically invoked when an object goes out of scope, this
guarantees that dynamically allocated memory is freed. Your task is to implement a smart pointer class
with using class template.
In the main function, instantiate smart pointer to int and smart pointer to char, then dynamically allocate
an integer and a character respectively. Assign values to the dynamically created variables and print
them using the overloaded de-refencing operators. Test object destruction by checking if memory is
automatically freed.