Sunteți pe pagina 1din 7

01.

Introduction to objects

Abstraction Object Oriented programming Objects

EE208 - Object Oriented Programming, Francesco Fusco

Abstraction

All programming languages provide abstractions The complexity of the problems youre able to solve is directly related to the kind and quality of abstraction ASSEMBLY
.data mesg: .ascii "Hello!\n\0" .text .globl main main: sub $sp, 24 sw $31, 20($sp) la $4, mesg jal printf la $4, mesg jal printf lw $31, 20($sp) add $sp, 24 j $31

IMPERATIVE
int main() { char *msg=Hello!\n; ptintln(msg); } return 0;

OBJECT - ORIENTED
int main() { Message msg; msg.setText(Hello!); msg.print(); } return 0;

EE208 - Object Oriented Programming, Francesco Fusco

Object Oriented Programming (OOP)

All real world's objects are also part of a class of objects that have characteristics and behaviors in common A class describes a set of objects that have identical characteristics and behaviors OOP consists of designing new types (classes), creating variables of these types (instances or objects) and manipulating them (sending messages)

EE208 - Object Oriented Programming, Francesco Fusco

Object Oriented Programming: Why?

Provides tools to represent elements in the problem space The program is allowed to adapt itself to the lingo of the problem by adding new types of objects When you read the code describing the solution, youre reading words that also express the problem Allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run

EE208 - Object Oriented Programming, Francesco Fusco

Objects: interface and hidden implementation

Client is the programmer using the class The interface estabilishes what requests you can make for a particular object Hidden implemenattion:

Light lt; lt.on();

Client

Light
on() off() brighten() dim()

Class Interface

Hidden implementation

the code that satisfies the request hidden information (data)

Light::on() { //do stuff ... return; }

Keeps client programmers' hands off portions they shouldn't touch Allows class designer to change the internal working of the class without worrying about how it will affect the client
5

EE208 - Object Oriented Programming, Francesco Fusco

Objects: reusage (1)

Code reusage is one of the greatest advantages of Object Oriented Programming:


1) Simple use of class 2) Composition or aggregation:
Car Engine

Your new class is made up of any other number of other objects Composition is referred to as a has-a relationship
6

EE208 - Object Oriented Programming, Francesco Fusco

Objects: reusage (2)


3) Inheritance :

Shape
draw() erase() move() setColor() getColor()

Your new class is a clone (derived) of the original class (base) with additions or modifications Inherintance is referred to as a is-a relationship Two ways to differentiate new derived class from base class:

Adding new functions Changing the behavior (overriding)

Circle

Square

It's possible to treat an object as its base type instead of its specific type. Messages sent to this object will be resolved in real time (polymorphism)
7

EE208 - Object Oriented Programming, Francesco Fusco

S-ar putea să vă placă și