OOP CS321 IIITPune Unit 01
OOP CS321 IIITPune Unit 01
Dr. M A Rahman
PhD IIT Patna.
Programming Paradigm
UNIT 1: INTRODUCTION (06 Hours)
Recommended Books:
▪ E. Balaguruswami,” Object Oriented Programming in C++”, TMH.
▪ Robert Lafore, “Object Oriented Programming in C++”, Pearson.
▪ M.T. Somashekare, D.S. Guru, “Object-Oriented Programming with C++”, PHI.
▪ Herbert Shildt, “The Complete Reference C++”, Tata McGraw Hill publication.
Programming Paradigm
❑What is Programming Paradigm:
❑Each paradigm consists of certain structures, features, and opinions about how
common programming problems should be tackled.
Programming Paradigm
1, 0
Machine Language
Assembly Language
Procedure Oriented
Object Oriented
❑Certain paradigms are better suited for certain types of problems, so it makes
sense to use different paradigms for different kinds of projects.
Popular Programming Paradigm
❑ Structured Programming
❑ Procedural Programming
❑ ….
❑ ….
Procedural Programming
❑ Procedural Programming can be defined as a programming model which is
derived from structured programming, which is based upon the concept of calling
procedures.
❑ During a program’s execution, any given procedure (or function) might be called
at any point, including by other procedures or itself.
Procedure Programming
Example of Procedure Programming
Language:
• FORTRAN
• C
• BASIC
• Pascal
❑ An object has an identity, state, and behavior. Each object contains data
(Attributes) and code (Method) to manipulate the data.
Object Oriented Programming (OOP)
❑Data Abstraction
❑ Data abstraction refers to providing only essential information about the data
to the outside world, hiding the background details or implementation.
❑ Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of the car or applying brakes
will stop the car, but he does not know about how on pressing the accelerator
the speed is increasing, he does not know about the inner mechanism of the
car or the implementation of the accelerator, brakes, etc. in the car. This is
what abstraction is.
Object Oriented Programming (OOP)
❑Data Abstraction
Object Oriented Programming (OOP)
❑ Demonstration of the difference between public and private access specifiers in
C++.
Object Oriented Programming (OOP)
❑Data Abstraction
Object Oriented Programming (OOP)
❑ Encapsulation:
❑ Encapsulation is defined as the wrapping up of data under a single unit.
❑ It is the mechanism that binds together code (Methods) and the data
(Attributes) it manipulates.
❑ In Encapsulation, the variables or data or Attributes of
a class are hidden from any other class and can be
accessed only through any member function of their
class in which they are declared.
❑ student_01.Name
❑ As in encapsulation, the data in a class is hidden from
other classes, so it is also known as data-hiding.
Object Oriented Programming (OOP)
❑ setter and getter methods to assign and access private data:
#include <iostream> • The salary attribute is private, which have restricted access.
using namespace std; • The public setSalary( ) method takes a parameter (s) and assigns it to the salary
class Employee { attribute (salary = s).
private: • The public getSalary( ) method returns the value of the private salary attribute.
// Private attribute • Inside main( ), we create an object of the Employee class.
int salary; • Now we can use the setSalary( ) method to set the value of the private attribute to
public: 50000.
// Setter • Then we call the getSalary( ) method on the object to return the value.
void setSalary(int s) {
salary = s; int main() {
} Employee myObj;
// Getter myObj.setSalary(50000);
int getSalary() { cout << myObj.getSalary();
return salary; return 0;
} }
};
Object Oriented Programming (OOP)
❑ Inheritance:
❑ The capability of a class (Child class) to derive properties and characteristics
from another class (Parent class) is called Inheritance.
❑ When we write a class (say Child class using Parent class), we inherit
properties from Parent classes.
❑ So when we create a class, we do not need to
write all the properties and functions again
and again, as these can be inherited from
another class that possesses it.
❑ Inheritance allows the user to reuse the code
whenever possible and reduce its redundancy.
Object Oriented Programming (OOP)
❑ Inheritance: Mother/Parent Class
Observatory
• Attributes
• ______
• Methods
• ______
Weather Air_quality
• Attributes_01 • Attributes_02
• _____ • ______
• Methods_01 • Methods_02
• _____ • ______
Mother/Parent Class
x = Student("Mike", "Olsen", 2019)
x.printname()
>> Mike Olsen
Child Class
x.welcome()
>> Welcome Mike Olsen to the class of 2019
Object Oriented Programming (OOP)
❑ Polymorphism:
❑ Means having Many Forms.
❑ Same Name But Different Behavior in Different Scenario/ Conditions
Object Oriented Programming (OOP)
❑ Polymorphism:
❑ Compile Time Polymorphism or Static Polymorphism:
❑ Multiple methods with same name but with different arguments are defined in
same class.
❑ Methods are differentiated by their Arguments.
❑ It occurs during compile time.
Function Overloading
Object Oriented Programming (OOP)
❑ C++ program to demonstrate Compile-time Polymorphism [function overloading]:
class Display_numbers { ❑ Function being called depends on the
public: parameters passed
// Function with 1 int parameter
void func(int x) int main()
{ {
cout << "value of x is " << x << endl; Display_numbers obj1;
} // func() is called with int value
obj1.func(7);
// Function with same name but 1 double parameter // func() is called with double value
void func(double x) obj1.func(9.132);
{ // func() is called with 2 int values
cout << "value of x is " << x << endl; obj1.func(85, 64);
} return 0;
}
// Function with same name and 2 int parameters
void func(int x, int y) Output:
{
cout << "value of x and y is " << x << ", " << y value of x is 7
<< endl; value of x is 9.132
} value of x and y is 85, 64
};
Object Oriented Programming (OOP)
❑C++ program to demonstrate Compile-Time Polymorphism [Operator Overloading]:
#include <iostream>
using namespace std;
Method
Car
Car.drive() Mother Class .drive
❑ cin
❑ It is pronounced "see-in".
❑ Used for input, and uses the extraction operator (>>)