0% found this document useful (0 votes)
8 views19 pages

Complete Oop Lab File

The document outlines a series of lab experiments focused on implementing various C++ concepts, including dynamic memory management, operator overloading, inheritance, polymorphism, templates, and exception handling. Each experiment provides objectives, explanations, code structure, and conclusions that highlight the practical applications of these programming principles. The experiments culminate in a file operation task that demonstrates generating and handling complex numbers in a structured format.

Uploaded by

roysahil689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Complete Oop Lab File

The document outlines a series of lab experiments focused on implementing various C++ concepts, including dynamic memory management, operator overloading, inheritance, polymorphism, templates, and exception handling. Each experiment provides objectives, explanations, code structure, and conclusions that highlight the practical applications of these programming principles. The experiments culminate in a file operation task that demonstrates generating and handling complex numbers in a structured format.

Uploaded by

roysahil689
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lab Experiment 2: Matrix Class Implementation

Objective:
To implement a Matrix class in C++ using dynamic memory, constructors, a destructor, a copy
constructor, and assignment operator overloading.

Explanation:
In this lab, we’ll create a Matrix class to handle 2D matrices using dynamic memory allocation. The
class will include:

• A constructor to initialize the matrix with a given number of rows and columns.

• A destructor to free allocated memory.

• A copy constructor to create a deep copy of a matrix.

• An assignment operator to copy values between matrices.

We will also add utility methods to set and get matrix values and a method to print the matrix. This
example demonstrates fundamental memory management and object-oriented programming concepts
in C++.

Code:
How It Works:
1. Matrix Class: Constructor allocates memory; destructor frees it.

2. Copy Constructor: Deep-copies data for new matrices.

3. Assignment Operator: Safely copies matrix data between objects.

4. Utility Methods: setValue, getValue, and print handle values.

Output:

Conclusion:
This experiment demonstrates dynamic memory management, copy construction, and operator
overloading in C++. The Matrix class provides safe and efficient handling of 2D data.
Lab Experiment 3: Complex Number Class
Objective:
To create a Complex class in C++ that supports arithmetic operations, allows type conversions, and
displays complex numbers.

Explanation:
In this experiment, we’ll make a Complex class with:

• A constructor to set real and imaginary parts.

• Operator overloading to add complex numbers and convert integers and doubles into complex
numbers.

• Type conversions to convert a Complex number to a double, representing its magnitude.

This class covers key topics in operator overloading and type conversions in C++.

Code:
How It Works:
1. Complex Class: Sets up complex numbers with real and imaginary parts.

2. Addition Overloading (+): Adds two complex numbers or a complex number and a basic
number (e.g., double).

3. Assignment Overloading: Allows assignment from int and double to Complex.

4. Type Conversion: Converts Complex to double as its magnitude.

5. Output Formatting (<<): Displays complex numbers in the form a + bi.

Output:

Conclusion:
This experiment demonstrates C++ operator overloading, type conversions, and stream insertion
operator overloading, providing flexible and efficient management of complex numbers and their
interactions with basic data types.
Lab Experiment 4: Custom Memory Allocation with new and delete
Objective:
To overload the new and delete operators in C++ for a custom memory allocation in a class.

Explanation:
In this lab, we’ll create a class that uses custom new and delete operators. This allows us to control how
memory is allocated and deallocated for objects of this class. Overloading new and delete can help
manage memory more efficiently or add extra functionality (like logging or debugging) when creating
or destroying objects.

Code:
How It Works:
1. CustomClass: A basic class with an integer data member, a constructor to initialize it, and a
destructor.

2. Overloaded new Operator: Replaces the default new operator to control memory allocation.
It uses malloc to allocate memory and prints a message when called.

3. Overloaded delete Operator: Replaces the default delete operator to control memory
deallocation. It uses free to release memory and prints a message when called.

4. display() Method: A simple method to display the data member.

Output:

Conclusion:
Overloading new and delete operators gives precise control over memory management. In this example,
we used malloc and free to allocate and deallocate memory manually, adding custom messages for
better tracking. This approach is useful for debugging, logging, or custom memory pooling.
Lab Experiment 5: Class Hierarchy for Inheritance Types
Objective:
To create a C++ class hierarchy that demonstrates different types of inheritance: single, multiple,
multilevel, and hierarchical inheritance.

Explanation:
In this experiment, we create a class hierarchy with base and derived classes to illustrate:

1. Single Inheritance: One class inherits from another.

2. Multilevel Inheritance: A class is derived from a derived class.

3. Multiple Inheritance: A class inherits from two or more base classes.

4. Hierarchical Inheritance: Multiple classes inherit from a single base class.

This example uses classes for demonstration, each with basic methods to show inheritance relationships.

Code:
How It Works:
1. Animal Class: Base class with a method eat() representing the behavior common to all animals.

2. Single Inheritance (Dog): Dog inherits from Animal, so it has access to eat() and adds bark().

3. Multilevel Inheritance (Puppy): Puppy inherits from Dog, so it has access to both eat() and
bark(), and it adds play().

4. Multiple Inheritance (Cat): Cat inherits from both Animal and Mammal, allowing it to use
eat(), feedMilk(), and meow().

5. Hierarchical Inheritance (Bird): Both Dog and Bird inherit from Animal, demonstrating
multiple derived classes from a single base.

Output:

