0% found this document useful (0 votes)
49 views2 pages

OOPs Lab Questions

The document outlines a series of C++ programming lab questions focused on object-oriented programming concepts. It includes tasks such as creating classes, implementing functions, handling exceptions, and demonstrating polymorphism. Each question specifies requirements for user interaction, data handling, and class design, covering a range of topics from basic input/output to advanced features like inheritance and operator overloading.

Uploaded by

Rahul Raj
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)
49 views2 pages

OOPs Lab Questions

The document outlines a series of C++ programming lab questions focused on object-oriented programming concepts. It includes tasks such as creating classes, implementing functions, handling exceptions, and demonstrating polymorphism. Each question specifies requirements for user interaction, data handling, and class design, covering a range of topics from basic input/output to advanced features like inheritance and operator overloading.

Uploaded by

Rahul Raj
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/ 2

OOPs (using C++) 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.

2. Write a C++ function that:


• Takes an integer array as input.
• Calculates and prints the sum of all odd numbers and the sum of all even numbers separately.
Example:
If the input array is {3, 4, 7, 2, 5},
the output should be:
Sum of odd numbers: 15 (3 + 7 + 5)
Sum of even numbers: 6 (4 + 2)

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

This is a Rectangle of 4.0x6.0


Area: 24.0

This is a Circle with radius 2.5


Area: 19.6349

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

You might also like