SlideShare a Scribd company logo
Introduction to Object Oriented
Programming (OOP)
Prepared By
Anita Vikram Shinde
Procedural Oriented Language
• Conventional programming, using high-level language such as COBOL,
FORTRAN and C are commonly known as Procedure oriented language
(POP).
• In POP numbers of functions are written to accomplish the tasks
2
Procedural Oriented Language
- Structure -
Function-1 Function-2 Function-3
Function-4 Function-5
Function-6 Function-7 Function-8
Main
program
3
Procedure Oriented Language
- Characteristics -
• Emphasis is on doing things (algorithms).
• Larger programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Employs top-down approach in program design.
4
Procedural Oriented Language
- Limitations -
• Data move freely around the program and are therefore vulnerable to
changes caused by any function in the program.
• It does not model very well the real world problems.
5
70 + OOP Based Programming Languages
Object Oriented Programming
• OOP treats data as critical element
• Ties data more closely to the functions that operate on it & allows
decomposition of problem into objects.
OBJECT
Operations
Data
OBJECT
Operations
Data
OBJECT
Operations
Data
Communication
7
Procedure Oriented Programming Object Oriented Programming
Divided Into In POP, program is divided into small parts
called functions
In OOP, program is divided into parts called objects
Importance In POP, Importance is not given to data but to
functions as well as sequence of actions to be
done
In OOP, Importance is given to the data rather than
procedures or functions because it works as a real
world
Approach POP follows Top Down approach OOP follows Bottom Up approach
Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private,
Protected, etc.
Data Moving In POP, Data can move freely from function to
function in the system
In OOP, objects can move and communicate with
each other through member functions
Expansion To add new data and function in POP is not so easy OOP provides an easy way to add new data and
function
Data Access In POP, Most function uses Global data for sharing
that can be accessed freely from function to
function in the system
In OOP, data can not move easily from function to
function, it can be kept public or private so we can
control the access of data
Data Hiding POP does not have any proper way for hiding data
so it is less secure
OOP provides Data Hiding so provides more
security
Examples Example of POP are : C, VB, FORTRAN, Pascal Example of OOP are : C++, JAVA, VB.NET, C#.NET
8
Fundamentals of OOP
• Objects
• Classes
• Encapsulation
• Data Abstraction
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
9
Four Pillars of OOPs
Objects
• OOP uses objects as its fundamental building blocks.
• Objects are the basic run-time entities in an object-oriented system.
• Every object is associated with data and functions which define meaningful
operations on that object.
• Object is a real world existing entity.
• Object is an Instance of a particular class.
11
Object
Attributes
Operation
Operation
Operation
Operation
12
Example: StudentObject
st_name
st_id
branch
semester
Enroll()
Displayinfo()
Performance()
Result()
13
Object
Class
• Class is a collection of similar objects.
Class
15
Class
• Define a attribute and behaviours of an object
Object Oriented Programming  Constructors &  Destructors
Object Oriented Programming  Constructors &  Destructors
Class Specification
• Syntax:
class class_name
{
};
Data members
Members functions
19
A Class is a way to bind the data and its associated functions together
General form of class declaration is:
class Class-name
{
private :
Variables declarations;
functions declarations;
public:
Variables declarations;
functions declarations;
};
Specifying Class
Class Specification
• class Student
{
int st_id;
char st_name[];
void read_data();
void print_data();
};
Data Members or Properties of
Student Class
Members Functions or
Behaviours of Student Class
21
Class Specification
• Visibility of Data members & Member functions
public –
Accessed by member functions and all other
non-member functions in the program.
private –
Accessed by only member functions of the class.
protected –
Similar to private, but accessed by all the member
functions of immediate derived class
Default –
All items defined in the class are private.
22
■ The class members that have been declared as private can be
accessed only from within the class.
■ The public members can be accessed from outside the class also.
■ The use of the keyword private is optional. By default , the members
of class are private
■ Data members & Member functions.
■ Only member functions can have access to the private data members
and private functions
Example of class
class student
{
int roll_no;
float marks;
public:
void input(int a, float b);
void display(void);
};
Creating Objects of class
■ student x,y,z;
OR
■ class student
{
……..
}x, y, z;
Accessing class data
main() can not contain statement that access roll_no and marks.
Only member functions can access private data .
Member functions can be called only by object using dot(.) operator.
Format for Calling member function :
object-name . Function-name (actual- arg)
ex. x.input(25, 32.5);
Defining member function
Member functions can be defined in two
places:
1. Outside the class definition
2. Inside the class definition
Outside the class definition
The imp difference between member function and
normal function is that member function
incorporates a membership ‘identity label’ in
the header.
This label tells compiler which class the function
belongs to.
■ The general form of member function definition outside class
is:
Return-type class-name :: fun–name(arg-list)
{
function body;
}
Ex. void student :: input(int a, float b)
{
rollno= a;
marks = b;
}
Member Function definition inside the class
class student
{
int rollno;
float marks;
public:
void input( int a, float b);
void display(void)
{
cout<<rollno<< marks;
}
};
Nesting of Member Functions
A member function can be called by using its name
inside another member function of the same class.
This is known as nesting of member functions.
class student
{ int roll_no;
float marks;
public:
void input(int a, float b);
void display(void);
};
void student ::input(int a, float b)
{
rollno= a;
marks = b;
display();
}
void student ::display()
{
cout<<rollno;
cout<<marks;
}
int main()
{
student s1;
s1.input(10, 75.55);
return 0;
}
Memory allocation for Objects
■ The memory place for objects is allocated when they are declared and
not when the class is specified. This is partly true.
■ The member functions are created and placed in memory space only
once when they are defined as part of class specification.
■ All objects belonging to that class use same member functions so no
separate space is allocated for member functions when objects are
created.
■ Only space for member variables is allocated separately for each object.
Arrays of Objects
• Several objects of the same class can be declared as an array and
used just like an array of any other data type.
• The syntax for declaring and using an object array is exactly the
same as it is for any other type of array.
34
Array of objects
Class employee
{
char name[10];
int emp_code ;
public:
void input(void);
void display(void);
};
employee manager[10];
employee worker[10];
manager[i].display();
Constructors
• A constructor function is a special member function that is a member of a class
and has the same name as that class, used to create, and initialize objects of
the class.
• Constructor function do not have return type.
• Should be declared in public section.
• Invoked automatically when objects are created
Constructors
Syntax:
class class_name
{
public:
class_name();
};
Example:
class student
{ int st_id;
public:
student()
{
st_id=0;
}
Constructors
• How to call this special function…?
int main()
{
student st;
…………
…………
};
class student
{
int st_id;
public:
student()
{
st_id=0;
}
};
38
Types of Constructors
• Parameterized constructors
• Constructors with default argument
• Copy constructors
• Dynamic constructors
39
Example of constructor:
A constructor is declared and defined as follows:
class integer
{
int m,n;
public:
integer (void);
…
};
integer :: integer (void)
{
m=0;
n=0;
}
Example of constructor:
integer int1;
A constructor that accepts no parameters is called default constructor.
Special characteristics of constructor:
They should be declared in public section.
They are invoked automatically when the objects are created
They don’t have any return type , Not even void. So they can not return
values.
They can have default arguments.
We can not refer to their addresses
They can not be inherited.
Parameterized Constructors
class Addition
{
int num1;
int num2;
int res;
public:
Addition(int a, int b); // constructor
void add( );
void print();
};
Constructor with parameters
B’Coz it’s also a function!
Constructor that can take arguments
is called parameterized constructor.
42
Parameterized Constructors:
• The constructor that can take arguments are called
parameterized constructor.
class integer
{
int m,n;
public:
integer (int a, int b);
}
integer :: integer (int a, int b)
{
m=a;
n=b;
}
• By calling constructor explicitly
integer int1= integer(10,20);
• By calling constructor implicitly
integer int1(10,20);
Constructor with default arguments:
Ex. Complex(float real, float imag=10);
Complex c1(2.0);
Complex c2(3.0, 5.0);
Constructors with Default Argument
class Addition
{
int num1;
int num2;
int res;
public:
Addition(int a, int b=0); // constructor
void add( );
void print();
};
Constructor with default
parameter.
45
Copy constructor
Copy Constructor is used to declare and initialize object
from another object.
integer (integer &I2); //Function prototyping
ex. integer I2(I1);
OR
integer I2=I1;
But I2=I1; Won’t invoke copy constructor.
Copy Constructor
class code
{
int id;
public:
code() //default constructor
{ id=0;}
code(int a){id=a;} // parameterized constructor
code(code &obj) // copy constructor
{
id=obj.id;
}
void display()
{
cout<<id;
}
};
int main()
{ code A(100);
code B(A);
code C=A;
code D;
D=A; // wrong syntax for copy construcor
cout<<“ id of A:”; A.display();
cout<<“ id of B:”; B.display();
cout<<“ id of C:”; C.display();
cout<<“ id of D:”; D.display();
}
47
Multiple constructors in a class/ Can we overload constructors
class integer
{
int m,n;
public:
integer( ){m=0;n=0;} //Default constructor
integer(int a , int b) //Parameterized constructor
{m=a;n=b;}
integer(integer &j) //Copy constructor
{m=j.m; n=j.n;}
};
• integer I1; invoke Default constructor
• integer I2(5,10); invoke Parameterized constructor
• integer I3(I2); invoke Copy constructor
• This is known as overloading of constructor
Dynamic constructor:
• The constructor can be used to allocate memory while creating
objects.
• Allocation of memory to objects at the time of their construction is
known as dynamic construction of object.
• The memory is allocated with the help of new operator.
Destructors
• A destructor is used to destroy the objects that have been created by constructor.
• The destructor is a member function whose name is same as the class name but preceded by
tilde(~).
Ex. ~integer ( );
• A destructor never takes any arguments nor does it return any value
• It will be invoked implicitly by the compiler upon exit from the program (or block or function) to
clean up storage that is no longer accessible.
• It's good practice to use destructor to release memory for future use.
• When new is used in constructor then use delete in destructor.
• The objects are destroyed in the reverse order of creation.
#include<iostream.h>
int count=0;
class alpha
{
public:
alpha( )
{
count++;
cout<<“No of object created<< count;
}
~alpha( )
{
cout<<“No of object destroyed<< count;
count--;
}
int main( )
{
cout<< “Enter main”;
alpha A1, A2, A3, A4;
{
cout<<“Enter Block1”;
alpha A5;
}
{
cout<<“Enter Block2”;
alpha A6;
}
cout<<“ Re-enter Main”;
return 0; }
• OUTPUT
Enter Main
No of object created 1
No of object created 2
No of object created 3
No of object created 4
Enter Block1
No of object created 5
No of object destroyed 5
Enter Block2
No of object created 5
No of object destroyed 5
Re-Enter Main
No of object destroyed 4
No of object destroyed 3
No of object destroyed 2
No of object destroyed 1
Ad

More Related Content

What's hot (20)

Chapter 9 morphological image processing
Chapter 9   morphological image processingChapter 9   morphological image processing
Chapter 9 morphological image processing
Ahmed Daoud
 
MULTIMEDIA COMMUNICATION & NETWORKS
MULTIMEDIA COMMUNICATION & NETWORKSMULTIMEDIA COMMUNICATION & NETWORKS
MULTIMEDIA COMMUNICATION & NETWORKS
Kathirvel Ayyaswamy
 
unit testing and debugging
unit testing and debuggingunit testing and debugging
unit testing and debugging
KarthigaGunasekaran1
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming concepts
salmankhan570
 
Lecture 13 (Usage of Fourier transform in image processing)
Lecture 13 (Usage of Fourier transform in image processing)Lecture 13 (Usage of Fourier transform in image processing)
Lecture 13 (Usage of Fourier transform in image processing)
VARUN KUMAR
 
Implicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handlingImplicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handling
VIKASH MAINANWAL
 
Critical System Validation in Software Engineering SE21
Critical System Validation in Software Engineering SE21Critical System Validation in Software Engineering SE21
Critical System Validation in Software Engineering SE21
koolkampus
 
color detection using open cv
color detection using open cvcolor detection using open cv
color detection using open cv
Anil Pokhrel
 
Multimedia
MultimediaMultimedia
Multimedia
Archana P Babu
 
Feature Driven Development
Feature Driven DevelopmentFeature Driven Development
Feature Driven Development
dcsunu
 
Image restoration and reconstruction
Image restoration and reconstructionImage restoration and reconstruction
Image restoration and reconstruction
Visvesvaraya National Institute of Technology, Nagpur, Maharashtra, India
 
Programming
ProgrammingProgramming
Programming
Leo Simon Anfone
 
Evolving role of Software,Legacy software,CASE tools,Process Models,CMMI
Evolving role of Software,Legacy software,CASE tools,Process Models,CMMIEvolving role of Software,Legacy software,CASE tools,Process Models,CMMI
Evolving role of Software,Legacy software,CASE tools,Process Models,CMMI
nimmik4u
 
Lecture 3 basic syntax and semantics
Lecture 3  basic syntax and semanticsLecture 3  basic syntax and semantics
Lecture 3 basic syntax and semantics
alvin567
 
Defining the Problem - Goals and requirements
Defining the Problem - Goals and requirementsDefining the Problem - Goals and requirements
Defining the Problem - Goals and requirements
Stephennancy
 
CNMES 2017 Software Cost Estimating with COSMIC - Critical knowledge for toda...
CNMES 2017 Software Cost Estimating with COSMIC - Critical knowledge for toda...CNMES 2017 Software Cost Estimating with COSMIC - Critical knowledge for toda...
CNMES 2017 Software Cost Estimating with COSMIC - Critical knowledge for toda...
COSMIC - Common Software Measurement International Consortium
 
Dip unit-i-ppt academic year(2016-17)
Dip unit-i-ppt academic year(2016-17)Dip unit-i-ppt academic year(2016-17)
Dip unit-i-ppt academic year(2016-17)
RagavanK6
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithm
Mani Kanth
 
Chapter 1 2 - some size factors
Chapter 1   2 - some size factorsChapter 1   2 - some size factors
Chapter 1 2 - some size factors
NancyBeaulah_R
 
Sound
SoundSound
Sound
Fawad Saleem
 
Chapter 9 morphological image processing
Chapter 9   morphological image processingChapter 9   morphological image processing
Chapter 9 morphological image processing
Ahmed Daoud
 
MULTIMEDIA COMMUNICATION & NETWORKS
MULTIMEDIA COMMUNICATION & NETWORKSMULTIMEDIA COMMUNICATION & NETWORKS
MULTIMEDIA COMMUNICATION & NETWORKS
Kathirvel Ayyaswamy
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming concepts
salmankhan570
 
Lecture 13 (Usage of Fourier transform in image processing)
Lecture 13 (Usage of Fourier transform in image processing)Lecture 13 (Usage of Fourier transform in image processing)
Lecture 13 (Usage of Fourier transform in image processing)
VARUN KUMAR
 
Implicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handlingImplicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handling
VIKASH MAINANWAL
 
Critical System Validation in Software Engineering SE21
Critical System Validation in Software Engineering SE21Critical System Validation in Software Engineering SE21
Critical System Validation in Software Engineering SE21
koolkampus
 
color detection using open cv
color detection using open cvcolor detection using open cv
color detection using open cv
Anil Pokhrel
 
Feature Driven Development
Feature Driven DevelopmentFeature Driven Development
Feature Driven Development
dcsunu
 
Evolving role of Software,Legacy software,CASE tools,Process Models,CMMI
Evolving role of Software,Legacy software,CASE tools,Process Models,CMMIEvolving role of Software,Legacy software,CASE tools,Process Models,CMMI
Evolving role of Software,Legacy software,CASE tools,Process Models,CMMI
nimmik4u
 
Lecture 3 basic syntax and semantics
Lecture 3  basic syntax and semanticsLecture 3  basic syntax and semantics
Lecture 3 basic syntax and semantics
alvin567
 
Defining the Problem - Goals and requirements
Defining the Problem - Goals and requirementsDefining the Problem - Goals and requirements
Defining the Problem - Goals and requirements
Stephennancy
 
Dip unit-i-ppt academic year(2016-17)
Dip unit-i-ppt academic year(2016-17)Dip unit-i-ppt academic year(2016-17)
Dip unit-i-ppt academic year(2016-17)
RagavanK6
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithm
Mani Kanth
 
Chapter 1 2 - some size factors
Chapter 1   2 - some size factorsChapter 1   2 - some size factors
Chapter 1 2 - some size factors
NancyBeaulah_R
 

Similar to Object Oriented Programming Constructors & Destructors (20)

c++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskkc++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
jayeshsoni49
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Shailendra Veeru
 
Oop ppt
Oop pptOop ppt
Oop ppt
Shani Manjara
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPT
nikshaikh786
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 
asic computer is an electronic device that can receive, store, process, and o...
asic computer is an electronic device that can receive, store, process, and o...asic computer is an electronic device that can receive, store, process, and o...
asic computer is an electronic device that can receive, store, process, and o...
vaishalisharma125399
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
rayanbabur
 
Class and objects
Class and objectsClass and objects
Class and objects
nafisa rahman
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Unit - I Fundamentals of Object Oriented Programming .pptx
Unit - I Fundamentals of Object Oriented Programming .pptxUnit - I Fundamentals of Object Oriented Programming .pptx
Unit - I Fundamentals of Object Oriented Programming .pptx
tanmaynanaware20
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
zahid khan
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
dkpawar
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskkc++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPT
nikshaikh786
 
Classes, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptxClasses, Inheritance ,Packages & Interfaces.pptx
Classes, Inheritance ,Packages & Interfaces.pptx
DivyaKS18
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 
asic computer is an electronic device that can receive, store, process, and o...
asic computer is an electronic device that can receive, store, process, and o...asic computer is an electronic device that can receive, store, process, and o...
asic computer is an electronic device that can receive, store, process, and o...
vaishalisharma125399
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
rayanbabur
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
Unit - I Fundamentals of Object Oriented Programming .pptx
Unit - I Fundamentals of Object Oriented Programming .pptxUnit - I Fundamentals of Object Oriented Programming .pptx
Unit - I Fundamentals of Object Oriented Programming .pptx
tanmaynanaware20
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
Getachew Ganfur
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
dkpawar
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
Ad

Recently uploaded (20)

Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
MODULE 03 - CLOUD COMPUTING- [BIS 613D] 2022 scheme.pptx
MODULE 03 - CLOUD COMPUTING-  [BIS 613D] 2022 scheme.pptxMODULE 03 - CLOUD COMPUTING-  [BIS 613D] 2022 scheme.pptx
MODULE 03 - CLOUD COMPUTING- [BIS 613D] 2022 scheme.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
ISO 9001 quality management systemPPT.pptx
ISO 9001 quality management systemPPT.pptxISO 9001 quality management systemPPT.pptx
ISO 9001 quality management systemPPT.pptx
mesfin608
 
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdfReese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...
Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...
Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...
SiddhiKulkarni18
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Compiler Design_Syntax Directed Translation.pptx
Compiler Design_Syntax Directed Translation.pptxCompiler Design_Syntax Directed Translation.pptx
Compiler Design_Syntax Directed Translation.pptx
RushaliDeshmukh2
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
ISO 9001 quality management systemPPT.pptx
ISO 9001 quality management systemPPT.pptxISO 9001 quality management systemPPT.pptx
ISO 9001 quality management systemPPT.pptx
mesfin608
 
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdfReese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary_ The Role of Perseverance in Engineering Success.pdf
Reese McCrary
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...
Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...
Comparing Emerging Cloud Platforms –like AWS, Azure, Google Cloud, Cloud Stac...
SiddhiKulkarni18
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Compiler Design_Syntax Directed Translation.pptx
Compiler Design_Syntax Directed Translation.pptxCompiler Design_Syntax Directed Translation.pptx
Compiler Design_Syntax Directed Translation.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
Efficient Algorithms for Isogeny Computation on Hyperelliptic Curves: Their A...
IJCNCJournal
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Ad

Object Oriented Programming Constructors & Destructors

