Lab File: Object Oriented Programming using C++ (Paper Code: 100313P)
Instructions for Lab File Submission
● Use a yellow-colored lab file and write your answers on plain white sheets.
● Write only the question number and a brief one-line version of the question. Do not
copy the full instructions into the lab file.
Submit your completed lab file in the Computer Lab and sign the Lab File Submission
Record Sheet.
● If any question seems unclear or you are unsure of what exactly needs to be done, you
may make a reasonable assumption. Clearly mention your assumption in the
answer.
● Due Date: 31-05-2025
1. Class and Objects
Q1. Write a program to demonstrate the concept of class, objects, data members,
member functions, and access specifiers in C++.
Instructions:
● Create a class Book with the following:
○ Private data members: title (string), author (string), price (float)
○ Public member functions: setDetails() to take user input, and
displayDetails() to print book details.
● In main():
○ Create two objects of Book.
○ Call setDetails() and displayDetails() for each object.
2. Constructors and Destructors
Q2. Write a program to demonstrate default, parameterized, copy constructors and
destructors.
Instructions:
● Create a class Student with:
○ Private data members: name (string), age (int)
○ Constructors:
■ Default constructor (name="Unknown", age=0)
■ Parameterized constructor
■ Copy constructor
○ Destructor to display a message when object is destroyed
● In main():
○ Create objects using all three constructors.
○ Print values of each object.
3. Constructor Overloading
Q3. Write a program to demonstrate constructor overloading using different numbers
and types of parameters.
Instructions:
● Create a class Rectangle with:
○ Private data members: length and breadth
○ Constructors:
■ No-arg constructor (0, 0)
■ One-arg constructor (square)
■ Two-arg constructor
○ A method area() to return area
● In main():
○ Create all three types of objects and display their area.
4. Operator Overloading using Member Function
Q4. Write a program to overload the '+' operator for a Complex class using a member
function.
Instructions:
● Create class Complex with:
○ Private data: real and imag
○ Overload + to add two complex numbers
○ Display result using a method
● In main():
○ Create two Complex objects
○ Add them and display result.
5. Operator Overloading using Friend Function
Q5. Write a program to overload the ‘>>’ and ‘<<’ operators for input and output using
friend functions.
Instructions:
● Create class Time with:
○ Private data: hours, minutes
○ Friend functions to overload >> and <<
● In main():
○ Take input and display multiple Time objects.
6. Inheritance: Single, Multilevel, and Multiple Inheritance
Q6. Write a program to demonstrate all types of inheritance and constructor/destructor
call order.
Instructions:
● Create base class Person with name, age
● Create Student (single inheritance), ResearchStudent (multilevel)
● Create another class Employee, then ScholarshipHolder inheriting both
ResearchStudent and Employee
● Add constructors and destructors to show order of execution
● In main():
○ Create object of ScholarshipHolder, observe order of constructor and
destructor calls.
7. Virtual Base Class and Abstract Class
Q7. Write a program to demonstrate virtual base class and abstract class.
Instructions:
● Create abstract class Shape with pure virtual function area()
● Derive classes Circle, Rectangle from Shape
● Also demonstrate diamond problem using a virtual base class
● In main():
○ Create objects of derived classes and call area().
8. Function Overloading
Q8. Write a program to demonstrate function overloading using different data types and
number of parameters.
Instructions:
● Create class MathOps with three overloaded add() functions:
○ Two integers
○ Two floats
○ Three integers
● In main():
○ Call all versions and display results.
9. Class and Function Templates
Q9. Write a program to demonstrate class and function templates with one and multiple
type arguments.
Instructions:
● Create class template Pair<T1, T2> to hold two values
● Create function template maxValue() for two values
● In main():
○ Use the class and function with different types (int, float, string)
10. Pointers and Arrays
Q10. Write a program to demonstrate pointers to object, pointer to class, void pointer,
and array usage.
Instructions:
● Create class Box with data: length, width
● Use pointer to access member functions
● Create an array of objects and access using pointers
● Use a void pointer to store address of int and float, and print their values using
typecasting
11. Exception Handling
Q11. Write a program to demonstrate try, throw, catch with different exception handling
techniques.
Instructions:
● Create class BankAccount with balance
● Withdraw function should throw exception if insufficient balance
● Catch exception and either:
○ Terminate
○ Log and continue
○ Fix and retry
● Use multiple catch blocks for int, string, custom exception class
12. Typecasting and Type Conversion
Q12. Write a program to demonstrate built-in to class type, class to built-in type, and
class to class type conversion.
Instructions:
● Create class DistanceMeters with constructor accepting float
● Overload typecast to float
● Create DistanceFeet which accepts DistanceMeters as constructor argument
● Use conversion factor 1 meter = 3.28084 feet
13. Control Structures and Loop Statements
Q13. Write a program using all control statements: if, else-if, switch, break, continue, and
loops.
Instructions:
● Ask user for choice:
○ 1: Print factorial using while
○ 2: Print Fibonacci using for
○ 3: Use goto for simple retry logic
● Handle invalid choices using switch and default
14. Pass by Value and Pass by Reference
Q14. Write a program to demonstrate passing parameters by value and by reference.
Instructions:
● Create a function swapValues():
○ One version using pass-by-value (no actual swap)
○ One using pass-by-reference (actual swap)
● Show original and swapped values for both versions
15. Virtual Function and Runtime Polymorphism
Q15. Write a program to demonstrate runtime polymorphism using virtual functions.
Instructions:
● Create base class Account with virtual function displayDetails()
● Create derived classes SavingAccount, CurrentAccount overriding the function
● Use base class pointer to call displayDetails() on derived objects