Title: Object-Oriented Programming
1Object-Oriented Programming
2Object-Oriented Principles
OOP
Polymorphism -- Many forms of same function --
Virtual functions -- Abstract Base Classes
Inheritance -- Hierarchy -- Reusability --
Extensibility -- Expressive power -- Reflects
many real-world problems
Encapsulation (class) -- Information Hiding --
Separation of Interface and
Implementation -- Standardization -- Access
Control mechanisms (private /public)
3Why Object-oriented paradigm?
- Separation of interface and implementation
- Any implementation change should not affect user
interface. - To ease design process Separation of Design
definition, Implementation, Usage through data
encapsulation. - Software reuse
- To allow (1) sharing, (2) upgrades (3)
modification using inheritance and polymorphism. - Parameterized classes in templates.
4OO Concepts sampler
- Consider the automobile (car) you drive.
- List the behaviors (functions) of this
automobile. - The ones you have control over define the
user-interface of the automobile. - The internal behavior defines the private
functionality. As a user you have no control over
the implementation of these. - List the attributes (parts) of the automobile.
- You have access to some. Example steering wheel,
brakes, head-lights, wipers. - You do not have access to some others Odometer
5Encapsulation class car
- class Car
- public //interface
- // behavior or functions
- startEngine
- accelerate
- brake
- park
- shiftGear
- // attributes or parts
- color
- seatBelt
- private //under the hood
- displaySpeed()
- Odometer
- Engine
6The notion of class
- Things discussed above are in general applicable
to any car or Shall we say to a class of cars? - Some functionality and parts are to be hidden.
- An interface containing a set of functions and
components have to be presented to the user of
the car.
7From problem to OO solution
- Identify the objects (nouns) in the problem.
- Define and implement classes to represent the
objects. - Class name
- Data members public, private, (protected)
- Function members
- Service functions (usually public)
- Utility functions (usually private)
- Predicate functions. Example warnings, status
indicators, error indicators, etc. - Write a driver that carries out the interaction
among the objects.
8Member functions
- Member functions are based on use cases
- Ask the question What is an object of this class
used for? And enumerate its uses. - This list will indicate to the designed one or
more functions that need to be implemented to
fulfill the use case
9Object instantiation
- If you are interested in a specific car Then an
instance of the class can be created - Example car mycar, rentedCar
- mycar and rentedCar are objects of class car.
- It is also possible to set a specific
characteristic such as color, if it is public. - Example mycar.color green
- If an attribute is private then an access
function is needed to set its values.
10Basic syntax class definition
- class class_name
-
- public
- list of class attributes (variables, types,
constants, and so on) that may be accessed by
name from outside the class. - list of prototypes for each member function that
may be accessed by name from outside the class. - private
- list of class attributes (variables, types,
constants, and so on) that are intended to be
hidden for reference from outside the class. - list of prototypes for each member function
intended to be hidden from outside of the class.
11Access specifiers- private and public
- The scope of a specifier begin with the keyword
private or public and ends when at the end of the
class or at another access specifier. - There can be more than one private or public
sections. But it is a good programming style to
pool all the public items into a single public
section and all private into a single private
section. - By default all items in a class are private! So
dont forget to publicize. - The items in a private section of a class are
available only to the member function of the
class.
12Simple definition of class Counter
- class Counter
- public
- Counter() //constructor
- void setCount( int val)
- int getCount()
- void increment ( int n)
- void decrement ( int n)
- private
- int count
13Class constructor(s)
- A class constructor is used to create
(instantiate) an object of the class. - The constructor is automatically executed each
time an object (of class type) is declared. - A class constructor has the same name as the
class. - Ensures all objects start in a consistent state
and contains the initialization code. - A constructor cannot specify a return type or
explicitly return a value. - A class typically has more than one constructor
default constructor, copy constructor and
(converter) or initializing constructor.
14constructor implementation
- operator is called the class resolution
operator. The class name before defines the
class context in which a function is defined. - CounterCounter()// default constructor
- count 0
- CounterCounter(int value)
- // initializing constructor
- count value
15Member Function implementations
- syntax
- type class_namefname (list of formal parameter
types and names) -
- function body
16Object Instantiation and Invoking Member Functions
- Counter score
- score.setCount(45)
- score.increment()
- score.decrement()
- int x score.getCount()
17Files Involved
include Counter.h
Counter.cpp
Counter.h
include Counter.h
Application.cc
18Summary
- Class definition, implementation, usage were
illustrated - Three principles of OO design encapsulation,
inheritance, polymorphism. We looked at examples
for the first two. - Problem statement to Object-oriented design.
- Notion of class and object instantiation were
illustrated. - Access control specifiers were introduced.