01 Object Oriented Concepts
01 Object Oriented Concepts
LECTURE-1
Introduction:
Programmers write instructions in various programming languages to perform their computation
tasks such as:
(i) Machine level Language
(ii) Assembly level Language
(iii) High level Language
The first high-level programming languages were designed in the 1950s. Now there are dozens of
different languages, including Ada , Algol, BASIC, COBOL, C, C++, JAVA, FORTRAN, LISP,
Pascal, and Prolog. Such languages are considered high-level because they are closer to human
languages and farther from machine languages. In contrast, assembly languages are considered low-
level because they are very close to machine languages.
In the procedure-oriented approach, the problem is viewed as a sequence of things to be done such
as reading, calculation, and printing.
Procedure-oriented programming basically consists of writing a list of instructions or actions for the
computer to follow and organizing these instructions into functions.
Main program
In procedural programming, the program is divided into In object-oriented programming, the program is
small parts called functions. divided into small parts called objects.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way Object-oriented programming provides data hiding
of hiding data so it is less secure. so it is more secure.
In procedural programming, there is no concept of data In object-oriented programming, the concept of data
hiding and inheritance. hiding and inheritance is used.
Procedural programming is used for designing medium- Object-oriented programming is used for designing
sized programs. large and complex programs.
Procedural programming uses the concept of procedure Object-oriented programming uses the concept of
abstraction. data abstraction.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Object A Object B
Data Data
Communication
Functions Functions
Object C
Functions
Data
LECTURE-3
1. Objects
2. Classes
3. Data abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic binding
8. Message passing
OBJECTS
Objects are the basic run-time entities in an object-oriented system. They may represent a person, a
place, a bank account, a table of data or any item that the program must handle.
The fundamental idea behind object-oriented approach is to combine both data and function into a
single unit and these units are called objects.
The term objects means a combination of data and program that represent some real word entity.
For example: consider an example named Amit; Amit is 25 years old and his salary is 2500. The
Amit may be represented in a computer program as an object. The data part of the object would be
(name: Amit, age: 25, salary: 2500)
The program part of the object may be a collection of programs (retrieve of data, change age,
change of salary). In general, even any user-defined type-such as a n employee may be used. In
the Amit object the name, age, and salary are called attributes of the object.
Display
CLASS:
A group of objects that share common properties for data part and some program
part are collectively called as class.
In C ++ a class is a new data type that contains member variables and member
functions that operate on the variables.
Examples :
#include <iostream>
using namespace std;
class Car {
public:
// class data
string brand, model;
int mileage = 0;
int main() {
return 0;
}
DATA ABSTRACTION :
class Car {
private:
// class data
int speed;
// class function
void show_car_status() {
// code
}
};
DATA ENCAPSALATION :
The wrapping up of data and function into a single unit (called class) is
known as encapsulation. The data is not accessible to the outside world and
only those functions which are wrapped in the class can access it. These functions
provide the interface between the objects data and the program.
class Car {
public:
// class data
string brand;
string model;
int mileage = 0;
// class function
void show_data() {
// code
}
};
INHERITENCE :
Inheritance is the process by which objects of one class acquire the properties
of another class. In the concept of inheritance provides the idea of reusablity.
This mean that we can add additional features to an existing class with out
modifying it. This is possible by desining a new class will have the combined
features of both the classes.
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
string brand;
void show_brand() {
cout << "Brand: " << brand << endl;
}
};
// derived class
class Car : public Vehicle {
public:
string model;
void show_model() {
cout << "Model: " << model << endl;
}
};
int main() {
POLYMORPHISIM:
Polymorphism means the ability to take more than one form. An operation may
exhibit different instance. The behaviour depends upon the type of data used in the
operation.
A language feature that allows a function or operator to be given more than one
definition. The types of the arguments with which the function or operator is
called determines which definition will be used.
Overloading may be operator overloading or function overloading.
It is able to express the operation of addition by a single operater say ‘+’. When
this is possible you use the expression x + y to denote the sum of x and y, for many
different types of x and y; integers , float and complex no. You can even define the
+ operation for two strings to mean the concatenation of the strings.
#include <iostream>
using namespace std;
// base class
class Shape {
public:
// derived class
class Square : public Shape {
public:
return 0;
}
DYNAMIC BINDING :
Binding refers to the linking of a procedure call to the code to the executed
in response to the call. Dynamic binding means the code associated with a given
procedure call is not known until the time of the call at run-time. It is associated
with a polymorphic reference depends upon the dynamic type of that reference.
Virtual functions
A virtual function is a member function declared in a base class and re-declared in
a derived class (overridden). You can execute the virtual function of the derived
class when you refer to its object using a pointer or reference to the base class. The
concept of dynamic binding is implemented with the help of virtual functions.
class DB {
public:
// function that call print
MESSAGE PASSING :
#include <iostream>
#include <string>
#include <queue>
using namespace std;
class Message {
public:
string content;
int priority;
class MessageQueue {
private:
queue<Message> messages;
public:
void push(Message message) {
messages.push(message);
}
Message pop() {
Message message = messages.front();
messages.pop();
return message;
}
bool isEmpty() {
return messages.empty();
}
class MessageHandler {
private:
MessageQueue queue;
public:
void handleMessage() {
while (!queue.isEmpty()) {
Message message = queue.pop();
cout << "Received message: " << message.content << endl;
}
}
int main() {
MessageHandler handler;
handler.handleMessage();
return 0;
}
LECTURE- 4
BENEFITS OF OOP:
Oop offers several benefits to both the program designer and the user. Object-
oriented contributes to the solution of many problems associated with the
development and quality of software products. The principal advantages are :
APPLICATION OF OOP:
The most popular application of oops up to now, has been in the area of user
interface design such as windows. There are hundreds of windowing systems
developed using oop techniques.
Real business systems are often much more complex and contain many more
objects with complicated attributes and methods. Oop is useful in this type of
applications because it can simplify a complex problem. The promising areas for
application of oop includes.