0% found this document useful (0 votes)
5 views

Lab 01 - OOP Refresher

The lab focuses on refreshing OOP concepts and good coding conventions while implementing data structures and algorithms. Students are required to write function templates for finding the second largest element in an array and shifting elements left, as well as create a smart pointer class in C++. Strict guidelines against collaboration and plagiarism are emphasized, and tasks include proper documentation and memory management practices.

Uploaded by

Zohaib Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 01 - OOP Refresher

The lab focuses on refreshing OOP concepts and good coding conventions while implementing data structures and algorithms. Students are required to write function templates for finding the second largest element in an array and shifting elements left, as well as create a smart pointer class in C++. Strict guidelines against collaboration and plagiarism are emphasized, and tasks include proper documentation and memory management practices.

Uploaded by

Zohaib Haider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CC-213L – Data Structures and Algorithms

Spring 2025
LAB-01 Issue Date: February 7, 2025

The objective of this lab is to:


1. Refresh OOP concepts focusing on Abstract Data Types, dynamic memory allocation, templates and
array of objects.
2. Reiterate good coding conventions.

ALERT!

1. You are encouraged to bring your laptop to attend the lab.


2. This is an individual lab, you are strictly NOT allowed to discuss your solution with fellow colleagues,
even not allowed asking how is he/she is doing, it may result in negative marking. You can ONLY
discuss with your TAs or with me.
3. Anyone caught in act of plagiarism would be awarded an “F” grade in this Lab.

Task 00: [10 Minutes]

Follow the good coding conventions for your lab tasks:


▪ Meaningful variable, function, and class names.
▪ Use camelCase user defined identifiers i.e. variable and function names.
▪ Well indented.
▪ Clear modular structure.
▪ Provide clear and meaningful error messages.
▪ Push interfaces and declarations up and implementation down.
▪ Do proper documentation.
▪ Use appropriate white space in your programs, and do so in a consistent fashion to make them easy
to read.
▪ Good coding convention makes program readable and easy to debug.
▪ Adhere to the same coding style.

Task 01: [5+5 Marks]

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.

Your function should deal with the following constraints:


▪ The array must contain at least two distinct elements, if not, throw an appropriate exception.
▪ If the array does not contain a second largest element (e.g., all elements are the same), the
function should throw an appropriate exception.

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

Faculty of Computing and Information Technology Page 1 of 3


Dr. Madiha Khalid
CC-213L – Data Structures and Algorithms
Spring 2025
LAB-01 Issue Date: February 7, 2025

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.

Your function should deal with the following constraints:


▪ If K = 0, the array remains unchanged.
▪ The value of K can be greater than the size of the array, so handle such cases using exception
handling.

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.

Task 02: [15 Marks]

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.

Your smart pointer should be able to:


▪ Allocate and deallocate memory automatically.
▪ Support dereferencing (operator*).
▪ Prevent memory leaks.

You should implement following functions in your class.

▪ A Constructor to dynamically allocate memory.


▪ A destructor to automatically deallocate memory when the object goes out of scope.
▪ Overload the operator* to return the object value.
▪ Overload the operator-> to provide direct access to the object's members.

Faculty of Computing and Information Technology Page 2 of 3


Dr. Madiha Khalid
CC-213L – Data Structures and Algorithms
Spring 2025
LAB-01 Issue Date: February 7, 2025

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.

Faculty of Computing and Information Technology Page 3 of 3


Dr. Madiha Khalid

You might also like