Object Oriented Programming Concepts

Introduction to Object Oriented Programming

  • Objective: Introduce object-oriented programming (OOP) concepts, features, benefits, and applications.
  • C++ is an object-oriented programming language, an extension of C.
  • In 1980, Bjarne Stroustrup addressed limitations of C by adding extensions, most importantly the concept of CLASS, to support object-oriented programming.

Object Oriented Programming Pattern

  • OOP addresses flaws in the procedural approach by treating data as a critical element.
  • OOP ties data closely to functions that operate on it, protecting it from accidental modification.
  • OOP decomposes problems into objects and builds data and functions around them.
  • Data within an object is accessed only by its associated functions.
  • Functions of one object can access functions of other objects.

Characteristics of C++/OOP

  • Emphasis on data rather than procedure.
  • Programs divided into objects.
  • Data structures designed to characterize objects.
  • Functions operating on object data are tied together in the data structure.
  • Data is hidden and inaccessible to external functions.
  • Objects communicate through functions.
  • New data and functions can be easily added.
  • Follows a bottom-up approach in program design.

Definition of Object-Oriented Programming

  • OOP modularizes programs by creating partitioned memory areas for data and functions, usable as templates for creating copies on demand.
  • Objects are partitioned memory areas storing data and operations to access it.
  • Independent memory partitions allow objects to be used in different programs without modification.

Basic Concepts of Object-Oriented Programming

  • Key concepts include:
    • Objects
    • Classes
    • Data abstraction and encapsulation
    • Inheritance
    • Polymorphism
    • Dynamic binding
    • Message passing

Objects

  • Objects are basic run-time entities in an OOP system.
  • Represent real-world items (e.g., person, place, bank account) or user-defined data (e.g., vectors, time, lists).
  • Objects occupy memory space and have an associated address.
  • During program execution, objects interact by sending messages.
  • Objects contain data and code to manipulate that data which allows interaction without detailed knowledge of each other's data/code.
  • Sufficient to know the type of message accepted and response returned.

Classes

  • Classes are user-defined data types that contain data and code to manipulate that data.
  • Objects are variables of the type class.
  • Once defined, any number of objects can be created from a class.
  • Definition: A class is a collection of objects of a similar type.
  • Example: mango, apple, orange are members of the class fruit.
  • The syntax to create an object is similar to creating an integer object in C.
  • Example: Fruit mango; creates an object mango of class fruit.

Data Abstraction and Encapsulation

  • Encapsulation: Wrapping up data and functions into a single unit (class).
  • Data encapsulation is a key feature; data is not accessible outside the class.
  • Functions within the class provide the interface between the object's data and the program.
  • Data hiding: Insulation of the data from direct access by the program.
  • Abstraction: Representing essential features without background details.
  • Classes use abstraction with abstract attributes (size, weight, cost) and functions to operate on them.
  • Attributes are data members which hold information; functions are methods or member functions.
  • Classes using data abstraction are known as Abstract Data Types (ADT).

Inheritance

  • Inheritance: Objects of one class acquire properties of objects of another class.
  • Example: robin (bird) is part of “flying bird” which is part of “bird”.
  • Derived classes share common characteristics with the base class.
  • Reusability: Add features to existing class without modification by deriving a new class.
  • The new class combines features of both classes.
  • Sub-classes define unique features.
  • Without classification, each class must explicitly include all features.

Polymorphism

  • Polymorphism: Ability to take more than one form.
  • An operation exhibits different behaviors in different instances, depending on the data types used.
  • Example: Addition of two numbers produces a sum; concatenation of strings produces a third string.
  • Operator overloading: Making an operator exhibit different behaviors in different instances.
  • Function overloading: Using a single function name to perform different types of tasks based on arguments.
  • Polymorphism allows objects with different internal structures to share the same external interface.
  • A general class of operations can be accessed the same way, even if specific actions differ.
  • Polymorphism is extensively used in implementing inheritance.

