Unit 1
Unit 1
UNIT-1
  Object Oriented Programming Concepts & Introduction to C++
1.1 Evolution of OOPS:
   The OOP (Object Oriented Programming) approach is most commonly used approach now a days.
OOP is being used for designing large and complex applications. Before OOP many programming
approaches existed which had many drawbacks.
   These programming approaches have been passing through revolutionary phases just like
computer hardware. Initially for designing small and simple programs, the machine language was
used. Next came the Assembly Language which was used for designing larger programs. Both
machine and Assembly languages are machine dependent. Next came Procedural Programming
Approach which enabled us to write larger and hundred lines of code. Then in 1970, a new
programming approach called Structured Programming Approach was developed for designing
medium sized programs. In 1980's the size of programs kept increasing so a new approach known
as OOP was invented.
Procedural Programming Approach: This approach is top down approach. In this approach, a
program is divided into functions that perform a specific task. Data is global and all the functions can
access the global data. Program flow control is achieved through function calls and goto statements.
This approach avoids repetition of code which is the main drawback of Monolithic Approach. The
basic drawback of Procedural Programming Approach is that data is not secured because data is
global and can be accessed by any function. This approach is mainly used for medium sized
applications. The programming languages: FORTRAN and COBOL follow this approach.
                                                                                                      1
Object Oriented Programming Approach: The OOP approach came into existence to remove the
drawback of conventional approaches. The basic principal of the OOP approach is to combine both
data and functions so that both can operate into a single unit. Such a unit is called an Object. This
approach secures data also. Now a days this approach is used mostly in applications. The
programming languages: C++ and JAVA follow this approach. Using this approach we can write any
lengthy code.
    Procedural Programming Approach is top down approach in which program is divided into
functions
that perform a specific task. Data is global and all the functions can access the global data. Program
flow control is achieved throw the use of jumps and function calls. This approach avoid the
repetition of code. The programming languages like FORTRAN and COBOL follow this approach.
1. It emphasis on algorithm.
                                                                                                      2
          2. Large programs are divided into smaller programs known as functions.
          3. Function can communicate by global variable.
          4. Data move freely from one function to another function.
          5. Functions change the value of data at any time from any place. (Functions transform
             data from one form to another.)
          6. It uses top-down programming approach.
    The Object Oriented Programming Approach came to remove some of the flaws encountered in
procedural approach for designing large and complex programs. The basic principal of Object
Oriented Programming Approach is to combine both data and functions that operate on that data
into a single unit. Such a unit is called an object.
It treats data as a critical element in the program development and does not allow it to flow freely
around the system. It tries data more closely to the functions that operate on it, and protects it from
accidental modifications from outside the functions . OOP allow decomposition of a problem into
number of entities called objects and build data functions around these objects. The data of a object
can be accessed only by the functions associated with that object. Functions of one object can access
the functions of another objects.
         Two objects can communicate via the function without knowing the data of each another.
         We can represent a class by two ways as shown in the below figure:
         Out of these two methods, first method to represent object is widely used.
                                                                                                     3
1.4 state basic concepts of object oriented programming languages:
    o     Object
    o     Class
    o     Inheritance
    o     Polymorphism
    o     Abstraction
    o     Encapsulation
Object:
Any entity that has state and behavior is known as an object. Here, state means data and behavior
means functionality. .
For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
                                                                                                     4
#include <iostream>
using namespace std;
class Student
 {
   public:
    int id;      //data member (also instance variable)
    string name; //data member(also instance variable)
};
int main() {
   Student s1;   //creating an object of Student
   s1.id = 001;
   s1.name = "red";
   cout<<s1.id<<endl;
   cout<<s1.name<<endl;
   return 0;
}
Output:
001
red
     /* Initialize and Display data through method*/
#include <iostream>
using namespace std;
class Student
{
   public:
     int id;
     string name;
     void insert(int i, string n)
           {
        id = i;
        name = n;
      }
     void display()
      {
        cout<<id<<" "<<name<<endl;
      }
};
int main( )
 {
   Student s1;
   Student s2;
   s1.insert(001, "red");
   s2.insert(002, "black");
   s1.display();
   s2.display();
   return 0;
}
Output:
001 red
002 black
                                                          5
    Inheritance:
            Inheritance is the process by which one class inherits the properties and method of another
             class. This is based on the hierarchical classification.
            For example, we can categories the ‘animal’ into two categories: ‘wild animal’ and ‘pet
             animal’. Also we can categories ‘wild animal’ into ‘tiger’, ‘lion’, ‘leopard’ and ‘pet animal’
             into ‘cat’, ‘dog’, ‘bull’.
Polymorphism:
   Polymorphism is a Greek work and its mean is to take more than one form. An operations show
    different behavior in different condition.
   The behavior is used on the data type of variable and number of arguments.
   For example following function calls are different in terms of number of arguments.
