CPP Programming
About Lesson

Like any regular variable of basic data type, objects also can be passed as function arguments. 

//Example c++ program to demonstrate how objects can be passed as function parameters
#include<iostream>
using namespace std;

class time
{
int hrs;
int mins;
public:
void getTime(int h, int m)
{
hrs=h;
mins=m;
}
void putTime(void)
{
cout<<hrs<<“Hrs and “<<mins<<“mins”<<endl;
}
void sum(time, time);

};

void time::sum(time t1,time t2)
{
mins=t1.mins+t2.mins;
hrs=mins/60;
mins=mins%60;
hrs=hrs+t1.hrs+t2.hrs;
}

main()
{
time t1,t2,t3;
t1.getTime(2,45);
t2.getTime(3,30);
t3.sum(t1,t2);

cout<<“T1= “;t1.putTime();
cout<<“T2= “;t2.putTime();
cout<<“T3= “;t3.putTime();

}

An object can also be passed to a non-member function.  However, the object can access the public members only but not private members.  If you want to make the outsider function to access private data of the class through its object passed as argument, make the outsider function as Friend function to the class.

Member function of a class can be friend to another class.  In this case, friend introduction to be done along with scope resolution operator.

Class X

{

int func();

};

 

Class Y

{

friend int X::func();

 

};

 

We can also declare all the member functions of one class as the friend functions of another class.  In such case, the class is called s friend class.

 

Eg:

Class Z

{

friend class X; //all the member functions of X are friends to Z

};

//Example C++ program to demonstrate forward declaration or a friend function to many classes
#include<iostream>
using namespace std;

class ABC;

class XYZ
{
int x;
public:
void setValue(){cout<<“Enter your first number: “; cin>>x;}
friend void max(XYZ, ABC);
};

class ABC
{
int y;
public:
void setValue(){cout<<“Enter your second number: “; cin>>y;};
friend void max(XYZ,ABC);

};

void max(XYZ m, ABC n)
{
if(m.x>n.y)
cout<<“biggest number in both objects is: “<<m.x;
else
cout<<“biggest number in both objects is: “<<n.y;
}

main()
{
XYZ obj1;
obj1.setValue();
ABC obj2;
obj2.setValue();
max(obj1,obj2);
}

Argument passing to function can be done in two ways:

  • Pass by value
  • Pass by reference

 

//Example C++ program to show how reference can be used to change private data of class

#include<iostream>

using namespace std;

 

class class2;   //forward declaration

 

class class1

{

    int value1;

public:

    void indata(int a){value1=a;}

    void display(void){cout<<“Value1 = “<<value1<<endl;}

    friend void swapval(class1, class2);

    friend void swapref(class1 &, class2 &);

};

 

class class2

{

    int value2;

public:

    void indata(int a){value2=a;}

    void display(void){cout<<“Value2 = “<<value2<<endl;}

    friend void swapval(class1, class2);

    friend void swapref(class1 &, class2 &);

};

 

void swapval(class1 x, class2 y)

{

    int temp=x.value1;

    x.value1=y.value2;

    y.value2=temp;

}

 

void swapref(class1 &x, class2 &y)

{

    int temp=x.value1;

    x.value1=y.value2;

    y.value2=temp;

}

 

main()

{

    class1 c1;

    class2 c2;

 

    c1.indata(100);

    c2.indata(200);

 

    cout<<“Values before Exchange”<<endl;

    c1.display();

    c2.display();

 

    swapval(c1,c2);

    cout<<endl;

    cout<<“Values after swapval call”<<endl;

    c1.display();

    c2.display();

 

    swapref(c1,c2);

    cout<<endl;

    cout<<“Values after swapref call”<<endl;

    c1.display();

    c2.display();

}

You cannot copy content of this page