Lesson 12
Object Oriented Programming Conclusion
In this module, the basic concept of
encapsulation in C++'s implementation of object-oriented programming.
You learned:
- Why encapsulation is central to object-oriented programming
- What classes and objects are
- How to write member functions as part of an abstract data type
- How to limit access to an ADT's member data and functions
- How a class differs from a struct
Encapsulation is a core concept in C++ and Object-Oriented Programming (OOP). Here's a breakdown of its importance and implementation in C++:
- Why encapsulation is central to object-oriented programming:
Encapsulation ensures that an object's internal state (data) and behavior (functions) are hidden from the outside world. It allows for data protection by controlling access to the internal implementation while exposing a clean, controlled interface. This separation of concerns increases modularity, maintainability, and security in OOP design.
- What classes and objects are:
A class is a blueprint or template for creating objects in C++. It defines the properties (data members) and behaviors (member functions) that the objects (instances of the class) will have. An object is an instance of a class, which represents a specific entity with defined data and functionality. For example, a class
Car
might define properties like speed
and methods like accelerate()
, and an object myCar
would be a specific instance of this class.
- How to write member functions as part of an abstract data type:
In C++, member functions are written within a class definition and act on the data members of that class. These functions are the methods by which the internal state of an object can be manipulated. For example:
class Car {
private:
int speed;
public:
void setSpeed(int s) {
speed = s;
}
int getSpeed() {
return speed;
}
};
Here, `setSpeed` and `getSpeed` are member functions that are part of the abstract data type (ADT) `Car` and are responsible for modifying and accessing the object's data.
- How to limit access to an ADT's member data and functions:
C++ provides access specifiers—
private
, public
, and protected
—to control how class members are accessed. Typically, member data is declared private
to prevent direct access from outside the class, while member functions that provide controlled access to this data are declared public
. This ensures encapsulation by allowing the object to maintain control over its data.
class Car {
private:
int speed; // This is private, can't be accessed directly
public:
void setSpeed(int s) { // Public function controls access
speed = s;
}
int getSpeed() { // Public function allows controlled access
return speed;
}
};
- How a class differs from a struct:
In C++, a struct is similar to a class, with one key difference: the default access level for members of a struct is
public
, whereas in a class it is private
. This means that unless specified otherwise, data in a struct can be accessed directly, which is less aligned with the principles of encapsulation.
struct CarStruct {
int speed; // Default access is public
};
class CarClass {
private:
int speed; // Default access is private
public:
void setSpeed(int s) { speed = s; }
int getSpeed() { return speed; }
};
In practice, classes are generally used for encapsulating behavior and data, while structs are often used for simple data containers where access control is not as important.
Encapsulation Review
Encapsulation is a technique that extends the concept of abstraction from a strictly data phenomena to both data and functions.
If you think of the
data abstraction used in C which is the
structure
, then it is easy to grasp the idea that a class encompasses what a structure did and then extends the concept to include the binding of functions into the single entity.
Abstraction is another object-oriented concept related to encapsulation and information hiding. Simply put, abstraction means dealing with the level of detail that is most appropriate to a given task. It is the process of extracting a public interface from the inner details.
For example, the driver of a car needs to interact with
- steering,
- gas pedal, and
- brakes.
The workings of the motor, drive train, and brake subsystem do not matter to the driver. A mechanic, on the other hand, works at a different level of abstraction, tuning the engine and bleeding the breaks Abstraction is the process of encapsulating information with separate public and private interfaces. The private interfaces can be subject to information hiding. The important lesson to take from all these definitions is to make our models understandable to other objects that have to interact with them. This means paying careful attention to small details. Ensure methods and properties have sensible names. When analyzing a system, objects typically represent nouns in the original problem, while methods are normally verbs. Attributes can often be picked up as adjectives, although if the attribute refers to another object that is part of the current object, it will still likely be a noun. Name classes, attributes, and methods accordingly.
The C++ Programming Language
C++ Programs are composed of two Fundamental Elements
All C++ programs are composed of the following two fundamental elements:
- Program statements (code): This is the part of a program that performs actions and they are called functions.
- Program data: The data is the information of the program which affected by the program functions.
Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. For example:
OOP Encapsulation Quiz
Click the Quiz link below to take a multiple-choice quiz covering the topics presented in this module.
OOP Encapsulation - Quiz