Object Oriented Programming - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Object Oriented Programming

Description:

Office hours: After the lecture. Teaching assistant: Mr. Moran Lefler ... BMW are both cars implies that they both accelerate when we press the gas pedal. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 34
Provided by: dmitryk2
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object Oriented Programming
  • Spring 2009
  • Recitation 1

2
Administrative Details
  • Course website http//cs.haifa.ac.il/courses/prog
    _tech
  • Lecturer Mr. Gennadi Lemberski
  • Office hours After the lecture
  • Teaching assistant Mr. Moran Lefler
  • Office hours Wednesday, 1100 , at HIACS lab,
    near Jacobs building
  • Slides based on work by Dmitry Koyfman

3
What is OOP?
  • OOP is a programming paradigm that focuses on
    objects and entities in the problem, rather than
    functions and algorithms.
  • It is more natural for a human to think of a
    problem as a collection of objects that interact,
    than to divide the problem into functions that
    run consecutively.

4
What is OOP?
  • We see objects around us.
  • An object has a state (its properties) and
    behavior (what it can do).
  • A car has state color, max speed, model and
    behavior drive, turn left, stop.
  • Objects can interact by creating, changing and
    using one another.
  • A car can use the road, carry passengers, fill
    gasoline.

5
3 Main OOP Concepts
  • Encapsulation
  • Inheritance
  • Polymorphism

6
Encapsulation
  • We dont really care how an object is
    implemented.
  • As long as a car works, we dont care that it
    includes 4-cylinder engine, steel wheels, and 12V
    accumulator. We dont care that the engine runs
    at 3,000 RPM and that the plugs make 700
    ignitions per second.
  • As long as cars exhibit some similar interface,
    we can drive them.

7
Encapsulation
  • An object exposes to the world an interface, and
    we can use (without any change) any object that
    corresponds to that interface.
  • All implementation details are hidden within the
    object, and dont interest us.

8
Inheritance
  • When creating something new we rarely create it
    from scratch. Rather we rely on existing objects.
    We specialize them.
  • When planning a car with automatic gear box, we
    wont create a completely new car. Well
    specialize existing car so that it uses an
    automatic gear box.
  • Moreover, we certainly wont invent the wheel,
    and design a land vehicle (that has steering
    wheel, engine, etc.) that is a car.

9
Inheritance
  • The inheritance models the is-a relationship.
  • The car is a land vehicle.
  • The car with automatic gear box is a car.
  • Well say that a car inherits from land vehicle,
    and a car with automatic gear box inherits from
    car.
  • The last inheritance is a design decision a car
    can include different kinds of gear boxes, or
    subtype (use inheritance) them.

10
There is no ideal design!
11
Polymorphism
  • Knowing that 83 Ford and 06 BMW are both cars
    implies that they both accelerate when we press
    the gas pedal. But the BMW accelerates faster and
    the inner workings are different!
  • This is called polymorphism objects exhibit
    varying behavior, dependent on the exact type of
    the object.

12
Switching to C
13
First C Program
  • // First C Program
  • include ltiostreamgt
  • int main()
  • stdcout ltlt "Hello, world!\n"
  • return 0

14
Line by Line
  • // First C Program
  • One line comments are now possible. Old / /
    comments work too.
  • include ltiostreamgt
  • Standard C headers are replaced by C headers.
    Notice the lack of .h.

15
Line by Line
  • int main()
  • C starts executing from function main() (just
    as C).
  • stdcout ltlt "Hello, world!\n"
  • The new (and better) way of outputting. stdcout
    is a standard output stream usually connected to
    the screen. We write to it using operatorltlt.
    Control characters are the same as in C.

16
Line by Line
  • return 0
  • A non-void function must return a value. The
    convention is that main() returns 0 if it ended
    without errors.

17
C as an extended C
  • C is designed to be almost backwards-compatible
    with C. Most correct C programs will compile with
    a C compiler.
  • The syntax is the same opening and closing
    braces , function names and calls, variable
    definitions, etc.

