Practice Questions For OOP Final
Practice Questions For OOP Final
1. Create a C++ class named Complex to represent complex numbers, with data members for the
real and imaginary parts. Include the following:
A parameterized constructor to initialize the real and imaginary parts to given values.
Write a main function to create complex numbers, add them using the overloaded operator, and
display the result.
2. Create a C++ class named Fraction to represent a fraction with a numerator and a denominator.
Include the following:
Data members for the numerator and denominator.
A default constructor that initializes the numerator to 0 and the denominator to 1.
A parameterized constructor to initialize the numerator and denominator to given
values.
An overloaded division operator (/) to divide two fractions, implemented as a friend
function.
A member function to display the fraction in the form numerator/denominator.
Write a main function to create Fraction objects, divide them using the overloaded operator, and
display the result.
3. Create a C++ class named Time to represent time in hours and minutes, with the following
features:
Write a main function to create Time objects, subtract them using the overloaded operator, and
display the result.
4. Create a C++ class named Date to represent a date with day, month, and year. Include the
following:
5. Create a base class Vehicle with a virtual function fuelEfficiency(). Derive two classes Car and Bike
from it and override the fuelEfficiency() function to calculate and display the fuel efficiency of a
car and a bike. In the main() function, create objects of Car and Bike, and use base class pointers
to call the fuelEfficiency() function.
6. Create a base class Appliance with a pure virtual function powerConsumption(). Derive two
classes WashingMachine and Refrigerator from it and override the powerConsumption() function
to calculate and display the power consumption of a washing machine and a refrigerator. In the
main() function, create objects of WashingMachine and Refrigerator, and use base class pointers
to call the powerConsumption() function
7. Create a base class Person with a pure virtual function displayInfo(). Derive a class Employee
from Person, and then further derive a class Manager from Employee.
The Person class should have a pure virtual function displayInfo() that displays basic
information about a person.
The Employee class should add additional attributes related to employment, such as
employeeID and department, and override the displayInfo() function to include these details.
The Manager class should add further attributes related to managerial roles, such as
teamSize, and override the displayInfo() function to include these additional details.
In the main() function, create an object of the Manager class and use a base class pointer to call
the displayInfo() function, displaying all the information.
8. Design a class named Book that encapsulates information about a book. The class should have
the following private members:
title - A string object that holds the title of the book.
author - A string object that holds the author’s name.
publicationYear - An integer variable that holds the year of publication.
10. Illustrate a class named DayOfWeek. The class should have the following private
members:
dayName - A string object that holds the name of the day, such as “Monday,”
“Tuesday,” etc.
dayNumber - An integer variable that holds the number of the day in the week (1 for
Monday, 2 for Tuesday, ..., 7 for Sunday). Valid values for this variable are 1 through
7.
11. Illustrate a class named Rectangle. The class should have the following private
members:
12. Design a class named Library that manages the book inventory for multiple branches of
a library network. Each branch has its own book inventory, categorized into four sections.
The library network keeps a record of the total number of books across all branches.
o Private Members:
books - An array of 4 integers to hold the number of books in each
section.
o Public Members:
Constructor that initializes the books array to zero.
Member function setBooks(int s1, int s2, int s3, int s4)
to set the number of books for the four sections.
Member function getBooks(int section) to return the number of
books for a given section (0 for Section 1, 1 for Section 2, 2 for
Section 3, 3 for Section 4).
o Private Members:
branches - An array of Branch objects.
totalBooks - A static variable to keep the total number of books for
the entire library network.
o Public Members:
Constructor that initializes the branches array.
Member function addBranch(int index, int s1, int s2, int
s3, int s4) to add book data for a specific branch. This function
should also update totalBooks.
Member function displayBranchBooks() to display the number of
books for each branch per section.
Member function displayTotalBooks() to display the total number
of books for the library network.
13. Task: Design a class named Employee to manage employee information and a class
named Department to handle multiple employees and their total salary expenditure.
14. Design a class named Product to manage product information, a class named Category
to organize products into categories, and a class named Store to manage multiple
categories and their total inventory.
15. Analyze the following C++ code to determine the output or identify any errors.
virtual void display() void show() { cout << Base() { cout << "Base
{ cout << "Base1"; } "Class A"; } Constructor\n"; }
public:
int main() { int main() {
Derived2() { cout <<
Base1 *b1; C c; "Derived2 Constructor\n"; }
} };
} Final f;
return 0;
}
#include <iostream> #include <iostream> #include <iostream>
class Counter {
// Default constructor
cout << }
"Parameterized
// Parameterized Constructor Called" <<
constructor endl;
// Parameterized
Book(const char* t, int } constructor
p) : pages(p) {
Inventory(const char*
title = new name, int qty) : quantity(qty)
char[strlen(t) + 1]; // Copy constructor {
}; }
// Destructor
~Book() {
int main() { // Destructor
delete[] title;
Counter c1(5); ~Inventory() {
cout << "Destructor
Called" << endl; Counter c2(c1); delete[] itemName;
int main() { };
b3.display(); inv1.display();
return 0; inv2.display();
} inv3.display();
return 0;
16.