Saturday, October 28, 2017

Object Oriented Programming



History of C++


 History of c++ OR What is c++?
o      C++ is object oriented language. It was found in year 1980.
o      It was made by Bjarne Stroustrup at AT & T Bell lab, NJ, USA.
o      C++ contains power of c language as well as it supports object oriented programming features.
o      So c++ is extended c language with classes, so initially c++ was  called as ‘C with classes’.
o      However later in 1983 name was changed to c++.
o      The idea of c++ language is comes from ++ operator of c language, so named given as c++ which suggesting that it is increment version of c.
o      During the early 1990s the language underwent a number of improvements and changes.
o      These changes were made fixed by ANSI / ISO standards committee in November 1997.  It also added several new features to it.
o      C++ is a super set of c. Therefore all C programs are also C++ programs.
o      However there are some minor difference between c and c++.
o      C++ adds some good features that c does not have like classes, inheritance, function overloading, operator overloading etc.
o      C provides us top – down programming while C++ provides us bottom – up programming.

Difference between POP & OOP


No
Procedure oriented programming (POP)
Object oriented programming (OOP)
1
POP emphasis on algorithms (procedure)
OOP emphasis on data rather than procedure.
2
Large programs are divided into smaller programs known as functions.
Programs are divided into objects.
3
They have not facility to hide data.
They have facility to hide data from outside world.
4
Function can transfer data from one function to another and one form to another form.
Objects can communication with each other using functions.
5
It does not have powerful features like operator overloading, inheritance etc.
It contains powerful features like operator overloading, inheritance.
6
For design program it uses top-down approach.
For design program it uses bottom-up approach.
7
Examples: C, COBOL, FORTRAN etc.
Examples: C++

Objects

OBJECT: 
o     Objects are basic runtime entity in object oriented system. 
o      Objects may be a pen, a person, a table etc.
o      Objects may define user define data types like time, vectors etc.
o      Programming problems analyzed in terms of objects.
o      Object should be chosen such that they match closely with real world objects.
o      Objects required memory space for storage just like structure of ‘C’ language.
o      One object can send message to another objects.
o      For example student and record are two objects and student object may send request to record object for marks, then record object send marks for particular student.
o      Object can interact with each other without having knowledge about data and code of each other’s.
o      Following are ways to represents an object.

Class

Classes:
o      Classes are user defined data types and it behaves like built in types of programming language.


o      Object contains code and data which can be made user define type using class.


o      Objects are variables of class.


o      Once a class has been defined we can create any number of objects for that class. A class is collections of objects of similar type.


o      We can create object of class using following syntax,


Syntax:   class-name object-name;


o      Here class name is class which is already created. Object name is any user define name. For example, if student is class,


Example: student s1, s2;


o      In example s1 and s2 are name of objects for class student. We can create any number of objects for class.

Encapsulation

Encapsulation:
o      Combine data and functions into a single unit (called class) known as encapsulation.
o      Encapsulation is most important features of class. By which we can combine data and functions in one unit.
o      Using encapsulation we can hide data from outside world.
o      To hide data from direct access by program is known as data hiding or information hiding.

Data Abstraction

Data Abstraction:
o      Abstraction refers representation of necessary features without including more details or explanations.
o      Classes use the concept of abstraction.
o      Classes define list of abstract attributes like height, weight etc.
o      These attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member functions.
o      Since classes use the concept of data abstraction, they are known as Abstract Data Types (ADT).

Inheritance or Derivation

   Inheritance OR Derivation:
o      The mechanism of deriving a new class from an old class is called inheritance or derivation. The old class is known as base class while new class is known as derived class or sub class.
o      The inheritance is the most powerful features of OOP.

Polymorphism

  Polymorphism:
