Você está na página 1de 10

The four pillars of object oriented programming development :

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:

For references : http://comsciguide.blogspot.com/

int count;

// data member of the class

s()

// constructor

{
count=0;

// data member initialization

}
int disp()

// method of the same class

{
return count;
}
void addcount(int a)

//method of the same class

{
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

For references : http://comsciguide.blogspot.com/

data members (here count).


Features and Advantages of concept of Encapsulation :

Makes maintenance of program easier .


Improves the understandability of the program.
As the data members and functions are bundled inside the class, human
errors are reduced.
Encapsulation is alone a powerful feature that leads to information
hiding, abstract data type and friend functions.

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;

// data member of the class


// default members are private

public:
s()

// constructor

{
count=0;

// data member initialization

}
int disp()

// method of the same class

{
return count;
}
void addcount(int a)

//method of the same class

{
count=count+a;
}
};
int main()
{
s obj;
obj.addcount(1);
obj.addcount(4);
obj.addcount(9);
cout<<obj.disp();

For references : http://comsciguide.blogspot.com/

return 0;
}

Here the data member count is private. So u can't access it directly.


Instead u can access it, by the methods of the same class. Here It is accessed by
the disp method .

In some cases of two classes, the programmer might require an unrelated


function to operate on an object of two classes. The programmer then
able to utilize the concept of friend functions.
It enhances the security and less data complexity
The focus of data encapsulation is on the data inside the capsule while
that data hiding is concerned with restrictions and allowance in terms of
access and use.

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/

C++ supports the concept of reusability. Once a class has written


and tested, it can be adapted by other programmers to suit their requirements.
This is done by creating new classes, and reusing the existing properties. The
mechanism of deriving a new class from old one is called Inheritance. The old
class is referred as Base class. And the

new one created is called as

Derived class.
BASE CLASS

FEATURE A
FEATURE B
FEATURE C

DERIVED FROM BASE CLASS

DERIVED CLASS

FEATURE A
FEATURE B
FEATURE C
FEATURE D

For references : http://comsciguide.blogspot.com/

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();
}

For references : http://comsciguide.blogspot.com/

Here a is declared in the base class e and is inherited in


the class c so that we need not to it declare again.

Reusing existing code saves time and increases a program's reliability.


An important result of reusability is the ease of distributing class
libraries. A programmer can use a class created by another person or
company, and without modifying it, derive other classes from it that are
suited to their requirements.

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/

you want a 70-degree temperature, you set the thermostat to 70 degrees. It


doesn't matter what t ype of furnace actually provides the heat.
This same principle can also apply to programming. For example,
you might have a program that defines three different types of stacks. One stack
is used for integer values, one for character values, and one for floating-point
values. You can define one set of names, push( ) and pop( ) that can be used for
all three stacks. In your program, you will create three specific versions of
these functions, one for each type of stack, but names of the functions should be
same. The compiler will automatically select the right function based upon the
data being stored. Thus, the interface to a stack -- the functions push( ) and
pop( ), are the same no matter which type of stack is being used. The individual
versions of these functions define the specific implementations (methods) for
each type of data.
Polymorphism reduces complexity by allowing the same interface to be
used to access class of actions. It is the compiler's job to select the
specific action (method) to each situation.
Also see :
1.Programs those work in C but not in C++
2.Difference between malloc(),calloc(),realloc()
3.Dynamic memory allocation (Stack vs Heap)
4.3 ways to solve recurrence relations

For references : http://comsciguide.blogspot.com/

WWW.COMSCIGUIDE.BLOGSPOT.COM

For references : http://comsciguide.blogspot.com/

Você também pode gostar