OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

33
OBJECT ORIENTED PROGRAMMING LECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena

Transcript of OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

Page 1: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

OBJECT ORIENTED PROGRAMMINGLECTURE 2

INSTRUCTOR: Rashi Garg

COORDINATOR: Gaurav Saxena

Page 2: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

EVALUATION STRATEGY

Examination T-1 T-2 End Sem Exam

Percentage of marks 20 20 35

Duration in Hours 1 1 2

Syllabi Coverage:

(i) T-1(Mid Term) Syllabi covered up-to T-1.

(ii) T-2: (Mid Term) Syllabi covered between T-1 & T-2.

(iii) End Semester Exam Full Syllabi

Teachers Assessment

Assignments, Tutorials, 25% EntireSemester

Quizzes, home work &

Regularity in attendance

Page 3: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

REFERENCE MATERIAL Reference Books: Michael R Blaha, James Rumbaugh, "Object Oriented

Modeling and Design with UML", Pearson.  James Rumbaugh et. al, “Object Oriented Modeling and

Design”, PHI Grady Booch, James Rumbaugh, Ivar Jacobson, “The

Unified Modeling Language UserGuide”, Pearson Education

Naughton, Schildt, “The Complete Reference JAVA2”, TMH Mark Priestley “Practical Object-Oriented Design with

UML”, TMH Booch, Maksimchuk, Engle, Young, Conallen and Houstan,

“Object Oriented Analysisand Design with Applicile ations”, Pearson Education

Pandey, Tiwari, “ Object Oriented Programming with JAVA” , Acme Learning

Page 4: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

INTRODUCTION

C++ programming language was developed by AT&T Bell Laboratories.

Simula 67 was earliest Object Oriented Language.

Paradigm means organizing principle of a program. It is an approach to programming.

Page 5: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

PROGRAMMING PARADIGMS

Procedural Programming Modular Programming Object Oriented Programming

Page 6: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

PROCEDURAL PROGRAMMING

Example: COBOL, FORTRAN, BASIC List of instructions where each statement

tells computer to do something. Focus is on processing Algorithm need to perform desired

computation Decide which procedure you want use best

algorithm you find Emphasis is on doing things But what happens to DATA ? IMP PART OF INVENTORY IS DATA NOT THE

FUNCTION TO CHECK OR DISPLAY DATA.

Page 7: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

PROCEDURAL PROGRAMMING CONTD…

Data types are used and worked upon by many functions.

If function makes changes to the data type, this change need to be propagated to all function that uses this data type

This is very time consuming. Does not model real world: vehicle is an

object which is capable of moving Procedural programming concerned about

the procedure (or doing things) i.e moving part not the vehicle

Page 8: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

LIMITATION

Emphasis on algorithm (or procedure) rather than on Data.

Change in data type being processed needs to be propagated to all functions.

Does not Model real world

Page 9: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

MODULAR/ STRUCTURED PROGRAMMING

Example: C Aimed at improving the clarity, quality, and

development time of programs Make extensive use of subroutines,

block structures and for and while loops

Page 10: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

OBJECT ORIENTED PROGRAMMING

Example: C++, c#, Java, Smalltalk Views problem in terms of objects involved

rather than procedure for doing it. OBJECT: identifiable entity with some

characteristics and behaviour EXAMPLE: “Orange is an object”

CHARACTERISTICS: spherical shaped, colour is orange

BEHAVIOUR: juicy/ citrus, tastes sweet/ sour CHARACTERISTICS DATA BEHAVIOUR FUNCTION

Page 11: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

WHAT IS AN OBJECT ?

Informally, an object represents an entity, either physical, conceptual, or software.

Physical entity

Conceptual entity

Software Entity

Truck

Chemical Process

Linked List

Page 12: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

AN OBJECT HAS STATE

The state of an object is one of the possible conditions in which an object may exist.

The state of an object normally changes over time.

State is represented by attributes and relationships.

Page 13: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

AN OBJECT HAS BEHAVIOR

Behavior determines how an object acts and reacts.

The visible behavior of an object is modeled by the set of messages it can respond to (operations the object can perform).

Behavior is represented by operations, methods, and state machines.

Page 14: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

AN OBJECT HAS IDENTITY

Each object has a unique identity, even if the state is identical to that of another object.

Professor “J Clark” teaches Biology

Professor “J Clark” teaches Biology

Page 15: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

PROCEDURAL VS OOP

• Procedural

Withdraw, deposit, transfer

• Object Oriented

Customer, money, account

Page 16: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

Function 1

Function 2

Function 3

Data 1

Data 2

Properties

Properties

Behaviour

Behaviour

Object 1 Object 2

All DATA is openly available to all functions in program

DATA and FUNCTIONS enclosed within objects and new objects communicate with one another

Page 17: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