o      Polymorphism is a Greek term which means ability to take more than one form.
o      For example, + is used to make sum of two numbers as well as it is used to combine two strings.
o      This is known as operator overloading because same operator may behave differently on different instances.
o      Same way functions can be overloaded. For example, sum () function may takes two arguments or three arguments etc. i.e. sum (5, 7) or sum (4, 6, 8).

Dynamic Binding or Late Binding

  Dynamic binding:
o      Binding means link between procedure call and code to be execute.
o      Dynamic binding means link exist between procedure call and code to be execute at run time when that procedure is call.
o      It is also known late binding.
o      It is generally use with polymorphism and inheritance.
o      For example, complier comes to know at runtime that which function of sum will be call either with two arguments or with three arguments.

Message Passing

Message Passing:
o      Objects can communicate with each others by passing message same as people passing message with each other.
o      Objects can send or receive message or information.
o      Message passing involves name of object, name of function (message) and information to be send.
o      For example, student.mark(name). Here student is object, mark is message, name is information.

Benefits of OOP

Benefits of OOP
o      Following are the benefits of object oriented programming language.
o      User can create new data type or users define data type by making class.
o      Code can be reuse by using inheritance.
o      Data can be hiding from outside world by using encapsulation.
o      Operators or functions can be overloaded by using polymorphism, so same functions or operators can be used for multitasking.
o      Object oriented system can be easily upgrade from small system to large system.
o      It is easy to partition work for same project.
o      Message passing techniques make communication easier.
o      Software complexcity can be easily managed.
o      Maintenances cost is less.
o      Simple to implement.

Application of OOP

Application of OOP
o      Following are the applications for object oriented language.
o      Real time systems
o      Simulation and modeling
o      Object oriented database
o      Hypertext, hypermedia and expertext
o      AI and expert systems
o      Neural network and parallel programming
o      Decision support system
o      Office automation system
o      CIM / CAM / CAD systems

Example of Simple C++ Program


[1] WAP to print message on screen.
                #include<iostream.h>
                #include<conio.h>
                void main()
                {
                        cout<<”This is first program in c++”;
                        cout<<”\n welcome”;
                        getch();
                }
[2] WAP to print value of variables.
                #include<iostream.h>
#include<conio.h>
                void main()
                {
                        int a;
                        a = 50;
cout<< ”\n value of a = ” << a;
getch();          
}

Example of Class programing


[1] WAP which define class employee. Enter emp-no, name, salary and age also display that data.
#include<iostream.h>
#include<conio.h>
class emp
{
        int eno,age;
        float sal;
        char name[30];
  public:
        void input();
        void disp();
};
void emp::input()
{
        cout<<"\n Enter no,name,age,salary for emp:";
        cin>>eno>>name>>age>>sal;
}
void emp::disp()
{
        cout<<"\n Eno: "<<eno;
        cout<<"\t Name: "<<name;
        cout<<"\t Age: "<<age;
        cout<<"\t Sal: "<<sal;
}
void main()
{
        clrscr();
        emp e;
        e.input();
        e.disp();
        getch();
}
OUTPUT:
Enter no,name,age,salary for emp:1  ans  23  45000
Eno: 1       Name: ans                Age: 23           Sal: 45000

Example of inheritance programming


[1] Define class p with two variables as data members. Define another class d which inherits the data members of p. Multiply two variables and display it.
#include<iostream.h>
#include<conio.h>
class p
{
  protected:
        int  a,b;
  public:
        void input()
        {
                cout<<"\n Enter a,b:";
                cin>>a>>b;
        }
        void display()
        {
                cout<<"\n a : "<<a;
                cout<<"\t b : "<<b;
        }
};
class d : public p
{
        int  c;
  public:
        void input()
        {
                p :: input();
                c = a * b;
        }
        void display()
        {
                p :: display();
                cout<<"\t Mul : "<<c;
        }
};
void main()
{
        clrscr();
        d d1;
        d1.input();
        d1.display();
        getch();
}
OUTPUT:
Enter a,b:10   20
a : 10  b : 20  Mul : 200