Conclusion:
This experiment demonstrates various types of inheritance in C++. By creating a class hierarchy, we
can show relationships between classes and reuse code through inheritance, which is a key feature of
object-oriented programming. This approach enables flexible design and helps avoid redundant code.
Lab Experiment 6: Demonstrating Dynamic Polymorphism and RTTI
Objective:
To create a C++ test application that demonstrates dynamic polymorphism and Runtime Type
Identification (RTTI). This program will utilize base and derived classes to show polymorphic behavior
and identify object types at runtime.

Explanation:
In C++, dynamic polymorphism allows derived classes to modify the behavior of functions in a base
class. Through RTTI, the program can identify the actual type of the derived class object at runtime,
allowing for type-specific actions.

Code:
How It Works
1. Dynamic Polymorphism: The sound() function is virtual in Animal, allowing derived classes
like Dog and Cat to override it. When called on an Animal*, the specific derived version of
sound() is executed.

2. RTTI: Using typeid, we determine the actual type of the derived object. This enables printing
specific messages depending on whether the object is of type Dog or Cat.

Output:

Conclusion:
This lab demonstrates dynamic polymorphism by allowing the same Animal pointer to call functions
from different derived classes (Dog and Cat). RTTI enhances this by enabling type-specific actions
based on the actual derived type, a valuable feature in object-oriented programming that supports code
flexibility and type safety.
Lab Experiment 7: Template for a Linked List Class in C++
Objective:
To create a template-based Linked List class in C++ that can store elements of any data type, with
methods to perform basic operations like adding, removing, and displaying nodes.

Explanation:
Using templates, we can create a generic linked list class that supports different data types (int, float,
string, etc.). Each node in the list will store data and a pointer to the next node, forming a singly linked
list.

Code:
How It Works:
1. Node Class: A Node class template is defined, storing data of any type (T) and a pointer to the
next node.

2. LinkedList Class: The LinkedList class contains:

o append method to add a new node to the end of the list.

o display method to print all elements.

o remove method to delete a node by value.

3. Destructor: Ensures all nodes are deleted when the linked list is destroyed.

Output:

Conclusion
This experiment demonstrates a template-based linked list class in C++, showcasing how to create and
manipulate a linked list for any data type. This generic approach increases reusability and flexibility,
making it useful for various data structures in programming.
Lab Experiment 8: Templates for Standard Sorting Algorithms in C++
Objective:
To create template-based implementations of standard sorting algorithms, including Bubble Sort,
Insertion Sort, and Quick Sort in C++, allowing these algorithms to work with different data types.

Explanation:
Using templates in C++, we can create generic versions of sorting algorithms that can be applied to
arrays of any data type, such as int, float, or string. This approach makes the algorithms more flexible
and reusable.

Code:
How It Works:
1. Bubble Sort: Compares adjacent elements and swaps them if they are in the wrong order,
pushing larger elements to the end with each iteration.

2. Insertion Sort: Iteratively inserts elements into their correct position in the sorted portion of
the array, ensuring the array is sorted up to each element.

3. Quick Sort: Uses a pivot element to partition the array into two halves, sorting them
recursively. It places smaller elements on the left and larger on the right of the pivot.

Output:

Conclusion:
This experiment demonstrates template-based implementations of Bubble Sort, Insertion Sort, and
Quick Sort, allowing each algorithm to operate on arrays of any data type. Templates provide a flexible
and reusable way to write generic sorting functions in C++, a powerful feature of the language.
Lab Experiment 9: Stack and Queue Classes with Exception Handling in
C++
Objective:
To implement Stack and Queue classes in C++ with exception handling to manage errors such as
overflow and underflow conditions.

Explanation:
In this experiment, we’ll create stack and queue data structures with common operations like push/pop
for the stack and enqueue/dequeue for the queue. Exception handling is used to manage cases where
operations are invalid, such as trying to remove an element from an empty stack or queue.

Code:
How It Works
1. Stack Class:

o push: Adds an element to the top. Throws an overflow_error if the stack is full.

o pop: Removes the top element. Throws an underflow_error if the stack is empty.

o peek: Returns the top element without removing it, with an exception if empty.

2. Queue Class:

o enqueue: Adds an element to the end. Throws overflow_error if the queue is full.

o dequeue: Removes the front element. Throws underflow_error if the queue is empty.

Output:

Conclusion:
This experiment demonstrates creating Stack and Queue classes with exception handling to manage
errors gracefully, showing how to catch and manage overflows and underflows effectively. This
improves program reliability by handling cases where operations may fail.
Lab Experiment 10: Complex Number File Operations with Random
Generation in C++
Objective:
To create a C++ program that generates random complex numbers, writes them to a file in pairs with a
random operator between them, and reads the file line by line.

Explanation:
This experiment involves two main tasks:

1. File Writing Program: Randomly generates complex numbers, pairs them with a random
operator (+, -, *, /), and writes them to a file.

2. File Reading Program: Reads and displays the file content line by line.

Code:
File Writing Program:
This program generates complex numbers in the format (a + bi), pairs them with an operator, and writes
them to a file.
File Reading Program:
This program reads and displays each line from the file.

Combined Code:
Output:
This program creates a file with lines like:

And displays these lines on the console upon reading.

Conclusion:
This experiment demonstrates how to work with file operations in C++, particularly generating and
writing structured data to a file and reading it back. It shows how to handle basic I/O in a controlled
format, illustrating a practical example of file handling with complex numbers in C++.

You might also like