sum(int a, int b)
Dynamic Binding:
                                                                                                            6
   It is also known as early binding.
   Another technique of binding is dynamic binding in which linking of function is performed during the
    execution when function is called.
   It is also known as the late binding. Dynamic binding provides the facility of the polymorphism.
Message Passing:
Usage of C++
                                                                                                        7
By the help of C++ programming language, we can develop different types of secured and robust
applications:
    o   Window application
    o   Client-Server application
    o   Device drivers
    o   Embedded firmware etc
        while SIMULA 67 is credited as the first object oriented programming language, the popular
        languages are:
C++ C# Java
                                                                                                    8
                 screen
   The statement cin>>n; is an input statement and causes the program to wait for the user to type
   in a number. The number keyed is placed on the variable “n”. The identifier cin is a predefined
   object in C++ that corresponds to the standard input stream. The operator >> is known as
   extraction operator. It extracts the value from the keyboard and assigns it to the value variable on
   its right.
Keyboard
3. DEFINITION SECTION : It is used to declare some constants and assign them some value.
   e.g. #define MAX 25
   Here #define is a compiler directive which tells the compiler whenever MAX is found in            the
   program replace it with 25.
4. GLOBAL DECLARATION SECTION : Here the variables and class definations which are used through
   out the program (including main and other functions) are declared so as to make them global(i.e
   accessible to all parts of program). A CLASS is a collection of data and functions that act or
   manipulate the data. The data components of a class are called data members and function
   components of a class are called member functions
           A class ca also termed as a blue print or prototype that defines the variable or functions
   common to all objects of certain kind. It is a user defined data type
e.g.
5. SUB PROGRAM OR FUNCTION SECTION : This has all the sub programs or the functions which
   our program needs.
    void display()
    {
    cout<<”C++ is better that C”;
    }
                                                                                                      9
   SIMPLE C++ PROGRAM:
                     #include<iostream> using namespace std; void display()
                     {
                                    cout<<”C++ is better that C”;
                     }
                     int main()
                     {
                    display()
                    return 0;
                     }
