CPP Objects

What Is an Object?

  • Objects are keys to understanding object-oriented technology.
  • They all have state and behavior or some action based on the state.
  • Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
  • Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
  • For each object that you see, ask yourself two questions:
    1. What possible states can this object be in?
    2. What possible behavior or action can this object perform?
  • You may also notice that some objects, in turn, will also contain other objects.
  • An object stores its states in member variables (fields), and exposes its behavior or action through methods.

Object benefits.

  • Bundling code into individual software objects provides a number of benefits, including:
    1. Modularity:
      The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
    2. Information-hiding:
      By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
    3. Code re-use:
      If an object already exists you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
    4. Plug ability and debugging ease:
      If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement.

The main 3 Object Oriented Programming (OOP) Concepts.

  1. Encapsulation:
    • Object legislates how its data may be accessed:
      • Some data may be accessible to others, some not.
      • Data can only be modified in specified ways - easier to ensure correctness.
  2. Inheritance:
    • One class may be a special case of another:
      • Rectangle is like a square, plus a second (possibly) distinct value.
      • Colored-Rectangle is like a rectangle, plus the concept of color.
  3. Polymorphism:
    • Different objects in a tree inheritance hierarchy behave correctly whatever allocated reference type exists for the object:
      • Shape.getArea() len*wid, if Shape refer a Rectangle object
      • shape.getArea() PI*r2, if Shape refer a Circle object
    • I will explain this in more detail in later sessions.
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.