Friend functions with objects of similar classes
Note: Passing objects to function and function returning an object
Question:1
Class Distance (feet and inches)
C++ program to declare a class named distance with attributes as feet and inches. Take input for two distances calculate and display their sum.
Sol:
Solution In CodeBlocks/DevC++
#include<iostream> #include<conio.h> using namespace std; class distance1 { private: int feet,inches; public: void read(); void show(); friend distance1 add(distance1 d1,distance1 d2); }; void distance1::read() { cout<<"Enter feet and inches "; cin>>feet>>inches; } void distance1::show() { cout<<"Feet "<<feet<<" Inches "<<inches<<endl; } distance1 add(distance1 d1,distance1 d2) { distance1 t; t.feet=d1.feet+d2.feet; t.inches=d1.inches+d2.inches; if(t.inches>=12) { t.feet=t.feet+t.inches/12; t.inches=t.inches%12; } return(t); } int main() { /* clrscr(); */ distance1 e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); return(0); }
/* Output */ Enter feet and inches 5 10 Enter feet and inches 6 11 Feet 5 Inches 10 Feet 6 Inches 11 Feet 12 Inches 9
Solution In Turboc3
/* this program is developed in turboc3 */ #include<iostream.h> #include<conio.h> class distance { private: int feet,inches; public: void read(); void show(); friend distance add(distance d1,distance d2); }; void distance::read() { cout<<"Enter feet and inches "; cin>>feet>>inches; } void distance::show() { cout<<feet<<" "<<inches<<endl; } distance add(distance d1,distance d2) { distance t; t.feet=d1.feet+d2.feet; t.inches=d1.inches+d2.inches; if(t.inches>=12) { t.feet=t.feet+t.inches/12; t.inches=t.inches%12; } return(t); } void main() { clrscr(); distance e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); }
Question:2
Class Distance (km and m)
C++ program to declare a class named distance with attributes as km and m. Take input for two distances calculate and display their sum.
Sol:
#include<iostream> #include<conio.h> using namespace std; class distance1 { private: int km,m; public: void read(); void show(); friend distance1 add(distance1 d1,distance1 d2); }; void distance1::read() { cout<<"Enter km and m "; cin>>km>>m; } void distance1::show() { cout<<"KM "<<km<<" M "<<m<<endl; } distance1 add(distance1 d1,distance1 d2) { distance1 t; t.km=d1.km+d2.km; t.m=d1.m+d2.m; if(t.m>=1000) { t.km=t.km+t.m/1000; t.m=t.m%1000; } return(t); } int main() { /* clrscr(); */ distance1 e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); return(0); }
/* Output */ Enter km and m 5 800 Enter km and m 6 700 KM 5 M 800 KM 6 M 700 KM 12 M 500
Question:3
Class Distance (m and cm)
C++ program to declare a class named distance with attributes as m and cm. Take input for two distances calculate and display their sum.
Sol:
#include<iostream> #include<conio.h> using namespace std; class distance1 { private: int m,cm; public: void read(); void show(); friend distance1 add(distance1 d1,distance1 d2); }; void distance1::read() { cout<<"Enter m and cm "; cin>>m>>cm; } void distance1::show() { cout<<"m "<<m<<" cm "<<cm<<endl; } distance1 add(distance1 d1,distance1 d2) { distance1 t; t.m=d1.m+d2.m; t.cm=d1.cm+d2.cm; if(t.cm>=100) { t.m=t.m+t.cm/100; t.cm=t.cm%100; } return(t); } int main() { /* clrscr(); */ distance1 e1,e2,e3; e1.read();e2.read(); e1.show();e2.show(); e3=add(e1,e2); e3.show(); getch(); return(0); }
/* Output */ Enter m and cm 5 20 Enter m and cm 6 90 m 5 cm 20 m 6 cm 90 m 12 cm 10