0% found this document useful (0 votes)
5 views15 pages

01 Object Oriented Concepts

Uploaded by

nanigir651
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

01 Object Oriented Concepts

Uploaded by

nanigir651
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SHRI SHANKARACHARYA TECHNICAL CAMPUS

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

Machine level Language :


Machine code or machine language is a set of instructions executed directly by a computer's central
processing unit (CPU). Each instruction performs a very specific task, such as a load, a jump, or an
ALU operation on a unit of data in a CPU register or memory. Every program directly executed by a
CPU is made up of a series of such instructions.

Assembly level Language :


An assembly language (or assembler language) is a low-level programming language for a computer,
or other programmable device, in which there is a very strong (generally one-to-one) correspondence
between the language and the architecture's machine code instructions. Assembly language is
converted into executable machine code by a utility program referred to as an assembler; the
conversion process is referred to as assembly, or assembling the code.

High level Language :


High-level language is any programming language that enables development of a program in much
simpler programming context and is generally independent of the computer's hardware architecture.
High-level language has a higher level of abstraction from the computer, and focuses more on the
programming logic rather than the underlying hardware components such as memory addressing and
register utilization.

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.

The high-level programming languages are broadly categorized in to two categories:

(i) Procedure oriented programming(POP) language.


(ii) Object oriented programming(OOP) language.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


1
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

Procedure Oriented Programming Language

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

Function-1 Function-2 Function-3

The disadvantage of the procedure oriented programming languages is:


1. Global data access
2. It does not model real word problem very well 3. No
data hiding

Global data Global data

Function-1 Function-2 Function-3

Local data Local data Local data

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


2
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

Characteristics of procedure-oriented programming:

1. Emphasis is on doing things(algorithm)


2. Large programs are divided into smaller programs known as functions. 3.
Most of the functions share global data
4. Data move openly around the system from function to function 5.
Function transforms data from one form to another.
6. Employs top-down approach in program design

Procedural Programming vs Object-Oriented Programming


Below are some of the differences between procedural and object-oriented programming:
Procedural Oriented Programming Object-Oriented Programming

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.

Object-oriented programming follows a bottom-up


Procedural programming follows a top-down approach. approach.

Object-oriented programming has access specifiers


There is no access specifier in procedural programming. like private, public, protected, etc.

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.

Overloading is possible in object-oriented


In procedural programming, overloading is not possible.
programming.

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.

In procedural programming, the function is more In object-oriented programming, data is more


important than the data. important than function.

Object-oriented programming is based on the real


Procedural programming is based on the unreal world. world.

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.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


3
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS
Procedural Oriented Programming Object-Oriented Programming

Code reusability present in object-oriented


Code reusability absent in procedural programming, programming.

Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


4
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS
LECTURE-2

Object Oriented Programing

“Object oriented programming as an approach that provides a way of modularizing programs


by creating partitioned memory area for both data and functions that can be used as
templates for creating copies of such modules on demand”.

Object A Object B

Data Data

Communication
Functions Functions

Object C

Functions

Data

Features of the Object Oriented programming

1. Emphasis is on doing rather than procedure.


2. programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data structure.
5. Data is hidden and can’t be accessed by external functions.
6. Objects may communicate with each other through functions. 7. New data
and functions can be easily added.
8. Follows bottom-up approach in program design.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


5
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

LECTURE-3

BASIC CONCEPTS OF OBJECTS ORIENTED PROGRAMMING

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.

Object: Student STUDENT

DATA Name Total


Date-of-birth Marks

FUNCTIONS Total Average Average


Display

Display

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


6
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

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;

// class function to drive the car


void drive(int distance) {
mileage += distance;
}

// class function to print variables


void show_data() {
cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
cout << "Distance driven: " << mileage << " miles" << endl;
}
};

