0% found this document useful (0 votes)
9 views13 pages

Encapsulation

The document explains the concepts of encapsulation and inheritance in C++. Encapsulation involves hiding sensitive data by declaring class attributes as private and using public methods to access them, enhancing data security and control. Inheritance allows a derived class to inherit attributes and methods from a base class, with different access modes (public, private, protected) affecting the accessibility of inherited members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Encapsulation

The document explains the concepts of encapsulation and inheritance in C++. Encapsulation involves hiding sensitive data by declaring class attributes as private and using public methods to access them, enhancing data security and control. Inheritance allows a derived class to inherit attributes and methods from a base class, with different access modes (public, private, protected) affecting the accessibility of inherited members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ENCAPSULATION

ENCAPSULATION
The meaning of Encapsulation is to make sure that "sensitive" data is hidden from users. To achieve this, you must
declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to
read or modify the value of a private member, you can provide public get and set methods.
Access Private Members
To access a private attribute, use public "get" and "set" methods:
Why Encapsulation?
• It is considered good practice to declare your class attributes as private (as often as you can). Encapsulation
ensures better control of your data because you (or others) can change one part of the code without
affecting other parts
• Increased security of data
EXAMPLE
Example explained
The salary attribute is private, which have restricted access.

The public setSalary() method takes a parameter (s) and assigns


it to the salary attribute (salary = s).

The public getSalary() method returns the value of the private


salary attribute.

Inside main(), we create an object of the Employee class. Now


we can use the setSalary() method to set the value of the
private attribute to 50000. Then we call the getSalary() method
on the object to return the value.
INHERITANCE
INHERITANCE
In C++, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
• derived class (child) - the class that inherits from another class
• base class (parent) - the class being inherited from
The derived class inherits the features from the base class and can have additional features of its own.
For example,
Here, the Dog class is derived from the Animal class.
Since Dog is derived from Animal, members of Animal
are accessible to Dog.
To inherit from a class, use the: symbol.
INHERITANCE

Notice the use of the keyword public while inheriting Dog


from Animal.

We can also use the keywords private and protected instead of


public. We will learn about the differences between using
private, public, and protected later in this tutorial.
is-a relationship
Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present
between the two classes.
Here are some examples:
• A car is a vehicle.
• Orange is a fruit.
• A surgeon is a doctor.
• A dog is an animal
EXAMPLE 1: SIMPLE EXAMPLE OF C++ INHERITANCE
Here, dog1 (the object of the
derived class Dog) can access
members of the base class
Animal. It's because Dog is
inherited from Animal
INHERITANCE

• C++ protected Members


The access modifier protected is especially relevant when it comes to C++ inheritance.
Like private members, protected members are inaccessible outside of the class. However,
they can be accessed by derived classes and friend classes/functions. We need protected
members if we want to hide the data of a class, but still want that data to be inherited by its
derived classes.
Protected Example of C++ Inheritance

Here, the variable type is protected and is thus accessible from the derived class Dog. We can see
this as we have initialized the type in the Dog class using the function setType().
EXAMPLE

• On the other hand, the private variable color cannot be initialized in Dog.

• Also, since the protected keyword hides data, we cannot access type directly from
an object of Dog or Animal class

.
ACCESS MODES IN C++ INHERITANCE
• In our previous tutorials, we have learned about C++ access specifiers such as public,
private, and protected. So far, we have used the public keyword to inherit a class from a
previously existing base class. However, we can also use the private and protected
keywords to inherit classes.
• For example,
ACCESS MODES IN C++ INHERITANCE
• The various ways we can derive classes are known as access modes. These access modes have
the following effect:
• public: If a derived class is declared in public mode, then the members of the base class are
inherited by the derived class just as they are.
• private: In this case, all the members of the base class become private members in the derived
class.
• protected: The public members of the base class become protected members in the derived
class.
The private members of the base class are always private in the derived class.

You might also like