Encapsulation is a fundamental concept in object-oriented programming that binds together the data and functions that manipulate the data, and keeps both safe from outside interference and misuse.
It helps to protect the internal state of an object from unauthorized access and modification, promoting modularity and maintainability.
class Employee {
private:
string name;
int age;
public:
void setName(string n) { name = n; }
string getName() { return name; }
void setAge(int a) { age = a; }
int getAge() { return age; }
};
Encapsulation allows for controlled access to the data through public methods, while keeping the actual data private.
This example demonstrates encapsulation by using private data members and public member functions to access and modify these data members.
class Car {
private:
string model;
int year;
public:
void setModel(string m) { model = m; }
string getModel() { return model; }
void setYear(int y) { year = y; }
int getYear() { return year; }
};
Accessors (getters) and mutators (setters) are used to read and modify the values of private variables.
Encapsulation can also be implemented using constructors to initialize data members at the time of object creation.
class Book {
private:
string title;
double price;
public:
Book(string t, double p) : title(t), price(p) {}
string getTitle() { return title; }
double getPrice() { return price; }
};
Constructors provide a way to set initial values for data members, ensuring that an object is always in a valid state.
Encapsulation helps in data hiding by restricting direct access to some of the object's components, which can prevent accidental or intentional misuse.
class Account {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
void withdraw(double amount) { if (amount <= balance) balance -= amount; }
double getBalance() { return balance; }
};
By hiding the data, encapsulation ensures that the internal representation of the object is protected from unintended interference.
Encapsulation facilitates abstraction by allowing the developer to expose only the relevant parts of the object and hide the unnecessary details.
class Student {
private:
int rollNumber;
string name;
public:
void setDetails(int r, string n) { rollNumber = r; name = n; }
void displayDetails() { cout << "Roll Number: " << rollNumber << ", Name: " << name << endl; }
};
Abstraction helps in reducing complexity by hiding the irrelevant details and showing only the essential features of an object.
Encapsulation can be effectively combined with inheritance to create a hierarchy of classes that share common functionality.
class Person {
protected:
string name;
int age;
public:
void setPersonDetails(string n, int a) { name = n; age = a; }
};
class Employee : public Person {
private:
int employeeID;
public:
void setEmployeeID(int id) { employeeID = id; }
void displayEmployeeDetails() { cout << "Name: " << name << ", Age: " << age << ", Employee ID: " << employeeID << endl; }
};
Inheritance promotes code reusability and extensibility, allowing new classes to inherit the properties and behaviors of existing classes.
Encapsulation works hand-in-hand with polymorphism, allowing objects to be treated as instances of their parent class, while maintaining specific behavior.
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override { cout << "Drawing Circle" << endl; }
};
class Square : public Shape {
public:
void draw() override { cout << "Drawing Square" << endl; }
};
Polymorphism allows for dynamic method binding, enabling a single function to handle different types of objects at runtime.
Encapsulation aids in designing robust interfaces by exposing only the necessary operations and keeping implementation details hidden.
class Interface {
public:
virtual void operation() = 0;
};
class Implementation : public Interface {
public:
void operation() override { cout << "Operation Implemented" << endl; }
};
Interfaces define a contract for what a class can do, without dictating how it should be done, promoting flexibility and scalability.
Encapsulation is widely used in software development to create applications that are easy to maintain, extend, and debug.
class ATM {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
void withdraw(double amount) { if (amount <= balance) balance -= amount; }
double checkBalance() { return balance; }
};
By encapsulating data and providing controlled access, developers can build systems that are robust and secure.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies