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();
}

    Related Post:
  1. Program to initialize array by even numbers starting from 2 and display sum of all elements

  2. Program to draw a spiral rotating clockwise at the center of the screen

  3. Program to sort that set of strings in ascending and descending order of their weights. The sorted output is to be displayed on the screen

  4. Program that changes an infix expression to a postfix expression according

  5. Program to estimate value of First Derivative of the function at the given points from the given data using Backward Difference Formula , Forward diff

  6. Program to show the implementation of Sutherland-Hodgeman Polygon Clipping Algorithm

  7. Program that are completely portable across different operating systems

  8. PROGRAM THAT PROVIDES AN EXAMPLE OF GETLINE FUNCTION

  9. Program to display bill receipt based on customer information and create telephone directory

  10. Program of displaying product inventory by converting one class to another class

  11. Program to read n integers and add them up and output the result

  12. Program to draw an Elliptical Arc using Trigonometric Method

  13. Program to illustrate the use of function overloading

  14. Program of Minimum Spaning Tree ( MST )

  15. Program to illustrate the use of the break statement

  16. Program to illustrate classes without using inline functions

  17. Program to illustrate the use of the continue statement

  18. Program to read a Non-Linear equation in one variable, then evaluate it using Newton-Raphson Method and display its kD accurate root

  19. Program to illustrate array of objects in classes

  20. Program to show the Simple implementation of Hashing

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