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

OOP CS321 IIITPune Unit 01

The document provides an overview of Object Oriented Programming (OOP) principles and paradigms, detailing the differences between procedural and object-oriented programming. It covers key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and access specifiers, along with examples in C++ and Python. Recommended books for further reading on OOP in C++ are also listed.
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 views43 pages

OOP CS321 IIITPune Unit 01

The document provides an overview of Object Oriented Programming (OOP) principles and paradigms, detailing the differences between procedural and object-oriented programming. It covers key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and access specifiers, along with examples in C++ and Python. Recommended books for further reading on OOP in C++ are also listed.
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/ 43

Indian Institute of Information Technology Pune

Object Oriented Programming


CS-321

Dr. M A Rahman
PhD IIT Patna.
Programming Paradigm
UNIT 1: INTRODUCTION (06 Hours)

Introduction: Object oriented programming, Introduction, Application,


characteristics, difference between object oriented and procedure programming,
Comparison of C and C++, Cout, Cin, Data Type, identifiers.

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:

❑Programming paradigms are different ways or styles in which a given


program or programming language can be organized.

❑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

Fig.: Layers of Computer Software


Programming Paradigm
❑Why are there many different programming paradigms:
❑This question is similar to, why are there many programming languages.
❑Certain programming Language are better suited for certain types of
problems.
• Web development: JavaScript
• Artificial Intelligence, Machine Learning and Deep Learning: Python
• Operating System, Embedded Systems, Hardware Drivers: C/C++
• Mobile Application Development: Java,
• Statistical Computation and Analysis: R

❑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

❑ Object Oriented Programming (OOP)

❑ ….

❑ ….
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.

❑ Procedures, also known as routines, subroutines or functions, simply consist of a


series of computational steps to be carried out.

❑ 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

Typical Structure of a Procedure Programming


Procedure Programming
Example of a Function:

Calling the function to check if the given


Number is Even or Odd.

>> evenOdd(7) will give output➔ odd


>> evenOdd(4) will give output➔ even
Typical Structure of a Procedure Programming
Object Oriented Programming (OOP)
❑ OOP can be defined as a programming model which
is based upon the concept of Class and Objects. CLASS: Student

❑ Objects contain data in the form of Attributes and Attributes:


Name: _ _ _
code in the form of Methods. Date_of_Birth: _ _ _
Marks: _ _ _
❑ In object-oriented programming, computer programs . . . . . .: _ _ _ _

are designed using the concept of objects that interact


Methods:
with the real world. Total_Students:
Average_Marks:
❑ Objects are instances of Classes. Print_Marksheet:
Print_Student_ID:
...............:
Object Oriented Programming (OOP)
Examples of Object Oriented Programing Language:

❑ Java, C++, C#, Python,

❑ PHP, JavaScript, Ruby, Perl,

❑ Objective-C, Dart, Swift, Scala.


Object Oriented Programming (OOP)
Example of a Class “Student” defined in Python:
Creating different Objects of class Student
Object Oriented Programming (OOP)
Defining a Class in C++:
Object Oriented Programming (OOP)
❑ Example of a Class “Student” defined in C++:

#include <iostream> ❑ Creating an Object “student_01” from


using namespace std;
Class “Student”
class Student { // Name of Class
public: // Access Specifier int main() {
int rollno; // Attributes Student student_01 = {1, "Harry", 91.5};
char name[50]; student_01.display();
float marks; return 0;
}
void display() { // Method Defined Output:
cout<<"Roll Number: "<< rollno <<endl;
cout<<"Name: "<< name <<endl; Roll Number: 1
cout<<"Marks: "<< marks <<endl; Name: Harry
} Marks: 91.5
};
Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
In C++, there are three access specifiers:

❑public - members are accessible from outside the class