int main() {

// create an object of Car class


Car my_car;

// initialize variables of my_car


my_car.brand = "Honda";
my_car.model = "Accord";
my_car.drive(50);

// display object variables


my_car.show_data();

return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


7
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

DATA ABSTRACTION :

Abstraction refers to the act of representing essential features without including


the background details or explanations. Classes use the concept of abstraction and
are defined as size, width cost, and functions to operate on the attributes.

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
}
};

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


8
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

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() {

// create an object of Car class


Car my_car;

// initialize variables of my_car


my_car.brand = "Honda";
my_car.model = "Accord";

// display variables of my_car


my_car.show_brand();
my_car.show_model();
return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


9
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

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:

// function of base class


void shape_name() {
cout << "Shape" << endl;
}
};

// derived class
class Square : public Shape {
public:

// overriding function of derived class


void shape_name() {
cout << "Square" << endl;
}
};
int main() {
// create class objects
Shape shape;
Square square;

// call function from Shape class


shape.shape_name();

// call function from Square class


square.shape_name();

return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


10
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

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.

Static Binding Vs Dynamic Binding


Static Binding Dynamic Binding

It takes place at compile time


It takes place at runtime so it is
which is referred to as early
referred to as late binding
binding

Execution of static binding is Execution of dynamic binding is


faster than dynamic binding slower than static binding
because of all the information because the function call is not
needed to call a function. resolved until runtime.

It takes place using normal


function calls, operator It takes place using virtual
overloading, and function functions
overloading.

Real objects never use static Real objects use dynamic


binding binding

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.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


11
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

// C++ Program to Demonstrate the Concept of Dynamic binding


// with the help of virtual function
#include <iostream>
using namespace std;

class DB {
public:
// function that call print

void call_Function() { print(); }


// the display function
virtual void print()
{
cout << "Printing the Base class Content" << endl;
}
};

// DB2 inherited publicly


class DB2 : public DB {
public:
void print() // DB2's display
{
cout << "Printing the Derived class Content"
<< endl;
}
};
int main()
{
DB dynamicbinding; // Creating DB's object
dynamicbinding.call_Function(); // Calling call_Function
DB2 dynamicbinding 2; // calling DB2
dynamicbinding2.call_Function();
return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


12
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

MESSAGE PASSING :

An object oriented program consists of a set of objects that communicate with


each other.

A message for an object is a request for execution of a procedure and therefore


will invoke a function (procedure) in the receiving object that generates the
desired result. Message passing involves specifying the name of the object,
the name of the function (message) and information to be sent.

Employee . Salary (name)

Object Information Message

#include <iostream>
#include <string>
#include <queue>
using namespace std;
class Message {
public:
string content;
int priority;

Message(string content, int priority) {


this->content = content;
this->priority = 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();
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


13
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS
};

class MessageHandler {
private:
MessageQueue queue;

public:
void handleMessage() {
while (!queue.isEmpty()) {
Message message = queue.pop();
cout << "Received message: " << message.content << endl;
}
}

void sendMessage(string content, int priority) {


Message message(content, priority);
queue.push(message);
}
};

int main() {
MessageHandler handler;

handler.sendMessage("Hello world", 1);


handler.sendMessage("How are you?", 2);
handler.sendMessage("Goodbye", 3);

handler.handleMessage();

return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


14
SOMESH KUMAR DEWANGAN
SHRI SHANKARACHARYA TECHNICAL CAMPUS

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 :

1. Through inheritance we can eliminate redundant code and extend the


use of existing classes.
2. We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the code from
scratch. This leads to saving of development time and higher productivity.
3. This principle of data hiding helps the programmer to build secure programs
that can’t be invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out
any interference. 5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large systems.
7. Message passing techniques for communication between objects makes
the interface description with external systems much simpler.
8. Software complexity can be easily managed.

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.

1. Real – Time systems.


2. Simulation and Modeling
3. Object oriented databases.
4. Hypertext,hypermedia and expertext.
5. Al and expert systems.
6. Neural networks and parallel programming.
7. Decision support and office automation systems.
8. CIM / CAM / CAD system.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


15
SOMESH KUMAR DEWANGAN

You might also like