Top Banner
Introduction to Object Oriented Programming CIS 230 01-03-06
26

Introduction to Object Oriented Programming CIS 230 01-03-06.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Object Oriented Programming CIS 230 01-03-06.

Introduction to Object Oriented Programming

CIS 230

01-03-06

Page 2: 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

Page 3: Introduction to Object Oriented Programming CIS 230 01-03-06.

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.

Page 4: Introduction to Object Oriented Programming CIS 230 01-03-06.

Why OOP?

• User Interface– 2/3 of an applications code– (windowing, pull down menus, graphics, etc.)– need to be able to write this easily

Page 5: Introduction to Object Oriented Programming CIS 230 01-03-06.

Misconceptions

1. Makes everything easy

2. You can reuse everything

Page 6: Introduction to Object Oriented Programming CIS 230 01-03-06.

Learning curves

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

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

Page 7: Introduction to Object Oriented Programming CIS 230 01-03-06.

Encapsulation

• The process of combining both properties (data) and behaviors (functions) into one entity.Examples:Integers –

Circle –

Check –

digits+, -, *, /

radius, circumference, areahow to calc circumference, how to calc area, …

amount, check number, date, commentwrite, sign, cash, record

Page 8: Introduction to Object Oriented Programming CIS 230 01-03-06.

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

Page 9: Introduction to Object Oriented Programming CIS 230 01-03-06.

C++ partial examples

• name might contain:

• circle might have:

what it ishow to get to it

data – radiushow to get the radiuscalculate circumferencecalculate area

Page 10: Introduction to Object Oriented Programming CIS 230 01-03-06.

C++ form

class ClassName

{

private:

data

public:

functions

};

This defines a class, nothing exists yet

prototypes:to keep the definition compact

Page 11: Introduction to Object Oriented Programming CIS 230 01-03-06.

Circle

class Circle{ private: float radius; public: void store_radius(float); float calc_circum(void); float calc_area(void); float return_radius(void);};

Page 12: Introduction to Object Oriented Programming CIS 230 01-03-06.

Class Functions

void Circle::store_radius(float value)

{

radius = value;

}

Class Name two colons

Page 13: Introduction to Object Oriented Programming CIS 230 01-03-06.

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

Page 14: Introduction to Object Oriented Programming CIS 230 01-03-06.

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)

Page 15: Introduction to Object Oriented Programming CIS 230 01-03-06.

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”;

Page 16: Introduction to Object Oriented Programming CIS 230 01-03-06.

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’;

Page 17: Introduction to Object Oriented Programming CIS 230 01-03-06.

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

Page 18: Introduction to Object Oriented Programming CIS 230 01-03-06.

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

Page 19: Introduction to Object Oriented Programming CIS 230 01-03-06.

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

Page 20: Introduction to Object Oriented Programming CIS 230 01-03-06.

Protecting the data

Circle circle1, circle2;

cin >> x;

circle.store_radius(x);

y = circle2.calc_circum();

what if x was -3.4?

Page 21: Introduction to Object Oriented Programming CIS 230 01-03-06.

Protecting the data

• A new store_radius() method:Circle::store_radius(float value){ if (value >= 0) radius = value; else radius = -1 * value;}

Page 22: Introduction to Object Oriented Programming CIS 230 01-03-06.

Constructor

• Automatically invoked when a new object is created:– when:

– or:

– or:

Page 23: Introduction to Object Oriented Programming CIS 230 01-03-06.

Constructor

• Default Constructor

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

• Can initialize data

Page 24: Introduction to Object Oriented Programming CIS 230 01-03-06.

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}

Page 25: Introduction to Object Oriented Programming CIS 230 01-03-06.

Destructor

• Automatically invoked when an object goes out of existence– when:

– or:

– or:

Page 26: Introduction to Object Oriented Programming CIS 230 01-03-06.

Destructor

• Default Destructor

• May cleanup any possible side effects

• ~ClassName:~Circle()