It’s time to expand our minds again! Today, I bring you two major features of classes in C++: encapsulation and inheritance.
1. Encapsulation
1.1 What is Encapsulation
C++ is based on object-oriented programming, which has three main features: encapsulation, inheritance, and polymorphism.
C++ combines an object’s properties (member variables) and behaviors (functions) through classes, making it easier for developers to understand and recognize them. It essentially encapsulates all resources belonging to the object together; by using access specifiers, some functionalities can be selectively exposed for interaction with other objects, while external users do not need to know the internal implementation details. This is about organically combining data and the methods that operate on the data, hiding the object’s properties and implementation details, and only exposing interfaces for interaction with the object.
1.2 Advantages of Encapsulation
①. Data hiding: Encapsulation can hide the implementation details of a class, preventing external access and modification of class data.
②. Data protection: Encapsulation can protect the data of a class, allowing access and operations only through the public interface of the class, thereby enhancing data security.
③. Code reuse: Encapsulation can organize related data and operations together, facilitating code reuse and maintenance.
1.3 Example of C++ Encapsulation
In C++, an encapsulation example usually refers to defining a class and creating an object of that class.
Here is a simple example:
#include <iostream>
using namespace std;
// Define a class
class A {
public:
void Display() const {
cout << "Hello, World!" << endl;
}
};
// Create an instance of the class in the main function
int main() {
A a; // Create object
a.Display(); // Call object's method
return 0;
}
2. Inheritance
2.1 What is Inheritance
In an inheritance relationship, a subclass can obtain the properties and methods of its parent class and can add its own specific functionalities or modify the behavior of the parent class. A subclass can inherit the public members (public) of the parent class, but cannot inherit the private members (private), which can only be accessed within the parent class. What is inherited is the commonality, which everyone has. By adding and modifying, it becomes one’s individuality, improving the utilization of code and enhancing the efficiency of your project development.
2.2 Advantages of Inheritance
①. Code reuse: Through inheritance, a subclass can directly use the properties and methods already defined in the parent class, avoiding the need to rewrite the same code, thus improving code reusability.
②. Organizing class hierarchy: Through inheritance, classes can be organized into a hierarchical structure, forming relationships between parent and child classes. This can better organize and manage code, making the code structure clearer and easier to understand.
③. Polymorphism: Inheritance is the foundation for achieving polymorphism. Through inheritance, subclasses can override the methods of the parent class to implement different behaviors. Thus, when using a variable of the parent class type, the corresponding method can be called based on the actual object type, achieving different processing logic.
2.3 Types of Inheritance
①. Public inheritance: The derived class inherits the public and protected members of the base class, while private members cannot be inherited.
②. Private inheritance: The derived class inherits the public and protected members of the base class, while private members cannot be inherited, but exist as private members in the derived class.
③. Protected inheritance: The derived class inherits the public and protected members of the base class, while private members cannot be inherited, but exist as protected members in the derived class.
Public inheritance is the most common and default inheritance method, allowing the derived class to fully access the public member functions and data of the base class. The derived class can also add its own data members and member functions, thereby extending the functionality of the base class.
2.4 Basic Example Code
#include <iostream>
using namespace std;
// Parent class
class A {
public:
int base_var;
A():base_var(10) {
cout << "Class A's constructor is called: " << endl;
}
void show(){
cout << "Class A's display function is called: " << endl;
cout << "base_var: " << base_var << endl;
}
};
// Subclass
class B : public A{
public:
int sub_var;
B():sub_var(20) {
cout << "Class B's constructor is called: " << endl;
}
void show(){
cout << "Class B's display function is called: " << endl;
cout << "sub_var: " << sub_var << endl;
cout << "base_var: " << base_var << endl;
}
};
int main() {
B b;
b.show();
b.A::show();
return 0;
}
The C++ series is continuously updated! Everyone is welcome to message me actively, and I will answer you as soon as possible.