Object Oriented Programming C++ Lecture No:1

Structured Programming Technique

  • Pascal, C, BASIC, Fortran, and similar traditional programming languages are structured/procedural languages.
  • That is, each statement in the language tells the computer to do something.
  • In a structured language, the emphasis is on doing things (functions).
  • A program is divided into functions and – ideally – each function has a clearly defined purpose and a clearly defined interface to the other functions in the program.

Problems with Structured Programming Technique

  • Data Is Undervalued
    – Data is, after all, the reason for a program’s existence.
    – The important parts of a program about a school for example, are not functions that display the data or functions that checks for correct input; they are student, teacher, i.e., data
  • Structured programs (functions and data structures) don’t model the real world very well.
    – The real word dos not consist of functions.
  • Global data can be corrupted by functions that have no business changing it.
  • To add new data items, all the functions that access the data must be modified so that they can also access these new items.
  • Creating new data types is difficult.

Why Object-Oriented Programming?

  • It is still possible to write good programs by using structured programming (C programs).
  • But, object-oriented programming offers programmers many advantages, to enable them to write high-quality programs.

The Object-Oriented Approach

  • The fundamental idea behind object-oriented programming is:
    – The real world consists of objects. Computer programs may contain computer world representations of the things (objects) that constitute the solutions of real world problems.
    – Real world objects have two parts:

    • Properties (or state:characteristics that can change)
    • Behavior (or abilities:things they can do).
  • To solve a programming problem in an object-oriented language, the programmer no longer asks how the problem will be divided into functions, but how it will be divided into objects.
  • The emphasis is on data.
  • What kinds of things become objects in object-oriented programs?
    – Human entities: Employees, customers, salespeople, worker, manager
    – Graphics program: Point, line, square, circle, …
    – Mathematics: Complex numbers, matrix
    – Computer user environment: Windows, menus, buttons
    – Data-storage constructs: Customized arrays, stacks, linked lists
  • Encapsulation and data hiding are key terms in the description of object-Oriented languages.
  • If you want to modify the data in an object, you know exactly what functions interact with it; the member functions in the object.
  • No other functions can access the data. This simplifies writing, debugging, and maintaining the program.

Object Example: A Point

  • A Point on a plane has two properties; x-y coordinates.
  • Abilities (behavior) of a Point are, moving on the plane, appearing on the screen and disappearing.
  • We can create a model for 2 dimensional points with the following parts:
    – Two integer variables (x , y) to represent x and y coordinates
    – A function to move the point: move ,
    – A function to print the point on the screen: print ,
    – A function to hide the point: hide .
  • Once the model has been built and tested, it is possible to create many objects of this model in main program.
    Point point1, point2, point3;

Structured vs. Object-Oriented Approach

Structured Programming:

  • Procedural languages still require you to think in terms of the structure of the computer rather than the structure of the problem you are trying to solve.
  • The programmer must establish the association between the machine model and the model of the problem that is actually being solved.
  • The effort required to perform this mapping produces programs that are difficult to write and expensive to maintain.
    – Because the real world things and their models on the computer are quite different.
  • Example:
    Real world thing: student
    Computer model: char *, int, float …
  • It is said that the C language is closer to the computer than the problem.

Object-Oriented Programming:

  • Object-oriented approach provides tools for the programmer to represent elements in the problem space.
  • We refer to the elements in the problem space and their representations in the solution space as “objects.”
  • The idea is that the program is allowed to adapt itself to the problem by adding new types of objects, so when you read the code describing the solution, you’re reading words that also express the problem.
  • OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run.
  • Benefits of the object-oriented programming:
    • Readability
    • Understandability
    • Low probability of errors
    • Maintenance
    • Reusability
    • Teamwork

Leave a Reply

Your email address will not be published. Required fields are marked *