SlideShare a Scribd company logo
1 of 62
Download to read offline
Acropolis Institute of
Technology & Research, Indore
www.acropolis.in
Object Oriented Programming and
Methodology
Unit 2:
Abstraction and
Encapsulation
Abstraction
❖Abstraction is one of the key concepts of object-oriented
programming (OOP) languages.
❖Abstraction means displaying only essential information and
hiding the details
Cont.
❖Its main goal is to handle complexity by hiding unnecessary
details from the user.
❖That enables the user to implement more complex logic on
top of the provided abstraction without understanding or
even thinking about all the hidden complexity.
Abstraction: Real life Example
Abstraction: Real life Example
Abstraction in OOP
❖Objects in an OOP language provide an abstraction that hides the
internal implementation details.
❖We just need to know which methods of the object are available to
call and which input parameters are needed to trigger a specific
operation.
❖But we don’t need to understand how this method is implemented
and which kinds of actions it has to perform to create the expected
result.
Program for Abstraction
#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
Advantages of Data Abstraction:
❖Helps to avoid writing the low level code
❖Avoids code duplication and increases reusability.
❖Can change internal implementation of class independently
without affecting the user.
❖Helps to increase security of an application or program as
only important details are provided to the user.
Encapsulation
❖Encapsulation refers to the binding of data with the
methods that operate on that data, or the restricting of
direct access to some of an object's components
Program for Encapsulation
#include<iostream>
using namespace std;
class Encapsulation
{
private:
// data hidden from outside world
int x;
public:
// function to set value of
// variable x
void set(int a)
{
x =a;
}
// function to return value of
// variable x
int get()
{
return x;
}
};
// main function
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}
Content
❖ Implementation of class and object in C++.
❖ Access Modifiers.
❖ Object Data Type.
Implementation of Class and Object
What is Class?
❖ Class is a user defined data type which can be used, just like any
other basic data type, to declare variables. In Object Oriented
Programming, class is a collection of similar objects.
❖ A class describes a group of objects with similar properties,
common behavior, and common relationships.
What it contains?
❖ A class contains the entire set of data and code to manipulate the
data (known as function). A class is a way to bind the data and its
associated functions together. It allows the data and functions to be
hidden, if necessary, from external use. A class contains both the
data and functions. The internal data of a class is called data
member and functions are called member functions.
Implementation of Class and Object
Fig. : class contains data and function
Need of Class
Classes are required to represent real world
entities that not only have data type properties
but also associated operations. The class
provides support for data hiding, abstraction,
encapsulation, inheritance and polymorphism.
Implementation of class in C++
❖ Look at the following class declaration:
Implementation of class in C++
● A class should give a meaningful name as this name becomes a new type identifier that
can be used to declare objects of class type.
● A class should contain data member and member functions. The data members are
usually declared as private and the member functions as public.
● All members of a class are by default private, and scope of private variable is limited
only within the class.
Implementation of Object
What is Object?
❖ Object is an instance of a class. It is a basic run-time entity in an
object oriented system. The class variables are known as objects
or instances of class.
Example
❖ A person, place or any data item can become an object. It can be
individual item, unit or entity. Any item that program has to
handle is an object.
Implementation of Object
***Note:
❖ Objects are the thing, we think about first in program design, and
object is what actually runs on the computer in programming.
How many objects of a class can be created?
❖ Once a class has been defined, we can create any number of
objects belonging to that class.
Implementation of Object
Implementation of object in C++
● The declaration of an object is similar to that of a variable of any basic type. Objects can
be created, when a class is defined, by placing object names immediately after the
closing brace, similarly we do create tag in a structure.
Syntax: class_name object_name;
● class class_name }
● {
● data members;
● member functions;
● };
● Int main()
● {
Implementation of Object
Program
● #include <iostream.h>
● class IT
● {
● private:
● int i;
● public:
● void get()
● {
● cout<<"Enter i : ";
● cin>>i;
● }
● void show()
● {
● i++;
● cout<<"i after increment : "<<i;
● }
● };
● int main()
● {
● IT obj;
● obj.get();
● obj.show();
● return 0;
● }
Output
Class Vs Object
Sr. No. Class Object
1 A class is a collection of objects of
similar type.
An object is a basic run time entity in an object oriented
programming approach.
2 A class is a set of object Object is a variable of type class.
3 A class contains many objects. Object contains data, and code to manipulate that data.
4 Once a class has been defined, it exists till the
program is running.
Object can be created and destroyed dynamically.
5 A class has a unique name, and each
object is associated with it.
An object has behavior and identity
6 A class is a logical abstraction. An object has physical existence.
7 A class has static nature. An object has dynamic nature.
8 For example: Branch IT;
Where "Branch" is a class.
For example: Branch IT;
Where "IT" is an object.
Access Modifiers
❖ Private, Protected, Public are called visibility labels.
❖ The members that are declared private can be accessed only from
within the class.
❖ Public members can be accessed from outside the class also.
❖ In C++, data can be hidden by making it private, thus we can
achieved Data Encapsulation.
❖ It is important to note that private and protected members can not be
accessed directly using class member access operator (.)
Access Modifiers
Private
❖ The members declared in private section of a class can only be
accessed by the member function and friend function of the class.
The private members of the class are inaccessible out of the class
scope.
Protected
❖ The members declared in protected section of a class can only be
accessed by the member function and friend function of the class.
The protected members of the class are inaccessible out of the class
scope except the class derived immediately from it. It serves a
limited purpose in inheritance.
Public
❖ The members declared in public section of a class can be accessed
by all the functions present in out of the class scope.
Access Modifiers
Access Modifiers
Sr. No. Access Specifier Accessible from own
class
Accessible from
derived class
Accessible from
outside
1 Public Yes Yes Yes
2 Protected Yes No No
3 Private Yes No No
Access Modifiers
Object data type
An object can represent variable of a user defined data
type. As a variable can have its data type similarly an
object may have its type.
For example:
int i; //declares a variable i of type integer. Similarly,
IT obj; //declares an object of type IT
Object data type
Program
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● void set(int x)
● {
● a=x;
● }
● void get()
● {
● cout<<"Enter a : ";
● cin>>a;}
● void show()
● {
● cout<<"a="<<a;
● }
● };
● int main()
● {
● IT obj1,obj2;
● obj1.set(5);
● obj2.get();
● cout<<"Value provided by
argument : ";
● obj1.show();
● cout<<"nValue input by user
: ";
● obj2.show();
● return 0;}
Output
Content
❖ Constructor
❖ Destructor
❖ Object as Function Arguments
Constructor
❖ A member function can be used to give values to the data items,
however an object can initialize itself when it is created, without the
need to make a separate call to a member function. This automatic
initialization is carried out using a special member function called
constructor.
❖ Constructor is a member function of a class having same name as
class and it is executed/invoked automatically whenever an object of
its associated class is created.
❖ Constructor is called constructor because it constructs the values of
data members of the class.
Constructor Important Points
● The name of constructor function must be same as that of its class name.
● Constructors are declared with no return type not even void.
● It should have public or protected access within the class.
● It should be declared private only in rare circumstances.
● It can be invoked for const objects.
● It cannot be declared as static, virtual or const.
Constructor
Syntax:
Characteristics of Constructor
● Whenever an object is created, the constructor will be called automatically.
● We cannot refer to their addresses.
● Constructors can have default arguments.
● They cannot be inherited.
● Constructor without arguments is called a default constructor.
● For dynamic memory allocation, constructors use implicit call with new and delete
operators.
● An object with a constructor cannot be used as member function.
● Initialization of an object of the class becomes mandatory, when a constructor is
declared for a class.
● Constructor can be overloaded to accommodate many different forms of initialization.
Types of Constructor
Types of constructor available in C++ are:
❖ 1. Default constructors (constructor without parameter)
❖ 2. Parameterized constructor (constructor with parameter)
❖ 3. Copy constructor
Types of Constructor
● Default Constructors: Default constructor is the constructor which doesn’t take any
argument. It has no parameters.
● Parameterized Constructors: It is possible to pass arguments to constructors.
Typically, these arguments help initialize an object when it is created. To create a
parameterized constructor, simply add parameters to it the way you would to any other
function. When you define the constructor’s body, use the parameters to initialize the
object.
● Copy Constructor: A copy constructor is a member function which initializes an object
using another object of the same class.
Default Constructor
● #include <iostream>
● class IT
● {
● public:
● IT()
● {
● cout<<"Inside default constructor"<<endl;
● }
● void show()
● {
● cout<<"Inside member function"<<endl;
● }
● };
● int main()
● {
● IT obj;
● obj.show();
● cout<<"Inside main function"<<endl;
● return 0;
● }
Output
Parameterized Constructor
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● IT(int x) //parameterized constructor
● {
● a=x;
● }
● void show()
● {
● cout<<"Value of a="<<a;
● }
● };
● int main()
● {
● IT obj(5); //object initialization
● obj.show();
● return 0;
● }
Output
Copy Constructor
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● IT(int t)
● {
● a=t;
● }
● IT(IT &x) //copy constructor
● {
● a=x.a;
● }
● void show()
● {
● cout<<a<<endl;
● }
● };
● int main()
● {
● int x;
● cout<<"Enter x : ";
● cin>>x;
● IT obj1(x);
● IT obj2(obj1); //call to copy constructor
● cout<<"Value of a : ";
● obj1.show();
● cout<<"Value of a after copy : ";
● obj2.show();
● return 0;
● }
Output
Destructor
● Destructor is also a member function of a class whose name is also same as that of the
class preceded by a ~ (tilde sign). Destructor is executed at the end of the function
when objects are of no use, or goes out of scope.
Use of destructor
● The most common use of destructor is to de-allocate memory that was allocated for
the objects by the constructor.
Destructor
● #include <iostream.h>
● int count=0;
● class IT
● {
● public:
● IT()
● {
● count++;
● cout<<"object created using constructor :
obj"<<count<<endl;
● }
● ~IT()
● {
● cout<<"object destroyed using destructor :
● obj"<<count<<endl;
● count--;
● }
● };
● int main()
● {
● IT obj1,obj2,obj3,obj4,obj5;
● cout<<"main function
executed"<<endl;
● return 0;
● }
Output
Access Modifiers
❖ Private, Protected, Public are called visibility labels.
❖ The members that are declared private can be accessed only from
within the class.
❖ Public members can be accessed from outside the class also.
❖ In C++, data can be hidden by making it private, thus we can
achieved Data Encapsulation.
❖ It is important to note that private and protected members can not be
accessed directly using class member access operator (.)
Access Modifiers
Private
❖ The members declared in private section of a class can only be
accessed by the member function and friend function of the class.
The private members of the class are inaccessible out of the class
scope.
Protected
❖ The members declared in protected section of a class can only be
accessed by the member function and friend function of the class.
The protected members of the class are inaccessible out of the class
scope except the class derived immediately from it. It serves a
limited purpose in inheritance.
Public
❖ The members declared in public section of a class can be accessed
by all the functions present in out of the class scope.
Access Modifiers
Access Modifiers
Sr. No. Access Specifier Accessible from own
class
Accessible from
derived class
Accessible from
outside
1 Public Yes Yes Yes
2 Protected Yes No No
3 Private Yes No No
Access Modifiers
Object data type
An object can represent variable of a user defined data
type. As a variable can have its data type similarly an
object may have its type.
For example:
int i; //declares a variable i of type integer. Similarly,
IT obj; //declares an object of type IT
Object data type
Program
● #include <iostream.h>
● class IT
● {
● private:
● int a;
● public:
● void set(int x)
● {
● a=x;
● }
● void get()
● {
● cout<<"Enter a : ";
● cin>>a;}
● void show()
● {
● cout<<"a="<<a;
● }
● };
● int main()
● {
● IT obj1,obj2;
● obj1.set(5);
● obj2.get();
● cout<<"Value provided by
argument : ";
● obj1.show();
● cout<<"nValue input by user
: ";
● obj2.show();
● return 0;}
Output
Content
❖ Static data Member
❖ Static Member Function
Static Data Member
❖ The static data members provide access control to some shared
resources. They normally used to maintain values common to the
entire class.
❖ Each object has its separate set of data members in memory. The
member functions are created only once and all objects share the
functions.
❖ No separate copy of functions of each object is created in the
memory.
***Point to remember:
❖ Static data members are data objects of a class. They exist only
once in all objects of this class.
Static Data Member
Advantage of Static Data Member
● Static data members may be helpful to declare the global data which should be updated while the
program exists in the memory.
Features of Static Data Member
● Static member variables are normally used to maintain values common to the entire class.
● Static member variables are visible within the class, but its lifetime is the entire program.
● Since they are associated with the class itself rather than with any class object, they are also known as
class variables.
● Type and scope of each static member variable must be defined outside the class definition, because the
static data members are stored separately, rather than as a part of an object.
● Only one copy of that member is created for the entire class and is shared by all the objects of that class,
no matter how many objects are created.
● Static member variable is initialized to zero when the first object of its class is created, no other
initialization is permitted.
Static Data in Private Section
Static Data in Private Section
❖ Output
Static Data in Public Section
Static data in Public Section
● Output-
Static Data in Public Section
Static Data in Public Section
● Output-
Static Member Function
● The static member functions used to pre-initialize private static data members before
any object is actually created. Like data members, functions can also be declared as
static.
● Static member functions can access only static data members and functions of the same
class. The non static data members are not available to these functions.
Features of static member functions
● The static member function declared in public section can be invoked using its class
name without using its objects.
● Just one copy of static member function is created in the memory for entire class. All
objects of the class share the same copy of static member function.
Static Member Function in Public
Output
Output
***Remark:
The static member function declared in public section can be invoked using its class name without using its objects.
Static Member Function in Private
Output

