C++ Object Oriented Programming


C++ Object Oriented Programming

    C++ Object Oriented Programming 


Implementing OOP concept in C++

A class binds together data and its associated functions under one unit thereby enforcing encapsulation as encapsulation means wrapping of data and associated functions together into a single unit.
A class groups its members into three sections: private, protected and public.


Data hiding

The private and protected members remain hidden from outside world. Thus it enforces data-hiding.



Abstraction

The outside world is given only the essential and necessary information through public members, rest of things remain hidden, this is nothing but abstraction i.e. to represent the needed information in program without presenting the detail.


A class is defined as-
class <class name>{ private:
                                            //hidden data members (and)/methods here
                                   protected:
                                           //unimportant implementation detail here
                                   public:
                                         //exposed important detail here
                              }


Inheritance

Inheritance is the process of forming a new class from an existing class that is,the existing class called as base class, new class is formed called derived class.
This is very important concept of object oriented programming since this feature helps to reduce the code size.


Inheritance is implemented in C++ by specifying the name of the (base) class from which the class being defined (the derived class)
class <derived class name>:<base class name>
{
Derived class own features
}
C++ Object Oriented Programming
                               General Structure Of Class



Abstract class and concrete class

In C++ an abstract class is one which defines an interface, but does not provide implementations for all its member functions .an abstract class is a base class.
The derived class is expected to provide implementations for all the functions are called concrete class.


Ex. a shape is a abstract class and other clad like circle class, rectangle class is concrete class.



Polymorphism

The ability to use an operator or function in different ways, in otherwise giving different meaning to the operators or function is called polymorphism.


Polymorphism allows us that one interface to be used with different situations.
                 C++ implements polymorphism through virtual function, overloaded function and overloaded operators.


The term ‘overloading’ means a name having two or more distinct meaning.
Ex.    float area(float a)
          {
               return a*a;
           }
         float area (float a,float b)
           {
               return a*b;
           }

It is compilers job to select specific action.



Modularity

The act of participating a program into individual module (components) is called modularity.


                A module is a separate unit in itself. It can be compiled separately through it has connections with other to achieve the program's goal.
Ex. A music system comprises of speakers, cassette-player, record-player, CD-player, tuner etc.


In object oriented languages, classes and objects form the logical structure of system. Especially in large applications ,in which we may have many hundreds of classes, the use of modules is essential to help manage complexity. 


 C++ Object Oriented Programming 


Need for Function Overloading

Object has characteristics and associate behavior. Thus object does different behavior in different situation.


It is a class that implements OOP in practice. Therefore, in order to simulate real-world objects in programming it is necessary to have function overloading.


Ex.    
void see_day()
  {
      cout<<”Can see throughout Daylight”;
}
void see_night()
{
 cout<<”Cannot see through darkness”;
}



Function Overloading

A function have same name but have different parameter list i.e. varying in type and number, is known as function overloading.


Ex. float area (float radius)                                                //circle
      {
         return 3.14*radius*radius;
      }
     float area (float length, float breath)                         //rectangle                 
      {
        return length*breath;
      }
   float area(float s1, float s2, float s3)                          //triangle
     {
         float s=(s1+s2+s3)/3;
        float ar=sqrt(s*(s-s1)*(s-s2)*(s-s3));
        return ar;
    }



Default arguments Versus Overloading

Using default arguments looks like overloading, because the function may be called with an optional number of arguments.


Ex.   function declaration  [ float amount(float principal, int time=2,float rate=0.08);

calling this function with different types-
cout<<amount(300);                         //use default value for time and rate
cout<<amount(300,4);                    //use default value for rate
cout<<amount(2500,5,0.12);        //don’t use any default value




 C++ Object Oriented Programming 

·        Program to illustrate working of default arguments. Calculate interest by making use of default argument.

#include<iostream.h>
#include<stdlib.h>
void amount(float p,int t=2,float r=0.08);
void amount (float p,int t,float r)
{
 cout<<”\n Principal:”<<p;
 cout<<”\n time:”<<t<<”years”;
 cout<<”\n rate:”<<r;
 cout<<”Interest:=”<<((p*r*t)/100);
}
void main()
{
 cout<<”Case 1:”;
      amount(2000);
 cout<<”Case 2:”;
     amount(2800,3);
 cout<<”Case 3:”;
     amount(2500,4,0.12);
 return 0;
}



Steps involved in finding the best match

A call to an overloaded function is resolved to a particular instance of function through a process known as argument matching.


To find the best possible match compiler follows the following steps-


1.      Search of an Exact match
void func(int);                                 //overloaded function
void func(double);
func(0);                                         //exact match func(int)
                                                        because ‘o’ is of type int
2.     A match through promotion
void func(int);                           //overloaded function
void func(float);
func(‘c’);                                   //matches func(int)
‘c’ is of type char and thus promoted to type int.

3.    A match through application of  standard C++ conversion rule-
void func(char);
void func(double);
func(471);      //through conversion ,it matches func(double)
                   
4.    A match through application of a user-defined conversion.



 In this post we are discussing about Implementing OOP concept in C++
 which is very important topic to understand C++. In the next post we will discuss about Classes and Objects.  




Post a Comment

0 Comments