Ads 468x60px

Monday 14 January 2013

Features of OOP

Let us look at the features, 
We will make an attempt to understand the above from a logical point of view. 

Encapsulation
The example of a TV set is often taken to illustrate objects and characteristics of OOP. The TV receiver is totally encapsulated. We cannot see the components inside or meddle with them. However, we can use the TV set easily. We can switch it on and select channels with the external interfaces provided in the receiver. This is the concept of encapsulation of objects in an OOP. 
Wrapping together data and functions creates the objects in OOP. They are bound together. This represents encapsulation in OOP. We can use the encapsulated objects through the designated interfaces only. Thus, the inner parts of the program are sealed or encapsulated to protect from accidental tampering. This feature is not available in the conventional procedure oriented programming languages where data can be corrupted since it is easily accessible. In C++, like other object oriented programming languages such as Java and C#, encapsulation is achieved through what is known as classes. 

A Class is a blueprint for making a particular kind of object, It defines the specifications for construction an object with data and function. It is generally specifies the private working of objects and their public interfaces. The data, known as data members, defines the state of the proposed object and function of its behavior. 

The class is used to declare two types of members:
1. Data 
2. Functions

Data are nothing but declaration of variables for holding data, Functions, called member functions, contain sequence of instructions that operate on the data. An object is nothing but a variable of type class. it is a self contained computing entity with its own data and functions. This means that an object will have its own copy of the variables. However, the functions being common to all the objects do not need to be kept in each object. 

Note carefully that a class can give rise to a number of objects, but is not an object on its own. It is only a structure or blueprint which helps in creating replicas with common structures, but different characteristics. This means, two objects of a class with different names will have same variable names but with different values i.e., they have the same data type but different data. In some special cases, two objects can also have same data. A class is a framework for proper encapsulation of objects. The data members of the objects can be accessed only through the interface available in public. 

Syntax for declaring a class
class class_name
{
   data's;
   functions;
};
Suppose we need to create a class for student the we will write
class student
{
  char name[20];
  int roll_no;
  char sec;
  
  public:
  void student_details()
  {
    cout<<"Enter name:\t";
    gets(name);
    cout<<"Enter Roll Number:\t";
    cin>>roll_no.;
    cout<<"Enter Section:\t";
    cin>>sec;
  }
};
This is a class name student which will store a student's name, roll number and section. Now the function is declared under public, because we need a main function for the execution of program and if the function is private then main function has not the permission to access the function so your program will give an error that function is inaccessible so we need to declare the function in public. Now for accessing anything under a class you need an object of that class and that is called encapsulation. When we create object of a class all the data of that class is encapsulated under a single object called encapsulation. 
class student
{
  char name[20];
  int roll_no;
  char sec;
  
  public:
  void student_details()
  {
    cout<<"Enter name:\t";
    gets(name);
    cout<<"Enter Roll Number:\t";
    cin>>roll_no.;
    cout<<"Enter Section:\t";
    cin>>sec;
  }
};

void main()
{
  student obj;
  obj.student_details():
}
The syntax for creating an object is
class_name object_name;
As we did above, and for accessing anything from class the syntax is
object_of_that_class.element_of_that_class
In object part we have to write the object name which we created for that class and after than we need to place a dot ( . ) and then have to write the element which we want to access, the element can be anything from that class e.g. functions and variables. The element will only be accessible when public. 

Abstraction
Data abstraction refers to, providing only essential information to the outside word and hiding their background details i.e. to represent the needed information in program without presenting the details.
For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.

Data Hiding
Related to the idea of encapsulation is the concept of data hiding. Encapsulation can hide data from classes and functions in other classes. In the example of the class Transport, we can access the three data members through the member function set_values(). Although there may be many other attributes and data elements in the class Transport, these are all hidden from view. This makes programs more reliable since declaring a specific interface, such as public or private, to an object prevents inadvertent access to data in ways that it was not designed for. In C++, access to an object and its encapsulated data and functions is treated very carefully using the keywords private, protected and public. The programmer has the facility to decide the access specifications for data objects and functions as being private, protected and public while defining a class. Only when the declaration is public can other functions and objects access the object and its components without question on the other hand if the declaration is private there is no possibility of such access. When the declaration given is protected, access to data and functions in a class by others is not as free as when it is public, not as restricted as when it is private. You can declare one base class as being derived from another, which will be discussed shortly. 

Inheritance 
A software object does not exist by itself; it is always an instance of a class. A class gives a blueprint for building a software object and can inherit the features of another class and add its own modifications. Inheritance is similar to human beings. A child can inherit his/her father's property and add/acquire more properties; he/she can modify the inherited property; or dispose of a property. In a similar manner, a new class can inherit its properties from another existing class. 
A class can have a child, which means one class can be derived from another. The original or parent class is known as the base class and the child class is known as the derived class. 

Polymorphism
Polymorphism is a useful concept in OOP languages, In simple terms it means one name, many duties. It provides a common interface to carry out similar tasks. In other words, a common interface is created for accessing related objects. This facilitates the objects to become interchangeable black boxes. hence they can be used in other programs as well. Therefore, polymorphism enables reuse of objects built with a common interface. 

Overloading
The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type it is said to be overloaded.
Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!

 
Related Posts Plugin for WordPress, Blogger...