More Related Content

Similar to oopm 2.pdf

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
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programmingNeelesh Shukla
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programmingsharmisivarajah
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1Geophery sanga
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesDigitalDsms
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three noskrismishra
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oopcolleges
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Objectdkpawar
 

Similar to oopm 2.pdf (20)

Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Oops
OopsOops
Oops
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
C++ training
C++ training C++ training
C++ training
 
C++ classes
C++ classesC++ classes
C++ classes
 
Object-oriented programming
Object-oriented programmingObject-oriented programming
Object-oriented programming
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
 

Recently uploaded

Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationBoston Institute of Analytics
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknowmakika9823
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 

Recently uploaded (20)

Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service BhilaiLow Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
Low Rate Call Girls Bhilai Anika 8250192130 Independent Escort Service Bhilai
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 

oopm 2.pdf

  • 1. Acropolis Institute of Technology & Research, Indore www.acropolis.in
  • 2. Object Oriented Programming and Methodology
  • 4. Abstraction ❖Abstraction is one of the key concepts of object-oriented programming (OOP) languages. ❖Abstraction means displaying only essential information and hiding the details
  • 5. Cont. ❖Its main goal is to handle complexity by hiding unnecessary details from the user. ❖That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity.
  • 8. Abstraction in OOP ❖Objects in an OOP language provide an abstraction that hides the internal implementation details. ❖We just need to know which methods of the object are available to call and which input parameters are needed to trigger a specific operation. ❖But we don’t need to understand how this method is implemented and which kinds of actions it has to perform to create the expected result.
  • 9. Program for Abstraction #include <iostream> using namespace std; class implementAbstraction { private: int a, b; public: // method to set values of // private members void set(int x, int y) { a = x; b = y; } void display() { cout<<"a = " <<a << endl; cout<<"b = " << b << endl; } }; int main() { implementAbstraction obj; obj.set(10, 20); obj.display(); return 0; }
  • 10. Advantages of Data Abstraction: ❖Helps to avoid writing the low level code ❖Avoids code duplication and increases reusability. ❖Can change internal implementation of class independently without affecting the user. ❖Helps to increase security of an application or program as only important details are provided to the user.
  • 11. Encapsulation ❖Encapsulation refers to the binding of data with the methods that operate on that data, or the restricting of direct access to some of an object's components
  • 12. Program for Encapsulation #include<iostream> using namespace std; class Encapsulation { private: // data hidden from outside world int x; public: // function to set value of // variable x void set(int a) { x =a; } // function to return value of // variable x int get() { return x; } }; // main function int main() { Encapsulation obj; obj.set(5); cout<<obj.get(); return 0; }
  • 13. Content ❖ Implementation of class and object in C++. ❖ Access Modifiers. ❖ Object Data Type.
  • 14. Implementation of Class and Object What is Class? ❖ Class is a user defined data type which can be used, just like any other basic data type, to declare variables. In Object Oriented Programming, class is a collection of similar objects. ❖ A class describes a group of objects with similar properties, common behavior, and common relationships. What it contains? ❖ A class contains the entire set of data and code to manipulate the data (known as function). A class is a way to bind the data and its associated functions together. It allows the data and functions to be hidden, if necessary, from external use. A class contains both the data and functions. The internal data of a class is called data member and functions are called member functions.
  • 15. Implementation of Class and Object Fig. : class contains data and function Need of Class Classes are required to represent real world entities that not only have data type properties but also associated operations. The class provides support for data hiding, abstraction, encapsulation, inheritance and polymorphism.
  • 16. Implementation of class in C++ ❖ Look at the following class declaration:
  • 17. Implementation of class in C++ ● A class should give a meaningful name as this name becomes a new type identifier that can be used to declare objects of class type. ● A class should contain data member and member functions. The data members are usually declared as private and the member functions as public. ● All members of a class are by default private, and scope of private variable is limited only within the class.
  • 18. Implementation of Object What is Object? ❖ Object is an instance of a class. It is a basic run-time entity in an object oriented system. The class variables are known as objects or instances of class. Example ❖ A person, place or any data item can become an object. It can be individual item, unit or entity. Any item that program has to handle is an object.
  • 19. Implementation of Object ***Note: ❖ Objects are the thing, we think about first in program design, and object is what actually runs on the computer in programming. How many objects of a class can be created? ❖ Once a class has been defined, we can create any number of objects belonging to that class.
  • 20. Implementation of Object Implementation of object in C++ ● The declaration of an object is similar to that of a variable of any basic type. Objects can be created, when a class is defined, by placing object names immediately after the closing brace, similarly we do create tag in a structure. Syntax: class_name object_name; ● class class_name } ● { ● data members; ● member functions; ● }; ● Int main() ● {
  • 21. Implementation of Object Program ● #include <iostream.h> ● class IT ● { ● private: ● int i; ● public: ● void get() ● { ● cout<<"Enter i : "; ● cin>>i; ● } ● void show() ● { ● i++; ● cout<<"i after increment : "<<i; ● } ● }; ● int main() ● { ● IT obj; ● obj.get(); ● obj.show(); ● return 0; ● } Output
  • 22. Class Vs Object Sr. No. Class Object 1 A class is a collection of objects of similar type. An object is a basic run time entity in an object oriented programming approach. 2 A class is a set of object Object is a variable of type class. 3 A class contains many objects. Object contains data, and code to manipulate that data. 4 Once a class has been defined, it exists till the program is running. Object can be created and destroyed dynamically. 5 A class has a unique name, and each object is associated with it. An object has behavior and identity 6 A class is a logical abstraction. An object has physical existence. 7 A class has static nature. An object has dynamic nature. 8 For example: Branch IT; Where "Branch" is a class. For example: Branch IT; Where "IT" is an object.
  • 23. Access Modifiers ❖ Private, Protected, Public are called visibility labels. ❖ The members that are declared private can be accessed only from within the class. ❖ Public members can be accessed from outside the class also. ❖ In C++, data can be hidden by making it private, thus we can achieved Data Encapsulation. ❖ It is important to note that private and protected members can not be accessed directly using class member access operator (.)
  • 24. Access Modifiers Private ❖ The members declared in private section of a class can only be accessed by the member function and friend function of the class. The private members of the class are inaccessible out of the class scope. Protected ❖ The members declared in protected section of a class can only be accessed by the member function and friend function of the class. The protected members of the class are inaccessible out of the class scope except the class derived immediately from it. It serves a limited purpose in inheritance. Public ❖ The members declared in public section of a class can be accessed by all the functions present in out of the class scope.
  • 26. Access Modifiers Sr. No. Access Specifier Accessible from own class Accessible from derived class Accessible from outside 1 Public Yes Yes Yes 2 Protected Yes No No 3 Private Yes No No
  • 28. Object data type An object can represent variable of a user defined data type. As a variable can have its data type similarly an object may have its type. For example: int i; //declares a variable i of type integer. Similarly, IT obj; //declares an object of type IT
  • 29. Object data type Program ● #include <iostream.h> ● class IT ● { ● private: ● int a; ● public: ● void set(int x) ● { ● a=x; ● } ● void get() ● { ● cout<<"Enter a : "; ● cin>>a;} ● void show() ● { ● cout<<"a="<<a; ● } ● }; ● int main() ● { ● IT obj1,obj2; ● obj1.set(5); ● obj2.get(); ● cout<<"Value provided by argument : "; ● obj1.show(); ● cout<<"nValue input by user : "; ● obj2.show(); ● return 0;} Output
  • 30. Content ❖ Constructor ❖ Destructor ❖ Object as Function Arguments
  • 31. Constructor ❖ A member function can be used to give values to the data items, however an object can initialize itself when it is created, without the need to make a separate call to a member function. This automatic initialization is carried out using a special member function called constructor. ❖ Constructor is a member function of a class having same name as class and it is executed/invoked automatically whenever an object of its associated class is created. ❖ Constructor is called constructor because it constructs the values of data members of the class.
  • 32. Constructor Important Points ● The name of constructor function must be same as that of its class name. ● Constructors are declared with no return type not even void. ● It should have public or protected access within the class. ● It should be declared private only in rare circumstances. ● It can be invoked for const objects. ● It cannot be declared as static, virtual or const.
  • 34. Characteristics of Constructor ● Whenever an object is created, the constructor will be called automatically. ● We cannot refer to their addresses. ● Constructors can have default arguments. ● They cannot be inherited. ● Constructor without arguments is called a default constructor. ● For dynamic memory allocation, constructors use implicit call with new and delete operators. ● An object with a constructor cannot be used as member function. ● Initialization of an object of the class becomes mandatory, when a constructor is declared for a class. ● Constructor can be overloaded to accommodate many different forms of initialization.
  • 35. Types of Constructor Types of constructor available in C++ are: ❖ 1. Default constructors (constructor without parameter) ❖ 2. Parameterized constructor (constructor with parameter) ❖ 3. Copy constructor
  • 36. Types of Constructor ● Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters. ● Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object. ● Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same class.
  • 37. Default Constructor ● #include <iostream> ● class IT ● { ● public: ● IT() ● { ● cout<<"Inside default constructor"<<endl; ● } ● void show() ● { ● cout<<"Inside member function"<<endl; ● } ● }; ● int main() ● { ● IT obj; ● obj.show(); ● cout<<"Inside main function"<<endl; ● return 0; ● } Output
  • 38. Parameterized Constructor ● #include <iostream.h> ● class IT ● { ● private: ● int a; ● public: ● IT(int x) //parameterized constructor ● { ● a=x; ● } ● void show() ● { ● cout<<"Value of a="<<a; ● } ● }; ● int main() ● { ● IT obj(5); //object initialization ● obj.show(); ● return 0; ● } Output
  • 39. Copy Constructor ● #include <iostream.h> ● class IT ● { ● private: ● int a; ● public: ● IT(int t) ● { ● a=t; ● } ● IT(IT &x) //copy constructor ● { ● a=x.a; ● } ● void show() ● { ● cout<<a<<endl; ● } ● }; ● int main() ● { ● int x; ● cout<<"Enter x : "; ● cin>>x; ● IT obj1(x); ● IT obj2(obj1); //call to copy constructor ● cout<<"Value of a : "; ● obj1.show(); ● cout<<"Value of a after copy : "; ● obj2.show(); ● return 0; ● } Output
  • 40. Destructor ● Destructor is also a member function of a class whose name is also same as that of the class preceded by a ~ (tilde sign). Destructor is executed at the end of the function when objects are of no use, or goes out of scope. Use of destructor ● The most common use of destructor is to de-allocate memory that was allocated for the objects by the constructor.
  • 41. Destructor ● #include <iostream.h> ● int count=0; ● class IT ● { ● public: ● IT() ● { ● count++; ● cout<<"object created using constructor : obj"<<count<<endl; ● } ● ~IT() ● { ● cout<<"object destroyed using destructor : ● obj"<<count<<endl; ● count--; ● } ● }; ● int main() ● { ● IT obj1,obj2,obj3,obj4,obj5; ● cout<<"main function executed"<<endl; ● return 0; ● } Output
  • 42. Access Modifiers ❖ Private, Protected, Public are called visibility labels. ❖ The members that are declared private can be accessed only from within the class. ❖ Public members can be accessed from outside the class also. ❖ In C++, data can be hidden by making it private, thus we can achieved Data Encapsulation. ❖ It is important to note that private and protected members can not be accessed directly using class member access operator (.)
  • 43. Access Modifiers Private ❖ The members declared in private section of a class can only be accessed by the member function and friend function of the class. The private members of the class are inaccessible out of the class scope. Protected ❖ The members declared in protected section of a class can only be accessed by the member function and friend function of the class. The protected members of the class are inaccessible out of the class scope except the class derived immediately from it. It serves a limited purpose in inheritance. Public ❖ The members declared in public section of a class can be accessed by all the functions present in out of the class scope.
  • 45. Access Modifiers Sr. No. Access Specifier Accessible from own class Accessible from derived class Accessible from outside 1 Public Yes Yes Yes 2 Protected Yes No No 3 Private Yes No No
  • 47. Object data type An object can represent variable of a user defined data type. As a variable can have its data type similarly an object may have its type. For example: int i; //declares a variable i of type integer. Similarly, IT obj; //declares an object of type IT
  • 48. Object data type Program ● #include <iostream.h> ● class IT ● { ● private: ● int a; ● public: ● void set(int x) ● { ● a=x; ● } ● void get() ● { ● cout<<"Enter a : "; ● cin>>a;} ● void show() ● { ● cout<<"a="<<a; ● } ● }; ● int main() ● { ● IT obj1,obj2; ● obj1.set(5); ● obj2.get(); ● cout<<"Value provided by argument : "; ● obj1.show(); ● cout<<"nValue input by user : "; ● obj2.show(); ● return 0;} Output
  • 49. Content ❖ Static data Member ❖ Static Member Function
  • 50. Static Data Member ❖ The static data members provide access control to some shared resources. They normally used to maintain values common to the entire class. ❖ Each object has its separate set of data members in memory. The member functions are created only once and all objects share the functions. ❖ No separate copy of functions of each object is created in the memory. ***Point to remember: ❖ Static data members are data objects of a class. They exist only once in all objects of this class.
  • 51. Static Data Member Advantage of Static Data Member ● Static data members may be helpful to declare the global data which should be updated while the program exists in the memory. Features of Static Data Member ● Static member variables are normally used to maintain values common to the entire class. ● Static member variables are visible within the class, but its lifetime is the entire program. ● Since they are associated with the class itself rather than with any class object, they are also known as class variables. ● Type and scope of each static member variable must be defined outside the class definition, because the static data members are stored separately, rather than as a part of an object. ● Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created. ● Static member variable is initialized to zero when the first object of its class is created, no other initialization is permitted.
  • 52. Static Data in Private Section
  • 53. Static Data in Private Section ❖ Output
  • 54. Static Data in Public Section
  • 55. Static data in Public Section ● Output-
  • 56. Static Data in Public Section
  • 57. Static Data in Public Section ● Output-
  • 58. Static Member Function ● The static member functions used to pre-initialize private static data members before any object is actually created. Like data members, functions can also be declared as static. ● Static member functions can access only static data members and functions of the same class. The non static data members are not available to these functions. Features of static member functions ● The static member function declared in public section can be invoked using its class name without using its objects. ● Just one copy of static member function is created in the memory for entire class. All objects of the class share the same copy of static member function.
  • 60. Output Output ***Remark: The static member function declared in public section can be invoked using its class name without using its objects.