C++ : Friend Function 4

Question:4
Class Distance (km, m and cm)
C++ program to declare a class named distance with attributes as km, 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 km,m,cm;
public:
     void read();
     void show();
     friend distance1 add(distance1 d1,distance1 d2);
};
void distance1::read()
{
  cout<<"Enter km, m and cm ";
  cin>>km>>m>>cm;
}
void distance1::show()
{
  cout<<"km "<<km<<" m "<<m<<" cm "<<cm<<endl;
}
distance1 add(distance1 d1,distance1 d2)
{
  distance1 t;
  t.km=d1.km+d2.km;
  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;
  }
  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, m and cm 5 200 40
Enter km, m and cm 6 900 65
km 5 m 200 cm 40
km 6 m 900 cm 65
km 12 m 101 cm 5

Question:5
Class Distance (km, m, cm and mm)
C++ program to declare a class named distance with attributes as km, m, cm and mm. 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 km,m,cm,mm;
public:
     void read();
     void show();
     friend distance1 add(distance1 d1,distance1 d2);
};
void distance1::read()
{
  cout<<"Enter km, m, cm and mm ";
  cin>>km>>m>>cm>>mm;
}
void distance1::show()
{
  cout<<"km "<<km<<" m "<<m<<" cm "<<cm<<" mm "<<mm<<endl;
}
distance1 add(distance1 d1,distance1 d2)
{
  distance1 t;
  t.km=d1.km+d2.km;
  t.m=d1.m+d2.m;
  t.cm=d1.cm+d2.cm;
  t.mm=d1.mm+d2.mm;
  if(t.mm>=10)
  {
  	t.cm=t.cm+t.mm/10;
  	t.mm=t.mm%10;
  }
  if(t.cm>=100)
  {
    t.m=t.m+t.cm/100;
    t.cm=t.cm%100;
  }
  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, m, cm and mm 5 400 20 9
Enter km, m, cm and mm 8 500 90 5
km 5 m 400 cm 20 mm 9
km 8 m 500 cm 90 mm 5
km 13 m 901 cm 11 mm 4

Solution In Turboc3

/* this program is developed in turboc3 */
#include<iostream.h>
#include<conio.h>
class distance
{
	int km,m,cm,mm;
public:
	void read();
	void show();
	friend distance add(distance d1,distance d2);
};
void distance::read()
{
  cout<<"Enter km,m,cm,mm ";
  cin>>km>>m>>cm>>mm;
}
void distance::show()
{
  cout<<km<<" "<<m<<" "<<cm<<" "<<mm<<endl;
}
distance add(distance d1,distance d2)
{
  distance t;
  t.km=d1.km+d2.km;
  t.m=d1.m+d2.m;
  t.cm=d1.cm+d2.cm;
  t.mm=d1.mm+d2.mm;
  if(t.mm>=10)
  {
    t.cm=t.cm+t.mm/10;
    t.mm=t.mm%10;
  }
  if(t.cm>=100)
  {
    t.m=t.m+t.cm/100;
    t.cm=t.cm%100;
  }
  if(t.m>=1000)
  {
    t.km=t.km+t.m/1000;
    t.m=t.m%1000;
  }
  return(t);
}
void main()
{
  clrscr();
 distance e1,e2,e3;
 e1.read();e2.read();
 e1.show();e2.show();
 e3=add(e1,e2);
 e3.show();
 getch();
 }
/* Output */

Enter km, m, cm and mm 5 400 20 9
Enter km, m, cm and mm 8 500 90 5
km 5 m 400 cm 20 mm 9
km 8 m 500 cm 90 mm 5
km 13 m 901 cm 11 mm 4

Question:6
Class weight (kg and g)
C++ program to declare a class named weight with attributes as kg and g. Take input for two weights calculate and display their sum.
Sol:

#include<iostream>
#include<conio.h>
using namespace std;
class weight1
{
private:
     int kg,g;
public:
     void read();
     void show();
     friend weight1 add(weight1 d1,weight1 d2);
};
void weight1::read()
{
  cout<<"Enter kg and g ";
  cin>>kg>>g;
}
void weight1::show()
{
  cout<<"kg "<<kg<<" g "<<g<<endl;
}
weight1 add(weight1 d1,weight1 d2)
{
  weight1 t;
  t.kg=d1.kg+d2.kg;
  t.g=d1.g+d2.g;
  if(t.g>=1000)
  {
  	t.kg=t.kg+t.g/1000;
  	t.g=t.g%1000;
  }
  return(t);
}
int main()
{
  /* clrscr(); */
  weight1 e1,e2,e3;
  e1.read();e2.read();
  e1.show();e2.show();
  e3=add(e1,e2);
  e3.show();
  getch();
  return(0);
 } 
/* Output */

Enter kg and g 5 700
Enter kg and g 6 600
kg 5 g 700
kg 6 g 600
kg 12 g 300

Number Of Visitors

0270468