6. MAIN FUNCTION SECTION : It tells the compiler where to start the execution
   from main()
   {
   point from execution starts
   }
     main function has two sections
            •declaration section : In this the variables and their data types are declared.
           • Executable section or instruction section : This has the part of program which
           actually performs the task we need.
   namespace: namespace is used to define a scope that could hold global identifiers.
               ex:-namespace scope for c++ standard library.
   A classes ,functions and templates are declared within the namespace
   named
   std using namespace std;
           user defined name space:
           syntax for defining name space is
          namespace namespace_name
          {
          //declarations of variables.functions,classes etc...
          }
          ex: #include<iostream> using
          namespace std;
          namespace sample
          {`
                  int m;
                  void display(int n)
                  {
                          cout<<"in namespace N="<<n<<endl;
                  }
          }
           #include<iostream>
   This directive causes the preprocessor to add content of iostream file to the program. some old
   versions of C++ used iostream.h .if complier does not support ANSI (american nation standard
                                                                                               10
     institute) C++ then use header file iostream.
Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming
(OOP)
     Procedure Oriented Programming            Object Oriented Programming
        program is divided into small parts
1                                                     program is divided into parts called objects.
        called functions.
        Importance is not given to data but to        Importance is given to the data rather than
2       functions as well as sequence of              procedures or functions because it works as
        actions to be done.                           a real world.
3      POP follows Top Down approach.                OOP follows Bottom Up approach.
4                                                     OOP has access specifiers named
        It does not have any access specifier.
                                                      Public, Private, Protected, etc.
5       Data can move freely from function            objects can move and communicate with
        to function in the system.                    each other through member functions.
6       To add new data and function in               OOP provides an easy way to add new
        POP is not so easy.                           data and function.
        Most function uses Global data for            In OOP, data can not move easily from
        sharing that can be accessed freely           function to function,it can be kept public
7       from function to function in the              or private so we can control the access of
        system.                                       data.
8       It does not have any proper way               OOP provides Data Hiding so provides
        for hiding                                    more
        data so it is less secure.                    security.
                                                      In OOP, overloading is possible in the
        Overloading is not possible.                  form of Function Overloading and
9                                                     Operator Overloading.
        Example of Procedure Oriented
        Programming are : C, VB,                      Example of Object Oriented Programming
10      FORTRAN, Pascal.                              are : C++, JAVA, VB.NET, C#.NET.
     Data abstraction :Abstraction refers to the act of representing essential features without
     including the back ground details or explanation. Classes use the concept of abstraction and are
     defined as a list of attributes such as size, weight, cost and functions to operate on these
     attributes. They encapsulate all essential properties of the object that are to be created. The
     attributes are called as data members as they hold data and the functions which operate on these
     data are called as member functions.Class use the concept of data abstraction so they are
     called abstract data type (ADT)
                                                                                                      11
     Polymorphism: Polymorphism comes from the Greek words “poly” and “morphism”. “poly”
     means many and “morphism” means form i.e.. many forms. Polymorphism means the ability to
     take more than one form. For example, an operation have different behavior in different
     instances. The behavior depends upon the type of the data used in the operation.
     Different ways to achieving polymorphism in C++ program:
             1) Function       overloading    2)
     Operator overloading #include<iostream>
     using namespace std;
     int main(){
     int a=4;
      a=a<<2;
     cout<<”a=”<<a<<endl; return 0;
     }
     Inheritance: Inheritance is the process by which one object can acquire the properties of another.
     Inheritance is the most promising concept of OOP, which helps realize the goal of constructing
     software from reusable parts, rather than hand coding every system from scratch. Inheritance
     not only supports reuse across systems, but also directly facilitates extensibility within a
     system. Inheritance coupled with polymorphism and dynamic binding minimizes the amount of
     existing code to be modified while enhancing a system.
             When the class child, inherits the class parent, the class child is referred to as derived
     class (sub class) and the class parent as a base class (super class). In this case, the class child has
     two parts: a derived part and an incremental part. The derived part is inherited from the class
     parent. The incremental part is the new code written specifically for the class child.
     Dynamic binding:      Binding refers to linking of procedure call to the code to be executed in
     response to the call. Dynamic binding(or late binding) means the code associated with a given
     procedure call in not known until the time of call at run time.
     Message passing: An object oriented program consists of set of object that communicate with
     each other.
     Objects communicates with each other by sending and receiving information .A message for an
     object is a request for execution of a procedure and there fore invoke the function that is called
     for an object and generates result.
C C++
C input and output functions are            printf and C ++ input and output functions are cout and cin
scanf.
C uses header file stdio.h                                C++ uses header file iostream.h
C is top to bottom approach                               C++ is bottom to top approach.
                                                                                                           12
C does not support inheritance                      C++ supports inheritance.
C supports encapsulation but not member             C++ supports encapsulation and member
functions                                           functions
C does not support function and operator            C++ supports function and operator over loading .
over loading .
    To handle the input and output operations we use pre-defined stream objects .
   Input                            Central                                Outp
       
   devic   Console  input:    To handle console
                                    processin    input we  use cin object. ut
    Cin : It is a input stream object which is available in<iostream.h> header file     .
   e              >>     this symbolgisunit
                                        called as extractor .              devic
     This is used to extract input from the keyboard and place it into specified variable
     name
     .
     Syntax:            cin >> variable _name ;
       For example :
                        cin >> a ;
     This is used to read the input from user with the specified name .
                                                                                                13
               These are used to describe only single line.
It is denoted by two back slash “ // ”.
For example:
         #include <iostream>
          main() {
           cout << "Hello World"; // prints Hello World
            return 0;
            }
MULTI LINE COMMENT:
          These are used to describe multi statements at a time.
It is denoted by stating with /* and ending with */
characters. For example:
               /* This is a comment */
         /* C++ comments can also
          * span multiple lines
          */
                                                                   14
1.12   LIST KEYORDS OF C++ OTHER THAN C
             The keywords implement specific c++ language features they are explicitly reserved identifiers
  and cannot be used as names for the program variables or other user defined program elements.
  Different keywords available in c+ + other than C
               Asm                                                     Friend
               Catch                                                   Static
                                                                        Template
               Throw                                                   New
               Try                                                     Delete
               Public                                                  Operator
               Private                                                 Enum
               Protected                                               Virtual
               Inline                                                   Class
                                                                                                        15
16
1.13      EXPLAIN C++ OPERATORS
     An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
     C++ is rich in built-in operators and provides the following types of operators:
         Arithmetic Operators
         Relational Operators
         Logical Operators
         Bitwise Operators
         Assignment Operators
         Misc Operators
      Arithmetic Operators
They are the types of operators used for performing mathematical/arithmetic operations.
                                                                             A + + will give 11
++                      Increases an integer value by 1.
                                                                             A -- will give 9
--                      Decreases an integer value by 1.
                                                                                                            17
       Relational Operators:
       There are following relational operators supported by C++ language.
                             Checks if the value of left operand is less than the                          A < B is true.
       <                     value of right operand, if yes then condition
                             becomes true.
                                                                                                   A >= B is not true.
                             Checks if the value of left operand is greater
       >=                    than or equal to the value of right operand, if
                             yes then condition becomes true.
                                                                                                          A <= B is true
                             Checks if the value of left operand is less than
       <=                    or equal to the value of right operand, if yes
                             then condition becomes true.
           Logical Operators:
           There are following logical operators supported by C++
then:
                                                                                                                  18
 Bitwise Operators:
  Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^
  are as follows:
 0   0       0       0        0
 0   1       0       1        1
1 1 1 1 0
 1   0       0       1        1
         Assume if A = 60; and B = 13; now in binary format they will be as follows: A = 0011 1100
B = 0000 1101
                                                                                                     19
A&B = 0000 1100
~A = 1100 0011
                  20
21
CM-404 C++
             22
CM-404 C++
             23
CM-404 C++
             24
CM-404 C++
25