Object Oriented Programming: Introduction - Student.cs.uwaterloo.ca

carenextSoftware and s/w Development

Nov 18, 2013 (3 years and 8 months ago)

85 views

Lecture
-
10

Object Oriented Programming:
Introduction

2.18

Structure Example

OOP:Concepts


Structured Programming


Data and operations on data are separate entities


Group logically related operations in one module


Object
-
Oriented Programming


Encapsulation
:
Hide data from external world and expose
only valid operations on data.


Abstraction
: Separate implementation from usage


Messaging
: Objects interact with each other with
messages, usually method calls.


Inheritance

: Controlled reuse of code.


Polymorphism
: Common interface for different objects

3

Object


Object = Data + Operation

4

Object form

re and
im

of

current
struct
, x

Method invocation

Method,

or member function

Alternatively

Structure form

A
struct
/class defines a full
fledged type like
int

or
float.


Declaration

Definition

this

Pointer


A constant pointer named
this

is
implicitly passed to each method






this

points to the
struct
/class that was used to invoke the method


this
is optional (implicitly used) while accessing member variables.


this

can be used to disambiguate member from parameter:






this

is a reserved keyword and defined within the
scope

of a
struct
/class.

5

Constructor


A special method having the same name as the
struct
/class name


Constructors may have parameter(s) but no return type


Implicitly called right after the object is allocated in memory

Constructor

7


A Constructor without
any
parameter is
called
default
constructor


Constructors can be
overloaded


Constructor may have
initializer lists

for
initializing member
variables


Once a constructor is
declared,
static
struct

initialization
is
disallowed.


These 3 constructors
can be condensed into
one constructor with
default parameters.

Same as:

re = r;

im

= i;