0% found this document useful (0 votes)
42 views10 pages

Practice Questions For OOP Final

OOP

Uploaded by

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

Practice Questions For OOP Final

OOP

Uploaded by

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

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 default constructor that initializes the real and imaginary parts to 0.

 A parameterized constructor to initialize the real and imaginary parts to given values.

 An overloaded addition operator (+) to add two complex numbers.

 A friend function to print a complex number in the form a + bi.

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:

 Data members for hours and minutes.

 A default constructor that initializes hours to 0 and minutes to 0.

 A parameterized constructor to initialize hours and minutes to given values.

 An overloaded subtraction operator (-) to subtract two Time objects.

 A member function to display the time in the format HH:MM.

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:

 Data members for the day, month, and year.

 A default constructor that initializes the date to 1/1/2000.

 A parameterized constructor to initialize the date to given values.

 An overloaded equality operator (==) to compare two Date objects.

 A member function to display the date in the format DD/MM/YYYY.


Write a main function to create Date objects, compare them using the overloaded operator, and
display whether they are equal.

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.

The class should provide the following:

 A default constructor that initializes title to "Unknown", author to "Unknown", and


publicationYear to 0.
 A constructor that accepts the title, author, and publication year as arguments.
 A constructor that accepts the title and author as arguments and sets publicationYear to a
default value of 2000.
 Member functions to set and get the values of title, author, and publicationYear.
 Overload the << operator to output the book's details in a formatted manner (e.g., "Title:
<title>, Author: <author>, Year: <publicationYear>").
 Additionally, implement a function to compare two Book objects to determine if they are the
same based on their title and author. The comparison function should return true if the two
books are the same and false otherwise.
9. Illustrate a class named Student. The class should have the following private members:

 name - A string object that holds the name of the student.


 id - An integer variable that holds the student ID number.
 grade - A double variable that holds the student’s GPA (Grade Point Average).
In addition, provide the following member functions:

 A default constructor that initializes name to "Unknown", id to 0, and grade to 0.0.


 A constructor that accepts the student's name, ID, and GPA as arguments.
 A constructor that accepts the student's name and ID, and sets grade to a default
value of 4.0 (assuming a perfect GPA).
 Appropriate set and get functions for the name, id, and grade member variables.
 Overload the += operator to add a GPA increment to the student’s current GPA,
ensuring the GPA does not exceed 4.0.
 Overload the -= operator to subtract a GPA decrement from the student’s current
GPA, ensuring the GPA does not go below 0.0.

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.

In addition, provide the following member functions:

 A default constructor that sets dayNumber to 1 and dayName to “Monday.”


 A constructor that accepts the name of the day as an argument. It should set dayName
to the value passed as the argument and set dayNumber to the correct value.
 A constructor that accepts the number of the day as an argument. It should set
dayNumber to the value passed as the argument and set dayName to the correct day
name.
 Appropriate set and get functions for the dayName and dayNumber member variables.
 Prefix and postfix overloaded ++ operator functions that increment dayNumber and set
dayName to the name of the next day. If dayNumber is set to 7 when these functions
execute, they should set dayNumber to 1 and dayName to “Monday.”
 Prefix and postfix overloaded -- operator functions that decrement dayNumber and
set dayName to the name of the previous day. If dayNumber is set to 1 when these
functions execute, they should set dayNumber to 7 and dayName to “Sunday.”

11. Illustrate a class named Rectangle. The class should have the following private
members:

 length - A double variable that holds the length of the rectangle.


 width - A double variable that holds the width of the rectangle.

In addition, provide the following member functions:

 A default constructor that initializes length and width to 1.0.


 A constructor that accepts the length and width as arguments.
 A constructor that accepts only one argument and sets both length and width to this
value (creating a square).
 Appropriate set and get functions for length and width.
 Overload the + operator to add the dimensions of two Rectangle objects. The
resulting rectangle should have dimensions equal to the sum of the respective
dimensions.
 Overload the * operator to scale the dimensions of the Rectangle by a factor. The
factor should be a double value, and the resulting rectangle should have dimensions
scaled by this factor.

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.

Define a class Branch with the following members:

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).

Define a class Library with the following members:

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.

1. Define a class Employee with the following members:


o Private Members:
 name - A string to hold the employee's name.
 salary - A double to hold the employee's salary.
o Public Members:
 Constructor that initializes the name and salary of the employee.
 Member function getName() to return the name of the employee.
 Member function getSalary() to return the salary of the employee.
2. Define a class Department with the following members:
o Private Members:
 employees - An array of Employee objects.
 totalSalary - A static variable to keep the total salary expenditure for
the department.
o Public Members:
 Constructor that initializes the employees array.
 Member function addEmployee(int index, string name,
double salary) to add an employee to a specific index in the array.
This function should also update totalSalary.
 Member function displayEmployeeDetails() to display the details
(name and salary) of each employee in the department.
 Member function displayTotalSalary() to display the total salary
expenditure for the department.

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.

1. Define a class Product with the following members:


o Private Members:
 productName - A string to hold the product's name.
 quantity - An integer to hold the quantity of the product.
o Public Members:
 Constructor that initializes the productName and quantity of the
product.
 Member function getProductName() to return the name of the
product.
 Member function getQuantity() to return the quantity of the
