01 Introduction OOPS

24
Object-Oriented Programming Prepared by:- Darshana Mistry eInfochips Training and Research Academy (eiTRA) eInfochips Institute of Training Research and Academi

description

By Darshna Mistry

Transcript of 01 Introduction OOPS

Page 1: 01 Introduction OOPS

Object-Oriented Programming

Prepared by:- Darshana Mistry

eInfochips Training and Research Academy (eiTRA)

eInfochips Institute of Training Research and Academics Limited

Page 2: 01 Introduction OOPS

Agenda

• Structured programming vs. OO programming– Structure and classes in c++

• Why OOPs?• Features of OOPs

– Abstraction– Encapsulation– Polymorphism– Inheritance

Page 3: 01 Introduction OOPS

STRUCTURED vs. OO PROGRAMMING

3

STRUCTURED PROGRAMMING:MAIN PROGRAM

FUNCTION 3FUNCTION 2

GLOBAL DATA

FUNCTION 5FUNCTION 4

FUNCTION 1

Page 4: 01 Introduction OOPS

OBJECT ORIENTED PROGRAMMING

4

Object 1 Object 2

Data

Function

Data

Function

Object 3

Data

Function

Page 5: 01 Introduction OOPS

OBJECT ORIENTED PROGRAMMING

Objects have both data and methods Objects of the same class have the same data

elements and methods Objects send and receive messages to invoke

actions

Key idea in object-oriented: 

The real world can be accurately described as a collection of objects that interact.

5

Page 6: 01 Introduction OOPS

Structures and Classes in C++

• Structures in C++ differ from those in C in that members can be functions.

• A special member function is the “constructor”, whose name is the same as the structure. It is used to initialize the object:

struct buffer {buffer()

{size=MAXBUF+1; front=rear=0;}char buf[MAXBUF+1];int size, front, rear;}

Page 7: 01 Introduction OOPS

Structures and Classes in C++

The idea is to add some operations on objectsof type buffer:

struct buffer {buffer()

{size=MAXBUF+1;front=rear=0;}char buf[MAXBUF+1];int size, front, rear;int succ(int i) {return (i+1)%size;}int enter(char);char leave();

}

Page 8: 01 Introduction OOPS

Structures and Classes in C++

The definition (body) of a member function can beincluded in the structure's declaration, or mayappear later. If so, use the name resolutionoperator (::)

int buffer::enter(char x) {

// body of enter }

char buffer::leave() {

// body of leave }

Page 9: 01 Introduction OOPS

Public and Private Members

Structures and classes are closely related in C++:

struct x { <member-dclns> };

is equivalent to

class x { public: <member-dclns>};

Difference: by default, members of a structure arepublic; members of a class are private. So,

class x { <member-dclns> };

is the same as

struct x { private: <member-dclns> };

Page 10: 01 Introduction OOPS

C++ Strong Typing

• There are 6 principal situations in which C++ has stronger typing than C.

1. The empty list of formal parameters means "no arguments" in C++.

• In C, it means "zero or more arguments", with no type checking at all. Example: char * malloc();

Page 11: 01 Introduction OOPS

C++ Strong Typing (cont’d)

2. In C, it's OK to use an undefined function; no type checking will be performed. In C++, undefined functions are not allowed.

Example: main() f( 3.1415 );

// C++: error, f not defined

// C: OK, taken to mean int f()

Page 12: 01 Introduction OOPS

C++ Strong Typing (cont’d)

3. A C function, declared to be value-returning, can fail to return a value. Not in C++. Example:

double foo() { /* ... */

return; }main() {

if ( foo() ) { ... } ...}// C : OK// C++: error, no return

value.

Page 13: 01 Introduction OOPS

C++ Strong Typing (cont’d)

4. In C, assigning a pointer of type void* to a pointer of another type is OK. Not in C++. Example:

int i = 1024;

void *pv = &i;

// C++: error,

// explicit cast required.

// C : OK.

char *pc = pv;

int len = strlen(pc);

Page 14: 01 Introduction OOPS

C++ Strong Typing (cont’d)

5. C++ is more careful about initializing arrays: Example:

char A[2]="hi";

// C++: error,

// not enough space for '\0'

// C : OK, but no '\0' is stored.

It's best to stick with char A[] = "hi“;

Page 15: 01 Introduction OOPS

C++ Strong Typing (cont’d)

6. Free store (heap) management. In C++, we use new and delete, instead of malloc and free.

• malloc() doesn't call constructors, and free doesn't call destructors.

• new and delete are type safe.

Page 16: 01 Introduction OOPS

Why OOP?

Save development time (and cost) by reusing code

once an object class is created it can be used in other applications

Easier debuggingclasses can be tested independently

reused objects have already been tested

16

Page 17: 01 Introduction OOPS

Object Oriented Programming

• A methodology of programming• Major principles:

1. Data Abstraction.2. Encapsulation.3. Information Hiding.4. Polymorphism (dynamic binding).5. Inheritance.

Page 18: 01 Introduction OOPS

Object-Oriented Programming

Object-oriented programming is a programmingmethodology characterized by the following concepts:

1. Data Abstraction: problem solving via the formulation of abstract data types (ADT's).

2. Encapsulation: the proximity of data definitions and operation definitions.

3. Information hiding: the ability to selectively hide implementation details of a given ADT.

4. Polymorphism: the ability to manipulate different kinds of objects, with only one operation.

5. Inheritance: the ability of objects of one data type, to inherit operations and data from another data type.

Page 19: 01 Introduction OOPS

O-O Principles and C++ Constructs

O-O Concept C++ Construct(s)

Abstraction Classes Encapsulation ClassesInformation Hiding Public and Private MembersPolymorphism Operator overloading,

templates, virtual functions

Inheritance Derived Classes

Page 20: 01 Introduction OOPS

Abstraction

• Focus only on the important facts about the problem at hand

• to design, produce, and describe so that it can be easily used without knowing the details of how it works.

Analogy:• When you drive a car, you don’t have to know

how the gasoline and air are mixed and ignited.• Instead you only have to know how to use the

controls.• Draw map

20

Page 21: 01 Introduction OOPS

Encapsulation

Also known as data hidingOnly object’s methods can modify

information in the object.

Analogy: ATM machine can only update accounts

of one person or object only.

21

Page 22: 01 Introduction OOPS

Polymorphism

the same word or phrase can mean different things in different contextsAnalogy: •Person role

In English, bank can mean side of a river or a place to put money

move -

22

Page 23: 01 Introduction OOPS

Inheritance

Inheritance—a way of organizing classes Term comes from inheritance of traits like

eye color, hair color, and so on. Classes with properties in common can be

grouped so that their common properties are only defined once.

Superclass – inherit its attributes & methods to the subclass(es).

Subclass – can inherit all its superclass attributes & methods besides having its own unique attributes & methods. 23

Page 24: 01 Introduction OOPS

An Inheritance Hierarchy

24

Vehicle

Automobile Motorcycle Bus

Sedan Sports Car School BusLuxury Bus

What properties does each vehicle inherit from the types of vehicles above it in the diagram?

Superclass

Subclasses