Lecture 2

26
Basic Concepts of OOPs Lecture 2 Object Oriented Programming with C++

description

 

Transcript of Lecture 2

Page 1: Lecture 2

Basic Concepts of OOPs

Lecture 2

Object Oriented Programming with C++

Page 2: Lecture 2

Basic Concepts of Object-Oriented Programming

• Objects

• Classes

• Data abstraction and encapsulation

• Inheritance

• Polymorphism

• Dynamic binding

• Message passing

Page 3: Lecture 2

Basic Concepts of Object-Oriented Programming

• Objects

• Classes

• Data abstraction and encapsulation

• Inheritance

• Polymorphism

• Dynamic binding

• Message passing

Page 4: Lecture 2

Starting with C++ classes

• C with Classes

Page 5: Lecture 2

Classes and Objects

• A class is a definition of an object.

• It's a type just like int.

• A class resembles a struct with just one difference : – All struct members are public by default. – All classes members are private by default.

Page 6: Lecture 2

Remember :

• A class is a type

• object of this class is just a variable

Page 7: Lecture 2

Definition of a class

class name {// members

}

Page 8: Lecture 2

A Simple Class

class item

{

int number; // variables declaration

float cost; // private by default

Public:

void getdata(int a, float b); // functions declaration

void putdata(void); // using prototype

}; // ends with semicolon

Page 9: Lecture 2

9

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 10: Lecture 2

10

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 11: Lecture 2

11

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 12: Lecture 2

12

Why do we care about objects?

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

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

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

Page 13: Lecture 2

Example: The Person class#include<string>

#include<iostream>

class Person{

char name[20];

int yearOfBirth;

public:

void displayDetails() {

cout << name << " born in "

<< yearOfBirth << endl;

}

//...

};

private data

public processes

Page 14: Lecture 2

14

The two parts of an object

Object = Data + Methods

or to say the same differently:

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

= +

Page 15: Lecture 2

Data

Functions

Object A

Data

Functions

Object B

Data

Functions

Object C

Communication

Organization of data and functions in OOP

Private

Public

Page 16: Lecture 2

Class• User-defined data type

• Objects are variables of the type class

• Any number of objects can be created from one class

• Collection of objects of similar user-defined data typesfruit mango;

Will create an object mango belonging to the class fruit.

Page 17: Lecture 2

Data Encapsulation

The wrapping up of data and functions into a single unit ( called class) is known as encapsulation.

• Class is not accessible by outside world

• Only its own functions can access it

The insulation of the data from direct access by the program is called data hiding or information hiding.

Page 18: Lecture 2

Data Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations.

• Classes use the concept of abstraction

• They are defined as a list of abstract attribute such as size, weight, cost and functions to operate on these attributes.

Page 19: Lecture 2

Trivia (something of small importance)

• Attributes also called data members– Because the hold information.

• Functions that operate on these data are called methods or member functions.

Page 20: Lecture 2

Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class.

• It support the concept of hierarchical classification.

• Bird robin is a part of the class flying bird which is again a part of the class bird.

Page 21: Lecture 2

BirdAttributes

FeathersLay eggs

Flying BirdAttributes

….….

Nonflying BirdAttributes

……

RobinAttributes

….….

SwallowAttributes

……

PenguinAttributes

……

KiwiAttributes

……

Page 22: Lecture 2

Polymorphism

• Polymorphism is another important OOP concept.

• Polymorphism, a Greek term, means the ability to take more than one form.

Page 23: Lecture 2

Shape

Draw()

Circle Object

Draw(circle)

Box object

Draw(box)

Triangle object

Draw(tringle)

Page 24: Lecture 2

Dynamic Binding

• Binding refers to the linking of a procedure call to the code to be executed in response to the call.

• Dynamic binding also know as late binding means the code associated with a given procedure call is not known until the time of the call at run-time.

Page 25: Lecture 2

Message Passing

• An object oriented program consists of a set of objects that communicate with each other.

• Employee.salary (name);

Object message information

Page 26: Lecture 2

Thanks