Académique Documents
Professionnel Documents
Culture Documents
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.
Takealookwithanexample:
#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
For references : http://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
explicitly define a get or set method to allow another object to read or modify
these values.
For references : http://comsciguide.blogspot.com/
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;
}
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.
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
Derived class.
BASE CLASS
FEATURE A
FEATURE B
FEATURE C
DERIVED CLASS
FEATURE A
FEATURE B
FEATURE C
FEATURE D
Hereisanexample:
#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();
}
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
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.
For references : http://comsciguide.blogspot.com/