0% found this document useful (0 votes)
44 views12 pages

Example Programs 5 Friend Function

The document contains 7 C++ programs that demonstrate the use of classes and objects to calculate simple interest and amount. The programs show how to: 1) Define a class with data members and member functions to calculate simple interest and amount 2) Pass objects to functions by value, address, and reference 3) Use friend functions to calculate values and swap data members between two classes 4) Calculate the sum of data member arrays from two classes and store in a third class using a friend function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views12 pages

Example Programs 5 Friend Function

The document contains 7 C++ programs that demonstrate the use of classes and objects to calculate simple interest and amount. The programs show how to: 1) Define a class with data members and member functions to calculate simple interest and amount 2) Pass objects to functions by value, address, and reference 3) Use friend functions to calculate values and swap data members between two classes 4) Calculate the sum of data member arrays from two classes and store in a third class using a friend function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. C++ Program to find simple interest and amount using class and object.

#include<iostream>
using namespace std;

class SI

{
private:
float p;

float r;

float t;

float sint,amt;

public:

void input();

void calc();

void show();

};

void SI :: input()
{
cout<<"Enter the Principal, Rate of Interest (in %) and Time (in years): ";
cin>>p>>r>>t;

}
void SI :: calc()
{
sint=(p*r*t)/100;
amt=p+sint;

}
void SI :: show()
{
cout<<"The Simple Interest is Rs."<<sint<<endl;

cout<<"The Amount is Rs."<<amt<<endl;

int main()
{
SI ob;

ob.input();

ob.calc();

ob.show();

return 0;
}

2. C++ Program to find simple interest and amount using class, object and passing object to a
function by value.

#include<iostream>
using namespace std;
class SI
{
public:
float p,r,t;

float sint;

void input();

float calc(SI);

void show(SI);

};
void SI :: input()

{
cout<<"Enter the Principal, Rate of Interest (in %) and Time (in years): ";

cin>>p>>r>>t;
}

float SI :: calc(SI ob)


{
sint=(ob.p*ob.r*ob.t)/100;

return(sint);
}
void SI :: show(SI ob)
{
cout<<"The Simple Interest is Rs."<<ob.sint<<endl;

cout<<"The Amount is Rs."<<ob.p+ob.sint<<endl;

int main()
{
SI obj;

obj.input();

obj.sint=obj.calc(obj);

obj.show(obj);

return 0;
}
3. C++ Program to find simple interest and amount using class, object and passing object to a
function by address.

#include<iostream>
using namespace std;

class SI
{
private:

float p;

float r;

float t;

float sint,amt;

public:

void input();

void calc(SI *);

void show(SI *);

};

void SI :: input()
{
cout<<"Enter the Principal, Rate of Interest (in %) and Time (in years): ";

cin>>p>>r>>t;
}
void SI :: calc(SI *ob)
{
sint=(ob->p*ob->r*ob->t)/100;

amt=ob->p+ob->sint;
}
void SI :: show(SI *ob)
{
cout<<"The Simple Interest is Rs."<<ob->sint<<endl;

cout<<"The Amount is Rs."<<ob->amt<<endl;


}

int main()

SI obj;

obj.input();

obj.calc(&obj);

obj.show(&obj);

return 0;

4. C++ Program to find simple interest and amount using class, object and passing object to a
function by reference.

#include<iostream>
using namespace std;

class SI
{
private:

float p;

float r;

float t;

float sint,amt;
public:

void input();

void calc(SI &);

void show(SI &);

};

void SI :: input()
{
cout<<"Enter the Principal, Rate of Interest (in %) and Time (in years): ";

cin>>p>>r>>t;
}
void SI :: calc(SI &ob)
{
sint=(ob.p*ob.r*ob.t)/100;

amt=ob.p+ob.sint;
}
void SI::show(SI &ob)
{
cout<<"The Simple Interest is Rs."<<ob.sint<<endl;

cout<<"The Amount is Rs."<<ob.amt<<endl;


}

int main()
{
SI obj;

obj.input();

obj.calc(obj);

obj.show(obj);

return 0;
}
5. C++ Program to find simple interest and amount using class and friend function.

#include<iostream>
using namespace std;

class SI
{
private:
float p;
float r;
float t;
public:
void input();
friend void calc(SI &);
};
void SI :: input()
{
cout<<"Enter the Principal, Rate of Interest (in %) and Time (in years): ";
cin>>p>>r>>t;
}
void calc(SI &ob)
{
float sint=(ob.p*ob.r*ob.t)/100;
cout<<"The Simple Interest is Rs."<<sint<<endl;
cout<<"The Amount is Rs."<<ob.p+sint<<endl;
}

int main()
{
SI obj;
obj.input();
calc(obj);
return 0;
}
6. C++ Program to swap the data members of two classes using friend function.
#include<iostream>
using namespace std;

class second;
class first
{
private:
float a;
public:
void input();
friend void swap(first &, second &);
void show();
};
void first :: input()
{
cout<<"Enter a number: ";
cin>>a;
}
void first :: show()
{
cout<<"\n The number of class first is: "<<a<<endl;
}

class second
{
private:
float b;
public:
void input();
friend void swap(first &, second &);
void show();
};
void second :: input()
{
cout<<"Enter a number: ";
cin>>b;
}
void second :: show()
{
cout<<"\n The number of class second is: "<<b<<endl;
}

void swap(first &ob1, second &ob2)


{
float temp=ob1.a;
ob1.a=ob2.b;
ob2.b=temp;
}

int main()
{
first obj1;
second obj2;
obj1.input();
obj2.input();
cout<<"\n Before Swapping: "<<endl;
obj1.show();
obj2.show();
swap(obj1,obj2);
cout<<"\n After Swapping: "<<endl;
obj1.show();
obj2.show();
return 0;
}
7. C++ Program to find the sum of the data member array elements of two classes A and B in the
data member array element of third class C using friend function.

#include<iostream>
using namespace std;
class B;
class C;
class A
{
private:
int a[5];
public:
void input();
friend void add(A &, B &, C &);
void show();
};
void A :: input()
{
cout<<"\n Enter the array elements of class A: ";
for(int i=0;i<5;i++)
cin>>a[i];
}
void A :: show()
{
cout<<"\n The array elements of class A are: "<<endl;
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
}

class B
{
private:
int b[5];
public:
void input();
friend void add(A &, B &, C &);
void show();
};
void B :: input()
{
cout<<"\n Enter the array elements of class B: ";
for(int i=0;i<5;i++)
cin>>b[i];
}
void B :: show()
{
cout<<"\n The array elements of class B are: "<<endl;
for(int i=0;i<5;i++)
cout<<b[i]<<" ";
}
class C
{
private:
int c[5];
public:
friend void add(A &, B &, C &);
void show();
};
void C :: show()
{
cout<<"\n The resultant array elements of class C are: "<<endl;
for(int i=0;i<5;i++)
cout<<c[i]<<" ";
}
void add(A &ob1, B &ob2, C &ob3)
{
for(int i=0;i<5;i++)
ob3.c[i]=ob1.a[i]+ob2.b[i];
}
int main()
{
A obj1;
B obj2;
C obj3;
obj1.input();
obj2.input();
cout<<"\n Before Addition: "<<endl;
obj1.show();
obj2.show();
add(obj1,obj2,obj3);
cout<<"\n After Addition: "<<endl;
obj3.show();
return 0;
}

You might also like