Abstraction
Abstraction in C++
Abstraction is one of the four fundamental principles of Object-Oriented Programming (OOP), along with encapsulation, inheritance, and polymorphism. In this tutorial, we will discuss what Abstraction is and how it is implemented in C++.
Introduction to Abstraction
Abstraction, as the name suggests, is the process of hiding complex details and showing only the essential features of the object or concept. In other words, it deals with the outside view of an object (interface). Abstraction helps to reduce complexity by separating an object’s implementation from its behavior or attributes.
Why Use Abstraction?
Abstraction is used for several reasons:
Simplifying complex systems: By abstracting away low-level details and complexity, you can focus on higher-level design and functionality.
Increasing efficiency: By abstracting away details, you can avoid repeating code.
Security: By hiding internal implementation details, you can prevent outside code from affecting internal object state.
Implementing Abstraction in C++
In C++, we achieve abstraction using classes and objects, and interfaces.
Class in C++
A class is a user-defined data type that encapsulates data and functions that operate on that data. The data and functions within a class are called members of the class. Here is an example of a class in C++.
class Car {
private:
string color;
string model;
int year;
public:
void setColor(string c) {
color = c;
}
void setModel(string m) {
model = m;
}
void setYear(int y) {
year = y;
}
string getColor() {
return color;
}
string getModel() {
return model;
}
int getYear() {
return year;
}
};
In the above example, color
, model
and year
are data members, and setColor
, setModel
, setYear
, getColor
, getModel
, and getYear
are function members of the class Car
.
Abstraction using Classes
In the context of classes, abstraction means exposing only the required features of a class and hiding the other internal details. This is achieved by using access modifiers private
, public
, and protected
.
In the Car
class example above, color, model and year are private data members. This means they cannot be accessed directly by an object of the class. However, we have set up public methods (getters and setters) to change or access these private members, providing a controlled way to access the private data.
Abstraction using Interfaces
An interface in C++ is like a blueprint of a class. An interface contains only the declaration of methods, properties, events, or indexers. The interface creates a contract for classes to implement. If a class implements an interface, it inherits the methods, properties, and other members that an interface declares.
In C++, we use Abstract Base Classes (ABC) to implement interfaces. An Abstract Base Class is a class that has at least one pure virtual function. Here is an example:
class Shape {
public:
virtual void draw() = 0; // Pure virtual function makes this class Abstract class.
};
In this case, any class that inherits from Shape
must implement the draw
function.
Conclusion
Abstraction is a vital principle of Object-Oriented Programming in C++. It simplifies complex systems by exposing only necessary details and hiding the rest. Abstraction in C++ can be achieved through classes and interfaces. Classes achieve abstraction by exposing public methods for private data. Interfaces create a contract for classes to implement, ensuring that certain methods are present in the classes.