OOP Lec2
OOP Lec2
10-10-2024
Topics covered
Data encapsulation
Constructors
Object and Class
Together they define the properties and behavior of the objects in a Class.
Data Encapsulation
• The wrapping up of data and information in a single unit.
• Bundling the data members and its related functions togather into a single
entity, is known as encapsulation.
• Similar to packing data and functions together into a "box," which we call a
class, and controlling who can open the box and how they can interact with it.
• Encapsulation hides the complex details of how something works and gives you
an easy way to interact with it.
Data Encapsulation
• Encapsulation ensures that only member functions of a class can access its data, which
results in data hiding.
• The member functions of the encapsulated class must only use member variables.
• Aimed to treat the class as a unique entity as well as hiding complex data using access
modifiers.
• Improves readability, maintainability, and security.
Public Private
Access modifiers
• Work on the principle of “data hiding”.
• Used to impose certain restrictions on the members of class.
• Limit the accessibility of the class members thus, it is not fesiable to directly
access them from the outside functions.
• Three generic types of access modifiers are;
i. Public
ii. Private
iii. Protected
Getter, Setter methods
Encapsulation conceals the inner workings of a class and one can interact with it through
methods provided by the class.
The data is hidden, but others can interact with it using these special methods.
1. Getter Method:
A way for others to look at your data, but not change it. "View-only" access.
2. Setter Method:
A controlled way to change the data. It often comes with checks to make sure only valid changes are
made.
Data Encapsulation
Constructors
• A special type of function in programming that is used to create objects.
• Set the initial values or properties of an object when it is created.
• Constructor is called by the compiler whenever the object of the class is created.
• Like functions, a constructor can take arguments that can aid in the initialization.
• It differs from a method in that it has no explicit return type, it is not inherited and
has different rules for scope modifiers.
• They are mostly accessed publically.
• Constructors often have the same name as the declaring class.
Constructors
• Enables an object to initialize itself at the time of its creation without the need
to make a separate call to the instance method.
• Just like functions, constructors can also be defined outside the class.
• Provides several benefits w.r.t code organization, memory management, and
initialization.
• Enforce encapsulation by setting the data members of the class to private and
providing public constructors to create and initialize the object.
Constructors
Methods of defining constructors:
Constructors
Constructors
Output:
Types of Constructors
• Has four generic types.
1. Default constructor:
A default constructor doesn’t take any
argument. It has no parameters. It is also
called a zero-argument constructor.
2. Parameterized constructor:
Parameterized constructors make it
possible to pass arguments to constructors.
These arguments help initialize an object
when it is created.
Types of Constructors
3. Copy Constructor:
A copy constructor initializes an object
using another object of the same class. Takes
a reference to an object of the same class as
an argument.
4. Move Constructor:
Like a copy constructor, but instead of
copying the object in the new memory, it
transfer the ownership of the already created
object to the new object without creating
extra copies.