oop lec 2

20
CSC 103 - Object Oriented Programming Spring, 2011 Lecture 2, Background 18 th Feb, 2011 Instructor: M. Anwar-ul-Haq

Upload: anwar-ul-haq

Post on 28-May-2015

891 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Oop lec 2

CSC 103 - Object Oriented Programming

Spring, 2011

Lecture 2, Background

18th Feb, 2011

Instructor: M. Anwar-ul-Haq

Page 2: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

2

Imperative Programming

What is Imperative Programming?

• Imperative programming is the type of programming you do when you essentially just have a list of commands that work from the top down to the bottom, and then the program is complete.

• This means that it doesn’t have any functions or the like.

Page 3: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

3

Imperative Programming

• This type of programming is very simplistic, which makes it very difficult, time-consuming, and inefficient to do complex programs.

Page 4: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

4

Procedural Programming

• What is Procedural Programming?

• Procedural programming is the type of programming you do when you make use of procedures (yet another synonym for functions, processes, and subroutines).

Page 5: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

5

Procedural Programming

• This type of programming is higher-level than imperative and allows you to accomplish more complex programs.

• However, even procedural programming isn’t very effective when it comes to very large programs.

• FORTRAN and C are two popular procedural programming languages.

Page 6: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

6

Object Oriented Programming

• What is Object-Oriented Programming (OOP)?

• Object-oriented programming is the type of programming in which you make use of objects. Objects are essentially meant to be representative of a potentially physical object. Objects contain their own data and methods (another synonym for functions).

Page 7: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

7

Object Oriented Programming

• Object-oriented programming allows for the rapid development of larger scale programs.

• Instead of having to have all the commands in one location, you can break the code down into separate objects.

• This allows for much more manageable and readable code, which in turn allows for more efficient programming.

Page 8: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

8

Object Oriented Programming

• C++, Java, and PHP are popular object-oriented programming languages. You could consider JavaScript to be an OOP language as well, though it doesn’t make use of the typical “class” as others do.

Page 9: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

9

Which is Best?

• OOP programs are best if they meet one or more of these criteria:– Are fairly large (more than a couple hundred

lines of code) and will be run on general computers.

– Are fairly complex (more than just output simple data).

– Will be maintained and added to over a period of time.

Page 10: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

10

What is Object Oriented Programming?

An object is like a black box.

The internal details are hidden.

• Identifying objects and assigning responsibilities to these objects.

• Objects communicate to other objects by sending messages.

• Messages are received by the methods of an object

Page 11: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

11

What is an object?

• Tangible Things as a car, printer, ...

• Roles as employee, boss, ...

• Incidents as flight, overflow, ...

• Interactions as contract, sale, ...

• Specifications as colour, shape, …

Page 12: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

12

So, what are objects?

• an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain.

Or

• An "object" is anything to which a concept applies.

etc.

Page 13: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

13

Why do we care about objects?

• Modularity - large software projects can be split up in smaller pieces.

• Reusability - Programs can be assembled from pre-written software components.

• Extensibility - New software components can be written or developed from existing ones.

Page 14: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

1414

What is an Object?

• Real world consists of objects (desk, your television set, your bicycle).

• They share two characteristics: They all have state and behavior.

• Bicycles have state (current gear, current speed) and behavior (changing gear, applying brakes).

• Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation.

Page 15: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

15

Real-World Modeling

• Attributes/State– Bicycle: Speed and Gear.– People: eye color and job etc.– Cars: horsepower and number of doors.

• Behavior: Something a real-world object does in response to some stimulus.– Bicycle: changeGear, speedUp, applyBrakes

and printStates.

Page 16: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

1616

What Is an Object?• Grouping code into individual software objects

provides a number of benefits, including:– Modularity: The source code for an object can

be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.

– Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.

Page 17: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

17

What Is an Object?

– Code re-use: If an object already exists, you can use that object in your program.

– Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as it is.

Page 18: Oop lec 2

OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS),

Islamabad

18

The two parts of an object

Object = Data (state) + Methods (behaviour)

or to say the same differently

An object has the responsibility to know and the responsibility to do.

= +

Page 19: Oop lec 2

Examplevoid speedUp(int increment)

{

speed = speed + increment; }

void applyBrakes(int decrement) {

speed = speed - decrement; }

void printStates() {

cout<<"Speed:"<<speed<<endl;cout<<"Gear:"<<gear<<endl;

}};

#include <iostream>using namespace std;

class Bicycle {private:

int speed;int gear;

public:Bicycle(){

speed=0;gear=0;

}

Page 20: Oop lec 2

Example (contd..)void main(){

Bicycle bike1,bike2;

bike1.speedUp(10);bike1.printStates();

bike2.speedUp(15);bike2.changeGear(12);bike2.applyBrakes(5);bike2.printStates();

bike2.speedUp(10);bike2.changeGear(3);bike2.printStates();

}