  • 1. Introduction to Object Oriented Programming (OOP) Prepared By Anita Vikram Shinde
  • 2. Procedural Oriented Language • Conventional programming, using high-level language such as COBOL, FORTRAN and C are commonly known as Procedure oriented language (POP). • In POP numbers of functions are written to accomplish the tasks 2
  • 3. Procedural Oriented Language - Structure - Function-1 Function-2 Function-3 Function-4 Function-5 Function-6 Function-7 Function-8 Main program 3
  • 4. Procedure Oriented Language - Characteristics - • Emphasis is on doing things (algorithms). • Larger programs are divided into smaller programs known as functions. • Most of the functions share global data. • Data move openly around the system from function to function. • Employs top-down approach in program design. 4
  • 5. Procedural Oriented Language - Limitations - • Data move freely around the program and are therefore vulnerable to changes caused by any function in the program. • It does not model very well the real world problems. 5
  • 6. 70 + OOP Based Programming Languages
  • 7. Object Oriented Programming • OOP treats data as critical element • Ties data more closely to the functions that operate on it & allows decomposition of problem into objects. OBJECT Operations Data OBJECT Operations Data OBJECT Operations Data Communication 7
  • 8. Procedure Oriented Programming Object Oriented Programming Divided Into In POP, program is divided into small parts called functions In OOP, program is divided into parts called objects Importance In POP, Importance is not given to data but to functions as well as sequence of actions to be done In OOP, Importance is given to the data rather than procedures or functions because it works as a real world Approach POP follows Top Down approach OOP follows Bottom Up approach Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private, Protected, etc. Data Moving In POP, Data can move freely from function to function in the system In OOP, objects can move and communicate with each other through member functions Expansion To add new data and function in POP is not so easy OOP provides an easy way to add new data and function Data Access In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system In OOP, data can not move easily from function to function, it can be kept public or private so we can control the access of data Data Hiding POP does not have any proper way for hiding data so it is less secure OOP provides Data Hiding so provides more security Examples Example of POP are : C, VB, FORTRAN, Pascal Example of OOP are : C++, JAVA, VB.NET, C#.NET 8
  • 9. Fundamentals of OOP • Objects • Classes • Encapsulation • Data Abstraction • Inheritance • Polymorphism • Dynamic Binding • Message Passing 9
  • 11. Objects • OOP uses objects as its fundamental building blocks. • Objects are the basic run-time entities in an object-oriented system. • Every object is associated with data and functions which define meaningful operations on that object. • Object is a real world existing entity. • Object is an Instance of a particular class. 11
  • 15. Class • Class is a collection of similar objects. Class 15
  • 16. Class • Define a attribute and behaviours of an object
  • 19. Class Specification • Syntax: class class_name { }; Data members Members functions 19
  • 20. A Class is a way to bind the data and its associated functions together General form of class declaration is: class Class-name { private : Variables declarations; functions declarations; public: Variables declarations; functions declarations; }; Specifying Class
  • 21. Class Specification • class Student { int st_id; char st_name[]; void read_data(); void print_data(); }; Data Members or Properties of Student Class Members Functions or Behaviours of Student Class 21
  • 22. Class Specification • Visibility of Data members & Member functions public – Accessed by member functions and all other non-member functions in the program. private – Accessed by only member functions of the class. protected – Similar to private, but accessed by all the member functions of immediate derived class Default – All items defined in the class are private. 22
  • 23. ■ The class members that have been declared as private can be accessed only from within the class. ■ The public members can be accessed from outside the class also. ■ The use of the keyword private is optional. By default , the members of class are private ■ Data members & Member functions. ■ Only member functions can have access to the private data members and private functions
  • 24. Example of class class student { int roll_no; float marks; public: void input(int a, float b); void display(void); };
  • 25. Creating Objects of class ■ student x,y,z; OR ■ class student { …….. }x, y, z;
  • 26. Accessing class data main() can not contain statement that access roll_no and marks. Only member functions can access private data . Member functions can be called only by object using dot(.) operator. Format for Calling member function : object-name . Function-name (actual- arg) ex. x.input(25, 32.5);
  • 27. Defining member function Member functions can be defined in two places: 1. Outside the class definition 2. Inside the class definition
  • 28. Outside the class definition The imp difference between member function and normal function is that member function incorporates a membership ‘identity label’ in the header. This label tells compiler which class the function belongs to.
  • 29. ■ The general form of member function definition outside class is: Return-type class-name :: fun–name(arg-list) { function body; } Ex. void student :: input(int a, float b) { rollno= a; marks = b; }
  • 30. Member Function definition inside the class class student { int rollno; float marks; public: void input( int a, float b); void display(void) { cout<<rollno<< marks; } };
  • 31. Nesting of Member Functions A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions.
  • 32. class student { int roll_no; float marks; public: void input(int a, float b); void display(void); }; void student ::input(int a, float b) { rollno= a; marks = b; display(); } void student ::display() { cout<<rollno; cout<<marks; } int main() { student s1; s1.input(10, 75.55); return 0; }
  • 33. Memory allocation for Objects ■ The memory place for objects is allocated when they are declared and not when the class is specified. This is partly true. ■ The member functions are created and placed in memory space only once when they are defined as part of class specification. ■ All objects belonging to that class use same member functions so no separate space is allocated for member functions when objects are created. ■ Only space for member variables is allocated separately for each object.
  • 34. Arrays of Objects • Several objects of the same class can be declared as an array and used just like an array of any other data type. • The syntax for declaring and using an object array is exactly the same as it is for any other type of array. 34
  • 35. Array of objects Class employee { char name[10]; int emp_code ; public: void input(void); void display(void); }; employee manager[10]; employee worker[10]; manager[i].display();
  • 36. Constructors • A constructor function is a special member function that is a member of a class and has the same name as that class, used to create, and initialize objects of the class. • Constructor function do not have return type. • Should be declared in public section. • Invoked automatically when objects are created
  • 38. Constructors • How to call this special function…? int main() { student st; ………… ………… }; class student { int st_id; public: student() { st_id=0; } }; 38
  • 39. Types of Constructors • Parameterized constructors • Constructors with default argument • Copy constructors • Dynamic constructors 39
  • 40. Example of constructor: A constructor is declared and defined as follows: class integer { int m,n; public: integer (void); … }; integer :: integer (void) { m=0; n=0; }
  • 41. Example of constructor: integer int1; A constructor that accepts no parameters is called default constructor. Special characteristics of constructor: They should be declared in public section. They are invoked automatically when the objects are created They don’t have any return type , Not even void. So they can not return values. They can have default arguments. We can not refer to their addresses They can not be inherited.
  • 42. Parameterized Constructors class Addition { int num1; int num2; int res; public: Addition(int a, int b); // constructor void add( ); void print(); }; Constructor with parameters B’Coz it’s also a function! Constructor that can take arguments is called parameterized constructor. 42
  • 43. Parameterized Constructors: • The constructor that can take arguments are called parameterized constructor. class integer { int m,n; public: integer (int a, int b); } integer :: integer (int a, int b) { m=a; n=b; }
  • 44. • By calling constructor explicitly integer int1= integer(10,20); • By calling constructor implicitly integer int1(10,20); Constructor with default arguments: Ex. Complex(float real, float imag=10); Complex c1(2.0); Complex c2(3.0, 5.0);
  • 45. Constructors with Default Argument class Addition { int num1; int num2; int res; public: Addition(int a, int b=0); // constructor void add( ); void print(); }; Constructor with default parameter. 45
  • 46. Copy constructor Copy Constructor is used to declare and initialize object from another object. integer (integer &I2); //Function prototyping ex. integer I2(I1); OR integer I2=I1; But I2=I1; Won’t invoke copy constructor.
  • 47. Copy Constructor class code { int id; public: code() //default constructor { id=0;} code(int a){id=a;} // parameterized constructor code(code &obj) // copy constructor { id=obj.id; } void display() { cout<<id; } }; int main() { code A(100); code B(A); code C=A; code D; D=A; // wrong syntax for copy construcor cout<<“ id of A:”; A.display(); cout<<“ id of B:”; B.display(); cout<<“ id of C:”; C.display(); cout<<“ id of D:”; D.display(); } 47
  • 48. Multiple constructors in a class/ Can we overload constructors class integer { int m,n; public: integer( ){m=0;n=0;} //Default constructor integer(int a , int b) //Parameterized constructor {m=a;n=b;} integer(integer &j) //Copy constructor {m=j.m; n=j.n;} };
  • 49. • integer I1; invoke Default constructor • integer I2(5,10); invoke Parameterized constructor • integer I3(I2); invoke Copy constructor • This is known as overloading of constructor Dynamic constructor: • The constructor can be used to allocate memory while creating objects. • Allocation of memory to objects at the time of their construction is known as dynamic construction of object. • The memory is allocated with the help of new operator.
  • 50. Destructors • A destructor is used to destroy the objects that have been created by constructor. • The destructor is a member function whose name is same as the class name but preceded by tilde(~). Ex. ~integer ( ); • A destructor never takes any arguments nor does it return any value • It will be invoked implicitly by the compiler upon exit from the program (or block or function) to clean up storage that is no longer accessible. • It's good practice to use destructor to release memory for future use. • When new is used in constructor then use delete in destructor. • The objects are destroyed in the reverse order of creation.
  • 51. #include<iostream.h> int count=0; class alpha { public: alpha( ) { count++; cout<<“No of object created<< count; } ~alpha( ) { cout<<“No of object destroyed<< count; count--; }
  • 52. int main( ) { cout<< “Enter main”; alpha A1, A2, A3, A4; { cout<<“Enter Block1”; alpha A5; } { cout<<“Enter Block2”; alpha A6; } cout<<“ Re-enter Main”; return 0; }
  • 53. • OUTPUT Enter Main No of object created 1 No of object created 2 No of object created 3 No of object created 4 Enter Block1 No of object created 5 No of object destroyed 5 Enter Block2 No of object created 5 No of object destroyed 5 Re-Enter Main No of object destroyed 4 No of object destroyed 3 No of object destroyed 2 No of object destroyed 1