OOPs Lab Questions
OOPs Lab Questions
1. Write a C++ program that demonstrates basic input/output (cin/cout) operations by interacting with the user
and display a greeting message.
Task:
• Prompt the user to enter their name and age.
• Read the input values.
• Display a greeting message that includes the entered name and age.
Example:
Enter your name: Sunny
Enter your age: 20
Hello, Sunny! You are 20 years old.
3. Write a C++ program that defines a function isPrime() to check if a number is prime, and use it to validate user
input.
Task:
a) Function isPrime(int n):
o Takes an integer n as input.
o Returns true if n is prime, otherwise false.
o Logic: A prime number is only divisible by 1 and itself. Handle edge cases (e.g., numbers ≤ 1).
b) Main Program:
o Prompt the user to enter an integer.
o Call isPrime() and display whether the number is prime or not.
Example 1:
Enter a number: 17
17 is a prime number .
Example 2:
Enter a number: 4
4 is not a prime number.
4. Write a program to define a class Book with data members (title, author, price) and member functions to input
and display details.
Include:
o Parameterized constructor, default constructor, destructor.
o Accessor and mutator functions.
5. Implement a class BankAccount with features like deposit, withdrawal, balance check.
o Use reference parameters in getters.
o Create object arrays in main and call each object using for loop.
6. Write a program with two overloaded functions in the same class and call them from the main method.
• sum(int a, int b) → returns sum of two integers.
• sum(double a, double b) → returns sum of two doubles.
Page 1 of 2
7. Create a C++ class Complex to represent complex numbers (real and imaginary parts). Implement function
operator overloading to perform arithmetic operations addition of two complex numbers.
8. Implement a class hierarchy to represent shapes (Shape → [Rectangle, Circle, etc.] -> [permitter, area etc.]) using
inheritance.
9. Create a C++ program that dynamically allocates an array of objects using pointers. Demonstrate how to:
• Use the new operator to allocate memory for an array of objects
• Access object members using the pointer (->) operator
• Properly deallocate memory using the delete[] operator
Define Class Students having data members name, regNo, branch and related getter setters and a display
function.
10. Write a program that takes two integers as input and performs division. Handle exceptions for division by zero
using try-catch blocks.
11. Create a C++ program to demonstrate runtime polymorphism through function overriding using a base class and
derived classes.
Requirements:
a) Base Class (Shape):
o Contains:
▪ A virtual function area() that returns 0
▪ A virtual function display() that prints "This is a Shape"
b) Derived Classes:
o Circle (inherits from Shape):
▪ Additional data member: radius (float)
▪ Overrides:
▪ area() → calculates and returns 3.14159 * radius * radius
▪ display() → prints "This is a Circle with radius [radius]"
o Rectangle (inherits from Shape):
▪ Additional data members: length, width (float)
▪ Overrides:
▪ area() → returns length * width
▪ display() → prints "This is a Rectangle of [length]x[width]"
c) Main Program:
o Create an array of Shape pointers
o Store objects of both Circle and Rectangle in this array
o Demonstrate polymorphism by:
▪ Calling display() for each object
▪ Calculating and printing area() for each object
o Include proper memory cleanup
Example Output:
Creating shapes:
This is a Circle with radius 5.0
Area: 78.5397
12. Define a custom exception class InvalidInputException. Write a function that throws this exception if user input
is negative. Demonstrate stack unwinding.
Page 2 of 2