SlideShare a Scribd company logo
29
Objects As Function
Arguments• Like any other variable, objects can also be
passed to the function, as an argument
• There are two ways of doing this
– Pass by value
– Pass by reference
• In the pass by value, the copy of the object is
passed to the function
• So, the changes made to the object inside
the function do not affect the actual object
30
Contd…..
• On the other hand, address of the object is
passed in the case of pass by reference
• So the changes made to the object inside
the function are reflected in the actual
object
• This method is considered more efficient
31
• Eg:
class height
{
int feet, inches; public:
void get_height()
{
cout<<“Enter height in feet and
inches”;
cin>>feet>>inches;
}
void put_height()
{
cout<<Feet:<<feet; cout<<and
inches:<<inches;
}
void sum(height, height);
};
void height:: sum(height h1, height
h2)
{
inches= h1.inches+h2.inches;
feet=inches/12; inches=inches%12;
feet=feet+h1.feet+h2.feet;
}
int main()
{
height h1, h2, h3; h1.get_height();
h2.get_height();
h3.sum(h1, h2);
cout<<“Height :”<<h .put_height();ϭ ϭ
32
Static Data Members
• Each object of a class maintain their own
copy of data member
• However, in some cases, it may be necessary
that all objects of a class have access to the
same copy of a single variable.
• This can be made possible by using static
variable
• A static variable is declared using the static
keyword
33
Contd…
• Only one copy of static member is created for
the entire class and is shared by all the
objects of that class, no matter how many
objects are created
• It is visible only within the class, but its
lifetime is the entire program
• It is initialized to zero when the first object of
its class is created
• It is also known as class variable
34
• Eg:
class A
{
static int
count; int
variable;
public:
A()
{
count++;
}
void get_var()
{
cin>>variable;
}
void put_var()
{
cout<<variable;
}
void put_count()
{
cout<<count;
}
};
int A:: count; // the type and
scope of each static member
variable is defined outside class
definition
main()
{
A a, b, c; a.put_count();
b.put_count(); c.put_count();
return(0);
}
Output: 1 2 3
35
Static Member Function
• In a class, functions can also be declared as
static
• Properties of static functions are:
– They can access only other STATIC
members(functions or variables) declared in the
same class
– They can be called using class name ( instead of
its object)
– Eg: class_name::function_name
class A
{
int no;
static int count; //static member
public:
void set_no()
{
count++;
no=count;
}
void put_no()
{
}
static void put_count() //static member
function accessing static member
{
}
};
Int A::count;
main()
{
A a1, a2;
a1.set_no();
a2.set_no();
A::put_count();
a1.set_no();
a2.set_no();
A::put_count();
a1.put_no();
a2.put_no();
return(0);
} 40
THE
END
41

More Related Content

What's hot

This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
Learn By Watch
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
Mayank Jain
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
 
functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 

What's hot (20)

This pointer
This pointerThis pointer
This pointer
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Inline function
Inline functionInline function
Inline function
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Call by value
Call by valueCall by value
Call by value
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
functions of C++
functions of C++functions of C++
functions of C++
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Friend functions
Friend functions Friend functions
Friend functions
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Inheritance
InheritanceInheritance
Inheritance
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 

Similar to C++ Returning Objects

class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
AshrithaRokkam
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
saifnasir3
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
urvashipundir04
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
Michael Heron
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
BArulmozhi
 
Functions
FunctionsFunctions
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
ThamizhselviKrishnam
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
Unviersity of balochistan quetta
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
Shehzad Rizwan
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
sai kumar
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
10.ppt
10.ppt10.ppt
10.ppt
BNJYOTHI
 

Similar to C++ Returning Objects (20)

class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Functions
FunctionsFunctions
Functions
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
10.ppt
10.ppt10.ppt
10.ppt
 

Recently uploaded

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 

Recently uploaded (20)

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 

C++ Returning Objects

  • 1. 29 Objects As Function Arguments• Like any other variable, objects can also be passed to the function, as an argument • There are two ways of doing this – Pass by value – Pass by reference • In the pass by value, the copy of the object is passed to the function • So, the changes made to the object inside the function do not affect the actual object
  • 2. 30 Contd….. • On the other hand, address of the object is passed in the case of pass by reference • So the changes made to the object inside the function are reflected in the actual object • This method is considered more efficient
  • 3. 31 • Eg: class height { int feet, inches; public: void get_height() { cout<<“Enter height in feet and inches”; cin>>feet>>inches; } void put_height() { cout<<Feet:<<feet; cout<<and inches:<<inches; } void sum(height, height); }; void height:: sum(height h1, height h2) { inches= h1.inches+h2.inches; feet=inches/12; inches=inches%12; feet=feet+h1.feet+h2.feet; } int main() { height h1, h2, h3; h1.get_height(); h2.get_height(); h3.sum(h1, h2); cout<<“Height :”<<h .put_height();ϭ ϭ
  • 4. 32 Static Data Members • Each object of a class maintain their own copy of data member • However, in some cases, it may be necessary that all objects of a class have access to the same copy of a single variable. • This can be made possible by using static variable • A static variable is declared using the static keyword
  • 5. 33 Contd… • Only one copy of static member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created • It is visible only within the class, but its lifetime is the entire program • It is initialized to zero when the first object of its class is created • It is also known as class variable
  • 6. 34 • Eg: class A { static int count; int variable; public: A() { count++; } void get_var() { cin>>variable; } void put_var() { cout<<variable; } void put_count() { cout<<count; } }; int A:: count; // the type and scope of each static member variable is defined outside class definition main() { A a, b, c; a.put_count(); b.put_count(); c.put_count(); return(0); } Output: 1 2 3
  • 7. 35 Static Member Function • In a class, functions can also be declared as static • Properties of static functions are: – They can access only other STATIC members(functions or variables) declared in the same class – They can be called using class name ( instead of its object) – Eg: class_name::function_name
  • 8. class A { int no; static int count; //static member public: void set_no() { count++; no=count; } void put_no() { } static void put_count() //static member function accessing static member { } }; Int A::count; main() { A a1, a2; a1.set_no(); a2.set_no(); A::put_count(); a1.set_no(); a2.set_no(); A::put_count(); a1.put_no(); a2.put_no(); return(0); } 40