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

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 theother 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 { intfeet, 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 onecopy 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 { staticint 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; staticint 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
  • 9.