Object Oriented Programming C++ Lecture No 4

Outline

  • Some C++ Terminology
  • Data Hiding
  • Access Specifiers
  • Objects as Function Arguments
  • Returning Objects from Functions
  • Classes vs. Structures
  • Classes, Objects, & Memory

Some C++ Terminology

  • A class is a grouping of data and functions.
  • A class is very much like a structure type, it is only a pattern to be used to create a variable which can be manipulated in a program.
  • An object is an instance of a class, which is similar to a variable defined as an instance of a type.
  • A method (member function) is a function contained within the class.
  • A message is the same thing as a function call. In object oriented programming, we send messages instead of calling functions.

A Class Acts as a Blueprint for New Objects

Data Hiding

  • Data is concealed within a class, so that it can’t be accessed mistakenly by functions outside the class.
  • The primary mechanism for hiding data is to put it in a class and make it private.
  • Hiding data from parts of the program that don’t need to access it.
  • One class’s data is hidden from other classes.

Access Specifiers

  • private data or functions
    –  Can only be accessed from within the class.
  • public data or functions
    – Are accessible from any where (both inside and outside the class).

Private Access Specifiers Ex:

Public Access Specifiers Ex:

Private and Public

  • Functions are public
    – Usually functions are public.
    – This is a result of how classes are used.
    – Functions that operate on data are public so they can be accessed from outside the class.
  • Data is private
    – Data is hidden so it will be safe from accidental manipulation.
  • There is no rule that data must be private and functions public.

Objects as Function Arguments

distance.cpp file:

Output
29
0
Press any key to continue

Objects as Function Arguments

  • Every call to a member function is associated with a particular object.
  • The function has direct access using the member names alone to all the members, whether private or public, of that object.
    – Example: feet and inches
  • It also has indirect access, using the object name and the member name, connected with the dot operator to other objects of the same class that are passed as arguments.
    – Example: d1.inches or d2.feet

Structures vs. Classes

  • We believe that
    – Structures provide a way to group data
    – Classes provide a way to group both data and functions.
  • In fact, structures can be used in almost exactly the same way as classes.
  • The only formal difference remains that
    – In a class, members are private by default.
    – In a structure, members are public by default.

Part Example (with class)

Part Example (with struct)

Classes, Objects, and Memory

  • We believe
    – Each object created from a class contains separate copies of that class’s data and member functions.
    – It emphasizes that objects are complete, self-contained entities, designed using the class declarations.
    – Example: cars
  • In fact
    – Each object has its own separate data items.
    – On the other hand, all the objects in a given class use the same member functions.
    – The member functions are created and placed in memory only once, when they are defined in the class declaration.
    – The rational being: functions for each object are identical.

Leave a Reply

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

nine + 19 =