0% found this document useful (0 votes)
5 views

12.Friend Function

The document presents a C++ program that defines two classes, 'dm' for distance in meters and centimeters, and 'db' for distance in feet and inches. It includes member functions for data input and output, as well as a friend function 'add' that combines distances from both classes. The main function demonstrates the usage of these classes by taking user input, performing the addition, and displaying the result.

Uploaded by

ponni.world009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

12.Friend Function

The document presents a C++ program that defines two classes, 'dm' for distance in meters and centimeters, and 'db' for distance in feet and inches. It includes member functions for data input and output, as well as a friend function 'add' that combines distances from both classes. The main function demonstrates the usage of these classes by taking user input, performing the addition, and displaying the result.

Uploaded by

ponni.world009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

12.

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

float inches;

int feet;

public:

void getdata(void);

void display(void);

friend dm add(dm,db);

};

void dm::getdata(void)

clrscr();

cout<<"DM getdata function ";


cout<<"\n \t Enter values for meters :";

cin>>mt;

cout<<"\n\t Enter the values for centimeters:";

cin>>cm;

void dm::display(void)

cout<<"\n The values of distance in meters is:"<<mt;

cout<<"\n The values of distance in centimeters is :"<<cm;

void db::getdata(void)

cout<<"\n DB getdata function :";

cout<<"\n \t Enter values for feet:";

cin>>feet;

cout<<"\n \t Enter the values for inches: ";

cin>>inches;

void db::display(void)

cout<<"\n \tThe values of distance in feet is :"<<feet;

cout<<"\n \tThe values of distance in inches :"<<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();

cout<<"\n Ater conversion & addition performed : ";

dm extra;

extra=add(a,b);

extra.display();

getch();

You might also like