Object Oriented Programming
A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure.
In this way, the data structure becomes an object that includes both data and functions.
Characteristics of Object Oriented Programming
Data encapsulation - Encapsulation is the process of combining data and functions into a single unit called class.
Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions existing inside the class.
Data hiding - Data hiding is the implementation details of a class that are hidden from the user.A class groups its members into three sections:
Private, protected and public
In which private and protected are hidden from the outside world
For example –
class Temp
{
private:
int m; // m is only accessible within the class
protected:
int n // n is only accessible within the class and
// to the subclass of Temp
public:
int p; // p is accessible inside and outside the class
Temp() // constructor
{ }
};
Polymorphism – it is a property by which a message can be sent to the objects of different classes and each object will respond in different way.
Or
It is the ability for a message or data to be processed in more than one form. An operation may exhibit different
behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing function overloading.
For example –
class Temp
{
public:
Temp()
{ }
int area(int a)
{
return a*a;
}
int area( int a,int b); //Overloaded function
{
return a*b;
}
};
Inheritance – It has the property of a class to inherit properties from other class .
Or
Inheritance is the process by which objects can acquire the properties of objects of other class.
In OOP, inheritance provides reusability, like, adding additional features to an existing class without modifying it.
This is achieved by deriving new class from the existing one. The new class will have combined features of both the classes.
For example-
class A
{
Public :
int a ,b;
void show ()
{
cout<<a<<b;
}
};
class B: private A //Use of Inheritance
{
int c;
void dispdata ()
{
cout<<c;
}
};
No comments:
Post a Comment