Upload
emailharmeet
View
8.628
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Basic Concepts of OOPs
Lecture 2
Object Oriented Programming with C++
Basic Concepts of Object-Oriented Programming
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Basic Concepts of Object-Oriented Programming
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Starting with C++ classes
• C with Classes
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.
Remember :
• A class is a type
• object of this class is just a variable
Definition of a class
class name {// members
}
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
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
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, …
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.
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.
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
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.
= +
Data
Functions
Object A
Data
Functions
Object B
Data
Functions
Object C
Communication
Organization of data and functions in OOP
Private
Public
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.
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.
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.
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.
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.
BirdAttributes
FeathersLay eggs
Flying BirdAttributes
….….
Nonflying BirdAttributes
……
RobinAttributes
….….
SwallowAttributes
……
PenguinAttributes
……
KiwiAttributes
……
Polymorphism
• Polymorphism is another important OOP concept.
• Polymorphism, a Greek term, means the ability to take more than one form.
Shape
Draw()
Circle Object
Draw(circle)
Box object
Draw(box)
Triangle object
Draw(tringle)
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.
Message Passing
• An object oriented program consists of a set of objects that communicate with each other.
• Employee.salary (name);
Object message information
Thanks