product.
 Member function setQuantity(int qty) to set the quantity of the
product.
2. Define a class Category with the following members:
o Private Members:
 products - An array of Product objects.
 categoryName - A string to hold the name of the category.
 totalQuantity - A variable to keep the total quantity of products in
the category.
o Public Members:
 Constructor that initializes the products array and categoryName.
 Member function addProduct(int index, string name, int
quantity) to add a product to a specific index in the array. This
function should also update totalQuantity.
Member function displayCategoryProducts() to display the
details of each product in the category.
 Member function getTotalQuantity() to return the total quantity of
products in the category.
3. Define a class Store with the following members:
o Private Members:
 categories - An array of Category objects.
 totalInventory - A static variable to keep the total inventory across
all categories.
o Public Members:
 Constructor that initializes the categories array.
 Member function addCategory(int index, string
categoryName) to create a new category at a specific index.
 Member function addProductToCategory(int catIndex, int
prodIndex, string name, int quantity) to add a product to a
specific category. This function should also update totalInventory.
 Member function displayStoreInventory() to display the
inventory of each category.
 Member function displayTotalInventory() to display the total
inventory for the store.

15. Analyze the following C++ code to determine the output or identify any errors.

#include <iostream> #include <iostream> #include <iostream>

using namespace std; using namespace std; using namespace std;

class Base1 { class A { class Base {

public: public: public:

virtual void display() void show() { cout << Base() { cout << "Base
{ cout << "Base1"; } "Class A"; } Constructor\n"; }

}; }; // Destructor is not virtual

~Base() { cout << "Base


Destructor\n"; }
class Base2 { class B {
};
public: public:

virtual void display() void show() { cout <<


{ cout << "Base2"; } "Class B"; } class Derived1 : virtual
public Base {
}; };
public:

class Derived : public class C : public A, public Derived1() { cout <<


Base1, public Base2 { B{ "Derived1 Constructor\n"; }

public: public: ~Derived1() { cout <<


"Derived1 Destructor\n"; }
void display() override { void show() { cout <<
cout << "Derived"; } "Class C"; } };

}; }; class Derived2 : virtual


public Base {

public:
int main() { int main() {
Derived2() { cout <<
Base1 *b1; C c; "Derived2 Constructor\n"; }

Base2 *b2; A *a = &c; ~Derived2() { cout <<


"Derived2 Destructor\n"; }
Derived d; B *b = &c;
};

class Final : public


b1 = &d; a->show(); Derived1, public Derived2 {

b2 = &d; b->show(); public:

c.show(); Final() { cout << "Final


Constructor\n"; }
b1->display();
~Final() { cout << "Final
b2->display(); return 0; Destructor\n"; }

} };

return 0; int main() {

} Final f;

return 0;

}
#include <iostream> #include <iostream> #include <iostream>

#include <cstring> using namespace std; #include <cstring>


using namespace std; using namespace std;

class Counter {

class Book { private: class Inventory {

private: int count; private:

char* title; char* itemName;

int pages; public: int quantity;

// Default constructor

public: Counter() : count(0) { public:

// Default constructor cout << "Default // Default constructor


Constructor Called" <<
Book() : title(new endl; Inventory() : quantity(0) {
char[20]), pages(0) {
} itemName = new
strcpy(title, char[10];
"Untitled");
strcpy(itemName,
cout << "Default // Parameterized "Unknown");
Constructor Called" << constructor
endl; cout << "Default
Counter(int c) : Constructor Called" <<
} count(c) { endl;

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 {

strcpy(title, t); Counter(const Counter itemName = new


&other) : char[strlen(name) + 1];
cout << count(other.count + 10) {
"Parameterized strcpy(itemName,
Constructor Called" << cout << "Copy name);
endl; Constructor Called" <<
endl; cout << "Parameterized
Constructor Called" <<
} } endl;

// Copy constructor // Increment count

Book(const Book void increment() { // Copy constructor


&other) :
pages(other.pages) { count++; Inventory(const Inventory
&other) :
title = new } quantity(other.quantity) {
char[strlen(other.title) + 1];
itemName = new
strcpy(title, char[strlen(other.itemName)
other.title); // Display count + 1];

cout << "Copy void display() const { strcpy(itemName,


Constructor Called" << other.itemName);
endl; cout << "Count: " <<
count << endl; cout << "Copy
} Constructor Called" <<
} endl;

}; }
// Destructor

~Book() {
int main() { // Destructor
delete[] title;
Counter c1(5); ~Inventory() {
cout << "Destructor
Called" << endl; Counter c2(c1); delete[] itemName;

} c1.increment(); cout << "Destructor


Called" << endl;
c1.display();
}
// Display book details c2.display();

void display() const { return 0;


// Display item details
cout << "Title: " << }
title << ", Pages: " << void display() const {
pages << endl;
cout << "Item: " <<
} itemName << ", Quantity: "
<< quantity << endl;
};
}

int main() { };

Book b1("C++ Primer",


500);
int main() {
Book b2(b1);
Inventory inv1("Laptop",
Book b3; 50);

b1.display(); Inventory inv2(inv1);

b2.display(); Inventory inv3;

b3.display(); inv1.display();

return 0; inv2.display();

} inv3.display();

return 0;

16.

You might also like