Academy for Academic Education

Introduction to Object Oriented Programming using C++

Programming

  • Programming involves writing instructions that enable a computer to carry out a single or group of tasks. Writing these sets of instructions which are known as programs or Software, requires using a computer programming language.
  • These Programs are also referred as application programs or simply applications by the end user who might be using the program written to accomplish tasks such as
    • to create inventory report
    • to create game
    • to process payrolls
    • to create insurance quote
  • To develop applications we might need to use programming languages as discussed above. Programming languages can be mostly classified into two paradigms
    • Procedural Programming
    • Object Oriented Programming

Procedural Programming

  • In Procedural programming languages we write statementss to tell computer to do something (instruction). A program in procedural language is list of instructions
  • As programs become larger, a single list of instrcutions becomes difficult to maintain, so the functions are adopted as a way to make programs more understandable to human creators. This in some languages may be referred as subroutine, procedure
  • A procedural program is divided into functions & each function has a clear defined purpose and clearly defined interface to the other functions in program
  • This idea of breaking program into functions can be extended further by grouping number of functions together into a larger entity called as module
  • Dividing the program into functions and modules is one of the corner stones of procedural programming
  • Problems with Procedural Programming:
    • Unrestricted Access:
      • In a procedural program (like one written in C). There are two kinds of data
        • Local data: This is hidden inside a function and is used exclusively by the function.
        • Global data: This data can be accessed by any function in the program
      • When we create more functions, data should be made available to multiple functions, so we make them global
      • As the application becomes more complex, then we need deal with lots of global variables (global data) and it will be very difficult to handle the changes made
    • Real World Modeling:
      • In physical world, we deal with objects such as people and cars. Such objects are not like data and functions, Real world objects have both attributes and behavior, so niether data nor functions by themselves model real-world objects effectively
      • Example: Car has color, horsepower, tyres etc (attributes) and also cars have the behavior to start, stop, move etc. Car has both data and behavior (i.e. functions)

Object-Oriented Programming

  • The fundamental idea behind object-oriented languages is to combine both data (attributes) and functions (behaviors) into single unit. This unit is called as an object
  • Object Data:
    • In Object Oriented programming terminology, data is called as attributes. The attributes contain the information that differentiates various objects.
    • Example -1: Employee attributes could be Social security number (aadhar number), data of birth, phone number etc
    • Example- 2: Student attributes could be roll number, name, branch etc
  • Object Behavior:
    • The behavior of object represents what object can do. In Object Oriented terminology these behaviors are contained in methods
    • Other objects can invoke methods by sending message to it
    • Example -1 : Employee object might have behavior of start work (Which could be making cars in automative industry, selling products in marketing etc) and stop work.
    • Example-2: Student object might have the behavior of learn, write exams etc
    • In C++, we call these behaviors as member functions
    • These member functions in addition to behaviors, also provide the only way to access its data.
    • A C++ program typically consists of number of objects which communicate with each other by calling one anothers member functions.
  • What can be objects in Object Oriented Programs?
    • Answer is limited only by your imagination. But lets consider some examples
      1. Point-Of-Sale: Objects in this system could be
        1. Customer
        2. Employee
        3. Payment
          1. Cash
          2. Card
        4. Bill
        5. Product/Item
      2. Library: Objects in this system could be
        1. Librarian
        2. Book
        3. CD
        4. Magazine
        5. Member
          1. Student
          2. Faculty
        6. Library Card

Class and Objects

  • A Class is blue print for an object
  • Creating object from class is called as instantiation.
  • Creating objects:
    • Class can be thought of as the template for objects
    • In the below image we are using cookie cutter to create cookies. The cookie cutter can be thought of as class which will have blueprint (or template like shape etc) and cookies created are objects
    • Objects now can be referred as instances of classes

Key Concepts of Object Oriented Programming

  • The following are the key concepts of object oriented programming
    1. Encapsulation & Data Hiding
    2. Abstraction
    3. Inheritance
    4. Polymorphism
Exit mobile version