18
But beware of writing C code instead of C code!
19
A Class
  • A class is a recipe for creating objects. It
    describes the state of the object (inner
    variables data members) and its behavior
    (functions that work with this object member
    functions or methods).
  • The class has a public part, a private part, and
    protected part.
  • Unless specified, the members are private.
  • The same with struct, except members are public
    by default.

20
Access Control
  • Everything in the public part can be used and
    accessed by anyone, e.g. any other function or
    object.
  • Everything in the private part is accessible only
    by the object itself.
  • The protected part will be described later.
  • Usually all data members and functions that are
    of no interest to the world are private.

21
Con/De-struction
  • When object comes to life, as well as when it
    dies, some actions must be taken.
  • When object is created, the class constructor is
    called. When object dies, the class destructor is
    called.
  • Constructor for class Car is defined by
    Car(parameter list). Multiple constructors are
    possible, differing by the parameters.
  • Destructor is defined by Car().

22
Stack Implementation Stack.h
  • ifndef _STACK_H_
  • define _STACK_H_
  • // A stack of ints that cannot hold 0s.
  • class Stack
  • public
  • Stack(int size)
  • Stack()
  • int pop()
  • bool push(int el)
  • private
  • int _contents
  • int _size
  • int _top_index
  • endif

23
Stack.cpp (1)
  • include "Stack.h"
  • StackStack(int size)
  • if ( (_contents new intsize) 0 )
  • _size 0
  • _top_index 0
  • else
  • _size size
  • _top_index 0
  • StackStack()
  • if (_size ! 0)
  • delete _contents

24
Stack.cpp (2)
  • bool Stackpush(int el)
  • if (el 0)
  • return false
  • if (_top_index _size)
  • return false
  • _contents_top_index el
  • _top_index
  • return true
  • int Stackpop()
  • if (_top_index 0)
  • return 0
  • _top_index--
  • return _contents_top_index

25
Scope
  • To specify that a function or variable reference
    refers to a particular class, we use the
    scope operator Stackpop().
  • To specify that a function or variable reference
    refers to a particular object, we use the .
    member access operator s.pop().

26
Memory Allocation
  • In C, memory is allocated using the new
    operator int p new int
  • An array is allocated using new operator int
    p_arr new int10
  • Memory is freed with delete operator delete p
  • An array is deleted with delete operator
    delete p_arr

27
Memory Allocation
  • new and delete call constructors and
    destructors, so dont use malloc() and free().
  • Remember to match new with delete and new with
    delete.

28
main.cpp
  • include ltiostreamgt
  • include "Stack.h"
  • using namespace std
  • int main()
  • Stack s(10)
  • int el(5)
  • s.push(el)
  • cout ltlt "Please enter a number "
  • cin gtgt el
  • s.push(el)
  • cout ltlt "Stack contains " ltlt s.pop() ltlt " and "
    ltlt s.pop() ltlt endl
  • return 0

29
Object Definition and Use
  • We define an object just as we defined a built-in
    type.
  • Constructor arguments are passed in parenthesis.
  • Built-in types can also be called with
    constructor semantics.
  • Member access is done using . operator (just as
    with struct) s.push(10)

30
Miscellany
  • using namespace std
  • A namespace is a collection of related data,
    functions, objects, etc. Everything in the
    standard library is in the namespace std. This
    line eliminates the need for preceding everything
    with std
  • cin gtgt el
  • The new way of inputting. cin is the standard
    input stream, usually connected to the keyboard.
    No need for and specifying format (as in
    scanf()).

31
Using Several Files
32
Using Several Files
  • The program should be separated into several
    files, each containing a logical unit.
  • Usually each class implementation in a separate
    .cpp file, each class declaration in a separate
    .h file, plus a file with main().
  • Do not forget to include the header files you
    need (i.e. do not forget to include Stack.h when
    using Stack).
  • Do not forget the ifndef-define construct in
    header files.

33
Compiling Together
  • Compiling with g
  • g main.cpp Stack.cpp o Stack.exe
  • Compiling with Visual Studio .NET
  • Open a new empty Win32 Console Project.
  • Add all source files (both .cpp and .h) to it.
  • Run Build Stack from Build menu.
Write a Comment
User Comments (0)
About PowerShow.com