Dynamic Binding

  • Binding: Linking a procedure call to the code to be executed.
  • Dynamic binding (late binding): Code associated with a procedure call is unknown until run-time. Associated with polymorphism and inheritance.
  • A function call associated with a polymorphism reference depends on the dynamic type of that reference.
  • Example: The “draw” procedure is redefined in each class that defines an object. At run-time, the code matching the object under current reference will be called.

Message Passing

  • An object-oriented program consists of objects communicating with each other.
  • Steps:
    1. Create classes defining objects and their behavior.
    2. Create objects from class definitions.
    3. Establish communication among objects.
  • Objects communicate by sending/receiving messages, similar to people.
  • A message requests the execution of a procedure and invokes a function in the receiving object.
  • Message passing involves specifying the object name, function (message) name, and information to be sent.
  • Objects have a lifecycle; they are created and destroyed. Communication is feasible as long as an object is alive.

Benefits of OOP

  • OOP provides benefits to program designers and users.
  • Contributes to solving problems related to software development and quality.
  • Promises greater programmer productivity, better software quality, and lower maintenance costs.
    • Inheritance eliminates redundant code and extends the use of existing classes.
    • Build programs from standard working modules, saving development time and increasing productivity.
    • Data hiding helps build secure programs, preventing intrusion from other code.
    • Multiple object instances can co-exist without interference.
    • Mapping objects in the problem domain to program objects is possible.
    • Easy to partition work in a project based on objects.
    • Data-centered design captures more model details in implementable form.
    • Object-oriented systems are easily scalable.
    • Message passing simplifies interface descriptions with external systems.
    • Software complexity is easily managed.
  • Importance of features depends on project type and programmer preference.
  • Object libraries must be available for reuse.
  • Technology is developing rapidly; products may be superseded quickly.
  • Strict controls and protocols are needed for reuse.
  • Developing easy-to-use software is challenging.
  • Object-oriented programming tools can help manage this problem.

Object Oriented Languages

  • OOP concepts can be implemented in languages like C and Pascal, but it can become clumsy for large programs.
  • Languages designed to support OOP concepts make implementation easier.
  • Languages are classified based on supported features:
    1. Object-based programming languages
    2. Object-oriented programming languages
  • Object-based programming supports encapsulation and object identity.
  • Major features:
    • Data encapsulation
    • Data hiding and access mechanisms
    • Automatic initialization and clear-up of objects
    • Operator overloading
  • Object-based languages do not support inheritance and dynamic binding. Ada is an example.
  • Object-oriented programming includes all object-based features plus inheritance and dynamic binding.
  • Object-oriented programming = Object-based features + inheritance + dynamic binding
  • Languages: C++, Smalltalk, Object Pascal, Java.

Applications of OOP

  • OOP is gaining importance in many areas.
  • Popular in user interface design (windows).
  • Simplifies complex problems in real-business systems.
  • Promising areas:
    • Real-time systems
    • Simulation and modeling
    • Object-oriented databases
    • Hypertext, hypermedia, and expertext
    • AI and expert systems
    • Neural networks and Parallel programming
    • Decision support and office automation systems
    • CIM/CAM/CAD systems
  • Object-oriented technology will change how software engineers think, analyze, design, and implement systems.

What is C++?

  • C++ is an object-oriented programming language developed by Bjarne Stroustrup at AT&T Bell Laboratories in the early 1980s.
  • An extension of C with the addition of the class construct feature of Simula67.
  • Underwent improvements and changes, standardized by ANSI/ISO in November 1997.
  • C++ is a superset of C; almost all C programs are also C++ programs.
  • Key additions:
    • Classes
    • Inheritance
    • Function overloading
    • Operator overloading

Applications of C++

  • Versatile language for handling large programs.
  • Suitable for developing editors, compilers, databases, communication systems, and complex real-life applications.
  • Allows creating hierarchy-related objects, enabling the creation of special object-oriented libraries for reuse.
  • C++ gives the ability to get close to machine-level details.
  • C++ programs are easily maintainable and expandable; new features can be easily added to existing object structures.
  • Expected to replace C as a general-purpose language.