0% found this document useful (0 votes)
27 views3 pages

CPP Encapsulation Inheritance FriendFunction Que

The document outlines 11 practical programming questions in C++ focusing on encapsulation, inheritance, and friend functions. It includes tasks such as designing classes with private members, implementing constructors, and demonstrating encapsulation through various examples like BankAccount, Student, and a Library System. Additionally, it covers the use of friend functions to access private data across classes, emphasizing the importance of encapsulation principles.

Uploaded by

aryapatel946
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)
27 views3 pages

CPP Encapsulation Inheritance FriendFunction Que

The document outlines 11 practical programming questions in C++ focusing on encapsulation, inheritance, and friend functions. It includes tasks such as designing classes with private members, implementing constructors, and demonstrating encapsulation through various examples like BankAccount, Student, and a Library System. Additionally, it covers the use of friend functions to access private data across classes, emphasizing the importance of encapsulation principles.

Uploaded by

aryapatel946
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

C++ Practical Questions

Encapsulation | Inheritance (Combined) | Friend Functions

Total Questions: 11 Type: Practical / Program Writing Topics: 3 Combined

SECTION A : Encapsulation
Que 1.

Design a class BankAccount that encapsulates the data members: accountNumber,


holderName, and balance (all private). Provide a constructor to initialize them and public
getter/setter methods. Add a method deposit(amount) that validates the amount is positive
before adding, and a method withdraw(amount) that checks sufficient balance. Demonstrate the
complete class with a main() function.

Que 2.

Create a class Student with private members: name, rollNo, and marks[5]. Write a constructor to
initialize all members. Provide public methods: setMarks(), getMarks(), calculateAverage(), and
getGrade(). Ensure marks are validated (0–100) inside setter. Create two student objects and
display their details.

Que 3.
Write a C++ program to demonstrate encapsulation using a class Temperature. Store the
temperature privately in Celsius. Provide setter setCelsius(float) with validation (must be above
-273.15°C). Provide getters: getCelsius(), getFahrenheit(), and getKelvin(). Show how internal
representation is hidden from the user.

Que 4.

Implement a class called Stack using encapsulation. Use a private array of size 10 and a private
top pointer. Provide public methods: push(int), pop(), peek(), isEmpty(), and isFull(). Each
method must validate its precondition (e.g., pop on empty stack prints an error). Write a main() to
demonstrate all operations.

SECTION B : Encapsulation + Inheritance (Combined)


Que 5.

Create a base class Person with private members: name and age. Provide a constructor and
public getters/setters with validation (age must be 0–120). Derive a class Employee that adds
private members: empId and baseSalary. Further derive a class Manager that adds a private
bonus. Override a method displayInfo() in each class. Show that private members of Person
cannot be accessed directly in derived classes — only through protected/public methods.
Que 6.

Design a class hierarchy for a Library System using encapsulation and inheritance. Base class
LibraryItem has private members: itemId, title, and author. Derive class Book (adds private
members: ISBN and pages) and class Magazine (adds issueNumber and month). Each class
must have its own constructor, getters/setters, and a virtual method displayDetails().
Demonstrate polymorphism using a base class pointer.

Que 7.

Write a program that demonstrates encapsulation with multilevel inheritance. Create: Vehicle
(private: registrationNo, speed), Car (private: brand, fuelType derived from Vehicle), and
ElectricCar (private: batteryCapacity, range derived from Car). Use constructors to initialize all
private members through the chain. Provide a method showSpecs() at each level and
demonstrate constructor calling order.

Que 8.

Implement a program combining encapsulation and multiple inheritance. Create two base
classes: Flyable (private: maxAltitude, wingSpan) and Swimmable (private: maxDepth,
swimSpeed). Derive a class Duck that inherits from both. All data must remain private with
proper getters/setters. Add constructors to each class and a display() method that prints all
attributes. Show constructor calling order in main().

SECTION C : Friend Functions


Que 9.
Write a C++ program to demonstrate a friend function. Create a class Box with private members:
length, breadth, and height. Declare a global function volume(Box&) as a friend of Box. The
function should calculate and return the volume by directly accessing the private members.
Explain why friendship is needed here and what encapsulation trade-offs it introduces.

Que 10.

Create two classes: Distance (stores meters and centimeters as private) and Weight (stores
kilograms and grams as private). Write a friend function compare(Distance&, Weight&) that
accesses private members of both classes and prints both values for comparison. Demonstrate
that a single friend function can access private data of multiple classes simultaneously.

Que 11.

Design a program that combines all three topics: encapsulation, inheritance, and friend
functions. Create a base class Account with private members: accountId and balance. Derive a
class SavingsAccount (adds private interestRate) and a class CurrentAccount (adds private
overdraftLimit). Declare a friend function auditAccounts(SavingsAccount&, CurrentAccount&)
that reads the private data of both objects and prints a complete financial audit report.
Demonstrate constructor chaining and show how the friend function bridges encapsulation
across both derived classes.

All programs must be written in C++ | Use constructors wherever applicable | Follow proper encapsulation principles

You might also like