Introduction to Object

processroguishSoftware and s/w Development

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

62 views

Introduction to Object
Oriented Programming

CIS 230

01
-
03
-
06

Introduction

What Makes an object oriented
programming language?

1.
Objects include both data and instruction

2.
Objects inherit behavior from existing
classes

3.
Objects determine at runtime how to resond
to messages

General Types of

OOP languages

1.
Hybrids


C++, Objective C, Object Pascal


Provide compatibility with older languages

2.
Pure OOP languages


Smalltalk, Actor, Java


Programmer is forced to create object
-
oriented code.

Why OOP?


User Interface


2/3 of an applications code


(windowing, pull down menus, graphics, etc.)


need to be able to write this easily

Misconceptions

1.
Makes everything easy




2.
You can reuse everything

Learning curves

1.
Must learn the tools provided to create
object
-
oriented programs



2.
Must learn to work in an object
-
oriented
programming style

Encapsulation


The process of combining both properties
(data) and behaviors (functions) into one
entity.

Examples:

Integers




Circle




Check


digits

+,
-
, *, /

radius, circumference, area

how to calc circumference, how to calc area, …

amount, check number, date, comment

write, sign, cash, record

Class


A definition of an object (or for a group of
similar objects)


A template for creating objects


Note:

Each object belongs to only one class

C++ partial examples


name

might contain:



circle

might have:




what it is

how to get to it

data


radius

how to get the radius

calculate circumference

calculate area

C++ form

class ClassName

{


private:


data


public:


functions

};

This defines a class,

nothing exists yet

prototypes:

to keep the definition compact

Circle

class Circle

{


private:


float radius;


public:


void store_radius(float);


float calc_circum(void);


float calc_area(void);


float return_radius(void);

};

Class Functions



void Circle::store_radius(float value)

{


radius = value;

}


Class Name

two colons

Class Functions

float Circle::calc_circum(void)

{


float circum;


circum = 3.14 * 2 * radius;


return circum;

}

float Circle::calc_area(void)

{


return (3.14 * radius * radius);

}

Local variable

Local variable wasn’t

really needed

Instance


Instance


an actual variable of the class


void main()

{


Circle circle1, circle2;


float x, y, z, w;




}

Each has its own data (i.e.
radius) but they share the
functions (methods)

Invoking methods


instance.method( );


circle1.store_radius(6);

cout << “Please enter a circle’s radius ”;

cin >> x;

circle2.store_radius(x);

y = circle1.calc_circum();

z = circle1.calc_area();

cout << “ A 6 inch circle has a circumference
\
n”;

cout << “ of “ << y << “ and an area of “ << z << “
\
n”;

Invoking methods

cout << “A “ << x << “ inch circle has a circumference
\
n”;

cout << “of “ << circle2.calc_circum();

cout << “and an area of “ << circle2.calc_area() << “
\
n”;

cout << “The sum of the two areas is “;

w = circle1.calc_area() + circle2.calc_area();

cout << w << ‘
\
n’;

Message


A request to an object


Must have at least two parts:

1.
an instance name

2.
the name of a method


Example:


first_circle.assign_radius(7);

first_circle.calc_area();

they may require more

instance

method

OOP Review


Class


A definition (template) of an object


Contains: data & methods


Encapsulation


Data


private


Methods


public


Instance (object)


An actual variable of
the class


Each instance has its own data but jointly use
the methods

OOP Review


Data Abstraction


The ability to
manipulate the data without knowledge of
the data’s internal format


Use methods to assign/retrieve values


Example:

circle1.store_radius(6);

y = circle1.calc_circum();

z = circle1.calc_area();

Assign

Retrieve

Protecting the data

Circle circle1, circle2;



cin >> x;

circle.store_radius(x);

y = circle2.calc_circum();


what if x was
-
3.4?

Protecting the data


A new store_radius() method:

Circle::store_radius(float value)

{


if (value >= 0)


radius = value;


else


radius =
-
1 * value;

}

Constructor


Automatically invoked when a new object
is created:


when:



or:



or:


Constructor


Default Constructor




Help establish the link between the
specific object and its class’ methods



Can initialize data

Constructors


Same name as the class


Can NOT return anything (not
even void)

class Circle

{


private:


float radius;


public


Circle();




};

Circle::Circle()

{


radius = 1; //Default
-

unit circle

}

Destructor


Automatically invoked when an object
goes out of existence


when:



or:



or:

Destructor


Default Destructor




May cleanup any possible side effects



~ClassName:

~Circle()