Object-Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Object-Oriented Programming

Description:

Title: New Employee Orientation Author: Kumar Madurai Last modified by: bina Created Date: 6/2/1995 9:42:18 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 19
Provided by: Kuma65
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Object-Oriented Programming


1
Object-Oriented Programming
  • B.Ramamurthy

2
Object-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)
3
Why 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.

4
OO 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

5
Encapsulation 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

6
The 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.

7
From 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.

8
Member 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

9
Object 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.

10
Basic 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.

11
Access 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.

12
Simple 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

13
Class 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.

14
constructor 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

15
Member Function implementations
  • syntax
  • type class_namefname (list of formal parameter
    types and names)
  • function body

16
Object Instantiation and Invoking Member Functions
  • Counter score
  • score.setCount(45)
  • score.increment()
  • score.decrement()
  • int x score.getCount()

17
Files Involved
include Counter.h
Counter.cpp
Counter.h
include Counter.h
Application.cc
18
Summary
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com