Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » BeginnersRSS Feeds

Program to illustrate friend function

Posted By: Easy Tutor     Category: C++ Programming     Views: 30796

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.

Code for Program to illustrate friend function in C++ Programming

/*www.DailyFreeCode.comDownload Projects, Sourcecodes, Tips and Tricks, Interview FAQs, Hotlinks and more....Logon to www.DailyFreeCode.com*///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();
}
  
Share: 


Didn't find what you were looking for? Find more on Program to illustrate friend function Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to illustrate friend function is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Dyan Hadi from United States Comment on: Oct 08
why wee need to use friend function???

View All Comments