❑private - members cannot be accessed (or viewed) from outside the class
❑protected - members cannot be accessed from outside the class, however, they
can be accessed in inherited classes.
Object Oriented Programming (OOP)
❑ Important Concepts of OOP:
❑ Class
❑ Objects
❑ Data Abstraction
❑ Encapsulation
❑ Inheritance
❑ Polymorphism
❑ Dynamic Binding
❑ Message Passing
Object Oriented Programming (OOP)
❑ Class: CLASS: Student
❑ A class is like a blueprint for an object.
Attributes:
❑ Class is a user-defined data type. Roll_No: _ _ _
❑ It consists of data members called Attributes and Name: _ _ _
member functions called Methods, which can be Date_of_Birth: _ _ _
Marks: _ _ _
accessed and used by creating an instance of that ..... :____
class.
❑ It represents the set of properties or methods that Methods:
are common to all objects of one type. Print_Marksheet:
Print_Student_ID:
...............:
Object Oriented Programming (OOP)
❑ Object:

❑ An Object is an instance of a Class.

❑ Creating an Object in In Python


• student_01 = Student(“0124”, “Rahul”, “M”)

❑ It is a basic unit of Object-Oriented Programming and represents the real-life


entities.

❑ 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
• _____ • ______

Child Class_01 Child Class_02


Object Oriented Programming (OOP)
❑ Inheritance: // Mother class
class Vehicle { ❑ Creating Object & Calling Method:
public:
string brand = "Ford"; int main() {
Mother Class Car myCar;
void honk() { myCar.honk();
cout << "Tuut, tuut! \n" ; cout << myCar.brand + “, " + myCar.model;
} return 0;
}; }

// Child class ❑Output:


class Car: public Vehicle {
Tuut, tuut!
Child Class public:
Ford, EchoSports
string model = “EchoSports";
};
Object Oriented Programming (OOP)
❑ Inheritance:

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;

class Complex { // Driver code


private:
int real, imag; int main()
public: {
Complex(int r = 0, int i = 0) Complex c1(10, 5), c2(2, 4);
{
real = r; // An example call to "operator+"
imag = i; Complex c3 = c1 + c2;
} c3.print();
// This is automatically called when '+' is used with between }
two Complex objects
Complex operator+(Complex const & obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
Object Oriented Programming (OOP)
❑ Polymorphism:
❑ Dynamic Polymorphism / Run Time Polymorphism:
❑ If a Method signature is present both in sub-class and super-class.
❑ Sub-class method is performed.
❑ It Occurs during Run Time, i.e. when the program is being executed.
❑ Here mother class (car) and child class (sports car) both having .drive method.

Method
Car
Car.drive() Mother Class .drive

Sports_Car.drive() Child Class Sports_Car .drive


Object Oriented Programming (OOP)
❑ C++ program to demonstrate Run-time Polymorphism or Dynamic Polymorphism:
// Base class
class Animal { ❑ Creating Object & Calling Method:
public:
void animalSound() { int main() {
cout << "The animal makes a sound \n"; Animal myAnimal;
} Cat myCat;
}; Dog myDog;
// Derived class-1 myAnimal.animalSound();
class Cat : public Animal {
public:
myCat.animalSound();
void animalSound() { myDog.animalSound();
cout << "The Cat says: Meow Meow\n"; return 0;
} }
};
// Derived class-2 ❑ Output:
class Dog : public Animal {
public: The animal makes a sound
void animalSound() { The Cat says: Meow Meow
cout << "The Dog says: bow wow \n"; The Dog says: bow wow
}
};
Object Oriented Programming (OOP)
❑ Polymorphism:
❑ Dynamic Polymorphism:
Object Oriented Programming (OOP)
❑ Public and Private Access Specifiers:
Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
❑ cin and cout:
❑ cout
❑ It is pronounced "see-out".
❑ Used for output, and uses the insertion operator (<<)

❑ cin
❑ It is pronounced "see-in".
❑ Used for input, and uses the extraction operator (>>)

You might also like