DIFFERENCE BETWEEN PROCEDURE ORIENTED PROGRAMMING (POP) & OBJECT ORIENTED PROGRAMMING

The unit in procedural programming is function, and unit in object-oriented programming is class

Procedural programming concentrates on creating functions, while object-oriented programming starts from isolating the classes, and then look for the methods inside them.

Procedural programming separates the data of the program from the operations that manipulate the data, while object-oriented programming focus on both of them

Page 18: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CONTD ….

Page 19: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CONTD …

Page 20: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

WHAT IS A CLASS ???

Class represents a group of objects that share common properties and relationships.

EXAMPLE: Car is an object ? Characteristics : steering wheel, brakes etc. Behavior: Mobility.

But Car is a CLASS “Opel Astra” is a object Car subclass of class AUTOMOBILES

Page 21: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CLASS CONTD…

class class_name {

private data and functionspublic:public data and functions

} object_names;

Where,  class_name is a valid identifier for the class

  object_names is an optional list of names for

objects of this class.

Page 22: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CONTD …

#define SIZE 100// This creates the class stack.class stack {

int stck[SIZE];int tos;public:

void init();void push(int i);int pop();

};

By default members are private

Page 23: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CONTD …

Creating objectstack mystack;

Code a Function

void stack::push(int i){

if(tos==SIZE) {cout << "Stack is full.\n";return;

}stck[tos] = i;tos++;

}

Scope Resolution Operator

Page 24: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CONTD …

When you refer to a member of a class from a piece of code that is not part of the class, you must always do so in conjunction with an object of that class.

Stack stack1. stack2;Stack1.init();

Page 25: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

COMPLETE EXAMPLE

#include <iostream>using namespace std;#define SIZE 100// This creates the class stack.

class stack {int stck[SIZE];int tos;public:void init();void push(int i);int pop();};

void stack::init(){

tos = 0;}void stack::push(int i){

if(tos==SIZE) {cout << "Stack is full.\n";

return;}stck[tos] = i;tos++;}int stack::pop(){if(tos==0) {cout << "Stack underflow.\n";return 0;}tos--; reutrn stck[tos];}

int main(){stack stack1, stack2; stack1.init();stack2.init();stack1.push(1);stack2.push(2);stack1.push(3);stack2.push(4);cout << stack1.pop() << " ";cout << stack1.pop() << " ";cout << stack2.pop() << " ";cout << stack2.pop() << "\n";return 0;}

Page 26: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

CONTD …

stack1.tos=0 ?

Page 27: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

HOW OOP OVERCOMES PROCEDURAL’S PROBLEM

Gives data prime consideration, by providing interface through functions associated with it.

An object is a complete entity: has all data & associated functions within it. In case of change only class gets changed

because it is complete in itself. All function are defined in class, they get to see

change immediately. Just as we replace TRANSISTOR in an equipment. It is a complete part, complete with its component and

interface. Thus can be replaced without altering rest of

machinery

Page 28: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

GENERAL CONCEPTS OF OOP

Data Abstraction Data Encapsulation Modularity Inheritance (Hierarchy) Polymorphism

Page 29: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

DATA ABSTRACTION

Act of representing essential features without including background details or explanations EXAMPLE: Driving Car

Switch Board

Page 30: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

ENCAPSULATION

Wrapping of Data and Functions (that operate on the data) into a single unit (called class).

Only way to access DATA is by FUNCTIONS These are called MEMBER FUNCTIONS Data cannot be accessed directly Data is hidden and safe from accidental

alteration While encapsulating abstraction is implemented Encapsulation is a way to implement Data

Abstraction EXAMPLE: 1 dept cannot access data of other

dept Dept Data and Dept Employee encapsulated into

single entity Dept

Page 31: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

INHERITANCE

Capability of one class of things to inherit capabilities or properties from another class

Vehicles

Automobiles

Pulled Vehicles

Bus Car Cart Rickshaw

Base Class

Derived Class

Page 32: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

ADVANTAGES OF INHERITANCE

Idea of reusability. Allows addition of additional features to existing class without modifying it

Transitive Nature: A inherits B then all subclasses of A will automatically inherit properties of B

Page 33: OBJECT ORIENTED PROGRAMMING L ECTURE 2 INSTRUCTOR: Rashi Garg COORDINATOR: Gaurav Saxena.

POLYMORPHISM Two or more functions can share the same name

as long as their parameter declarations are different. In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading.#include <iostream>

using namespace std;// abs is overloaded three waysint abs(int i);double abs(double d);long abs(long l);int main(){cout << abs(-10) << "\n";cout << abs(-11.0) << "\n";cout << abs(-9L) << "\n";return 0;}

int abs(int i){cout << "Using integer abs()\n";return i<0 ? -i : i;}double abs(double d){cout << "Using double abs()\n";return d<0.0 ? -d : d;}

long abs(long l){cout << "Using long abs()\n";return l<0 ? -l : l;}