Followers

Thursday, November 9, 2023

Data encapsulation in C++

 

Data encapsulation is one of the fundamental principles of object-oriented programming and is often associated with C++. It refers to the bundling of data (attributes or variables) and the methods (functions) that operate on that data into a single unit called a class. Data encapsulation restricts direct access to some of an object's components, providing control over the object's internal state and behavior.

Here's an example of data encapsulation in C++:

cpp
#include <iostream> class Car { private: // Private data members (attributes) std::string make; std::string model; int year; public: // Public member functions (methods) // Getter methods to access private data std::string getMake() const { return make; } std::string getModel() const { return model; } int getYear() const { return year; } // Setter methods to modify private data void setMake(const std::string& newMake) { make = newMake; } void setModel(const std::string& newModel) { model = newModel; } void setYear(int newYear) { if (newYear >= 1886) { year = newYear; } else { std::cout << "Invalid year for a car!" << std::endl; } } }; int main() { Car myCar; myCar.setMake("Toyota"); myCar.setModel("Camry"); myCar.setYear(2022); std::cout << "My car is a " << myCar.getYear() << " " << myCar.getMake() << " " << myCar.getModel() << std::endl; return 0; }

In this example:

  1. 1. We have defined a Car class, which encapsulates data related to a car (make, model, and year). The data members (make, model, and year) are marked as private, meaning they can only be accessed and modified from within the class itself.

  2. 2. We provide public member functions (getters and setters) to access and modify the private data members. The getter methods (getMake, getModel, and getYear) allow external code to retrieve the values of the private attributes, while the setter methods (setMake, setModel, and setYear) allow external code to modify these attributes under controlled conditions.

  3. 3. In the main function, we create a Car object (myCar) and use its setter methods to set the car's make, model, and year. We then use the getter methods to retrieve and display this information.

Data encapsulation is essential in C++ because it provides a level of abstraction and data protection. It allows us to hide the internal implementation details of a class and provides controlled access to the data, ensuring that the data is valid and consistent. This helps maintain the integrity of the object's state and allows for easier maintenance and evolution of the code.

Pure virtual function in C++


A pure virtual function in C++ is a special type of virtual function that is declared in a base class but has no implementation there. Instead, it must be overridden in any derived class, making it mandatory for derived classes to provide their own implementation of the function. Pure virtual functions are also known as abstract functions, and they are declared using the "= 0" syntax.

Here's an example to illustrate pure virtual functions in C++:

cpp
#include <iostream> using namespace std; class Shape { public: virtual void draw() = 0; // Pure virtual function }; class Circle : public Shape { public: void draw() override { cout << "Drawing a circle" << endl; } }; class Square : public Shape { public: void draw() override { cout << "Drawing a square" << endl; } }; int main() { // Shape shape; // You cannot create an object of an abstract class Circle circle; Square square; // Polymorphic behavior through base class pointers Shape* shape1 = &circle; Shape* shape2 = &square; shape1->draw(); // Calls Circle's draw shape2->draw(); // Calls Square's draw return 0; }

In this example:

  1. 1. Shape is an abstract base class with a pure virtual function draw. It cannot be instantiated on its own because it lacks an implementation for draw.

  2. 2. Circle and Square are derived classes of Shape. They provide their own implementations of the draw function.

  3. 3. In the main function, you cannot create an object of the abstract class Shape because it has a pure virtual function. However, you can create objects of the derived classes, Circle and Square.

  4. 4. The key concept is the use of polymorphism. We can use base class pointers to point to objects of derived classes (shape1 and shape2). When calling the draw function through these pointers, the appropriate implementation in the derived class is invoked, demonstrating polymorphic behavior.

Pure virtual functions are commonly used in C++ to define interfaces, ensuring that derived classes provide specific functionality while allowing polymorphism to work effectively.

Concept of Abstract Base Class in C++


 

  1. Abstract Base Class: An abstract base class is a class that cannot be instantiated and is meant to be subclassed. It usually contains one or more pure virtual functions, which are declared with the virtual keyword and set to 0, indicating that they must be overridden in derived classes. Abstract base classes are used to define a common interface that derived classes must implement.

    Here's an example:

    cpp
    class Shape { public: virtual void draw() = 0; // Pure virtual function }; class Circle : public Shape { public: void draw() override { // Implement the drawing logic for a circle } }; class Square : public Shape { public: void draw() override { // Implement the drawing logic for a square } };

    In this example, Shape is an abstract base class with a pure virtual function draw. It cannot be instantiated on its own, but Circle and Square are derived classes that must provide an implementation for the draw function.

By using virtual functions and base classes, C++ allows us to create a hierarchy of classes with polymorphic behavior, making it easier to work with different types of objects in a unified way.