4 Pillars
4 Pillars
4 Pillars
1. Encapsulation :
Encapsulation is one of the fundamental concept of OOP's. It refers
to the bundling (combining) of data with the methods (member functions that
operate on that data) into a single entity (like wrapping of data and methods
into a capsule) called an Object. It is used to hide the values or state of a
structured data object inside a class, preventing unauthorized parties, direct
access with them. Publicly accessible methods are generally provided in the
class to access the values, and other client classes call these methods to retrive
and modify the values within the object.
We generally call the functions that are declared in c++ are called
as Member functions. But in some Object Oriented languages (OO's) we call
them as Methods. Also the data members are called Attributes or Instance
variables.
Take a look with an example :
#include<iostream>
using namespace std;
class s
{
public:
int count;
s()
// constructor
{
count=0;
}
int disp()
{
return count;
}
void addcount(int a)
{
count=count+a;
}
};
int main()
{
s obj;
obj.addcount(1);
obj.addcount(4);
obj.addcount(9);
cout<<obj.count;
//
output : 14
return 0;
}
In the above example u can observe that all tha data members and
methods are binded into a object called obj. In Encapsulation we can declare
data members as private, protected, or public. So we can directly access the
WWW.COMSCIGUIDE.BLOGSPOT.COM
2.Data Hiding :
The concept of encapsulation shows that a non -member function
cannot access an object's private or protected data. This has leads to the new
concept of data hiding. Data Hiding is a technique specifically used in object
oriented programming(OOP) to hide the internal object details (data members
or methods) from being accessible to outside users and unauthorised parties.
The Private access modifier was introduced to provide that protection. Thus it
keeps safe both the data and methods from outside interference and misuse. In
data hiding, the data members must be private. The programmer must
For references : http://comsciguide.blogspot.com/
explicitly define a get or set method to allow another object to read or modify
these values.
For example :
class s
{
int count;
public:
s()
// constructor
{
count=0;
}
int disp()
{
return count;
}
void addcount(int a)
{
count=count+a;
}
};
int main()
{
s obj;
obj.addcount(1);
obj.addcount(4);
obj.addcount(9);
cout<<obj.disp();
return 0;
}
WWW.COMSCIGUIDE.BLOGSPOT.COM
3.Inheritance:
Reusability is yet another important feature of OOP's concept. It is
always nice, if we could reuse something instead of creating the same all over
again. For instance, the reuse of a class that has already tested and used many
times can save the effort of developing and testing it again.
For references : http://comsciguide.blogspot.com/
Derived class.
BASE CLASS
FEATURE A
FEATURE B
FEATURE C
DERIVED CLASS
FEATURE A
FEATURE B
FEATURE C
FEATURE D
Here is an example :
#include<iostream>
using namespace std;
class e
{
public:
int a;
void b()
{
a=10;
}
};
class c : public e
// public inheritance
{
public:
void d()
{
cout<<a<<endl;
}
};
int main()
{
c ab;
ab.b();
ab.d();
}
WWW.COMSCIGUIDE.BLOGSPOT.COM
4.POLYMORPHISM:
Polymorphism -- one interface, multiple methods.
Polymorphism occurs when there is a hierarchy of classes and they
are related by inheritance. Generally, from the Greek meaning Polymorphism
is the ability to appear in many forms (having multiple forms) and to redefine
methods for derived classes.
A real-world example of polymorphism is a thermostat. No matter
what type of furnace your house has(gas, oil, electric, etc), the thermostat
works in the same way. In this case, the thermostat (which is the interface) is
the same, no matter what type of furnace (method) you have. For example, if
For references : http://comsciguide.blogspot.com/
WWW.COMSCIGUIDE.BLOGSPOT.COM