Encapsulation

 

Encapsulation:

Encapsulation is one of the fundamental principles of object-oriented programming (OOP). Which defines as the bundling of data (data-Members, fields, or attributes) and methods (functions, Member-Functions) that operate on the data into a single unit (class) known as a class.
The main purpose of Encapsulation is data hiding.
 

Practice Program:


Q1:Create a class Book with private fields for title, author, and price. Include getter and setter methods. Demonstrate the encapsulation by creating instances and updating the fields.

Q2:Design a class Student to encapsulate student information such as name, age, and grade. Use appropriate access modifiers and methods to interact with the class.

Q3:Design a class TemperatureConverter with methods to convert Celsius to Fahrenheit and vice versa. Ensure valid input values.

Q4:Build a class Employee with private fields for name, id, and salary. Create methods to give a salary raise and display employee details. Ensure the salary is not negative

Q5:Develop a class Library that encapsulates books in a library. Implement methods to add and remove books, and a method to display all available books.

Q6:Construct a class Product to encapsulate product details such as name, price, and quantity. Include methods to calculate the total value and restock items.

Important Points:

  1. Encapsulation uses access modifiers (e.g., private, public, protected) to control the visibility of class members (data member and member functions)