C++ Programming Blog

 
 
 
//create two classes DM and DB which stores the value of distances. DM stores distances in metres
//and centimetres and DB in feet and inches. Write a program that can read values for the class objects
//and add one object DM with another object of DB.
//use friend function.

#include <iostream.h>
#include <conio.h>

class db;

class dm
{
float mt;
int cm;
public:
void getdata(void);
void display(void);
friend dm add(dm,db);
};

class db
{
int feet;
float inches;
public:
void getdata(void);
void display(void);
friend dm add(dm,db);
};

void dm :: getdata(void)
{
clrscr();
cout<<\"\\t\\tDM GETDATA FUNCTION\\n\\n\";
cout<<\"\\n\\nEnter Values for metres :-\";
cin>>mt;
cout<<\"Enter Values for centimetres:-\";
cin>>cm;
}
void dm :: display(void)
{
cout<<\"\\n\\nThe value of distance in metres is \"<<mt;
cout<<\"\\nThe value of distance in Centimetres is \"<<cm;
}


void db :: getdata(void)
{
clrscr();
cout<<\"\\t\\tDB GETDATA FUNCTION\\n\\n\";
cout<<\"\\n\\nEnter Values for feet :-\";
cin>>feet;
cout<<\"Enter Values for inches :-\";
cin>>inches;
}
void db :: display(void)
{
cout<<\"\\n\\nThe value of distance in feet is \"<<feet;
cout<<\"\\nThe value of distance in inches is \"<<inches;
}

dm add(dm a,db b)
{
dm temp;
temp.cm=a.cm+(b.feet*30)+((b.inches*30)/12.0);
temp.mt=a.mt+(temp.cm % 100);
temp.cm=temp.cm-((temp.cm % 100)*100);
return(temp);
}

void main()
{
dm a;
a.getdata();
db b;
b.getdata();

clrscr();
cout<<\"\\n\\t\\tAFTER CONVERSION AND THEIR ADDITION IS PERFORMED\\n\";

//extra variable of type dm to display result of adding two different types of distance
dm extra;
extra=add(a,b);
extra.display();

getch();
}

 
 
Didn't find what you were looking for? Find more on Program to illustrate friend function