FRIEND FUNCTION PROGRAM

 

CREATE TWO CLASSES dm AND db WHICH STORE THE VALUE OF DISTANCE. WRITE A PROGRAM THAT CAN READ VALUES FOR THE CLASS OBJECTS AND ADD ONE OBJECT OF dm WITH ANOTHER OBJECT OF db.

USE A FRIEND FUNCTION TO CARRY OUT THE ADDITION OPERATION OBJECT THAT STORES THE RESULT MAY BE IN dm OBJECT OR db OBJECT DEPENDING ON THE UNITS ON WHICH THE RESULTS ARE REQUIRED.

THE DISPLAY SHOULD BE IN THE FORMAT OF FEET AND INCHES OR METERS AND CENTIMETERS DEPENDING ON THE OBJECT ON DISPLAY.

C++ PROGRAM:

#include <iostream>
#include <stdlib.h>
using namespace std;
//   '& # 0 9 1 ;' is third left bracket
class DB;
class DM
{
	float c1, c2, inc1;
	int m, cm, ft1;
	public:
	void m_cm(){
		cout<<"M= ";  cin>>m;   cout<<endl<<"CM= ";    cin>>cm;    cout<<endl;
	}

	void convart1()
	{
		m=m*100;	c1=m+cm; 	c2=c1*0.4;
		ft1=c2/12; 	inc1= c2-(ft1*12);	
	}
	friend void add1(DM,DB);
	friend void add2(DM,DB);
};

class DB{
	float d1,d2,cm1; 	int ft,inc,m1;
	public:
	void ft_inc()
	{
		cout<<"FT= "; cin>>ft; 	cout<<endl<<"Inc= "; cin>>inc; cout<<endl;
		}
		
	void convart2()
	{
		ft=ft*12;	d1=ft+inc;	d2=d1*2.5;	m1=d2/100;	cm1=d2-(m1*100);
	}
	friend void add1(DM,DB);
	friend void add2(DM,DB);
};

void add1(DM M1, DB B1)
{
	int a,b;	a=M1.m + B1.m1;
	b=M1.cm + B1.cm1;
	if(b >= 100) { a=a+1;	b=b-100; }
	cout<<"Result in m-cm= "<<a<<" m "<<b<<" cm";
	
}

void add2(DM M2, DB B2)
{
	int c,d;
	c=M2.ft1 + B2.ft;
	d=M2.inc1 + B2.inc;
	if(d>=12)
	{  c=c+1; 	d=d-12;	}
	cout<<"Result in ft-inc= "<<c<<" ft "<<d<<" inc";
}

int main()
{
DM M; DB B; char x;

do
{

cout<<"1. Want result in m-cm"<<endl;
cout<<"2. Want result in ft-inc"<<endl;
cout<<"Enter your choice: "<<endl;
int n; 	cin>>n;
switch(n){
	case 1:
	cout<<"Enter the 1st value in m-cm"<<endl;
	M.m_cm();
	cout<<"Enter the 2nd value in ft-inc"<<endl;
	B.ft_inc(); 	B.convart2();
	add1(M,B);	break;
	
	case 2:
	cout<<"Enter the 1st value in m-cm"<<endl;
	M.m_cm();	M.convart1();
	cout<<"Enter the 2nd value in ft-inc"<<endl;
	B.ft_inc(); 	add2(M,B);	break;
	
	default:
	cout<<endl<<"Invalid Input";	
}
cout<<endl<<"Do you want to continue? (Y/N): ";
cin>>x;
}
while(x=='Y' || x== 'y');
	return 0;
}

PYTHON PROGRAM:

# PYTHON PROGRAM WITHOUT FRIEND FUNCTION

class DM:
	def __init__(self):
		self.c1=self.c2=self.inc1=0.0
		self.m=self.cm=self.ft1=0

	def m_cm(self):
		self.m=int(input("M= "))
		self.cm=int(input("CM= "))

	def convart1(self):
		self.m=self.m*100
		self.c1=self.m+self.cm
		self.c2=self.c1*0.4
		self.ft1=self.c2/12
		self.inc1= self.c2-(self.ft1*12)

class DB:
	def __init__(self):
		self.d1=self.d2=self.cm1=0.0
		self.ft=self.inc=self.m1=0

	def ft_inc(self):
		self.ft=int(input("FT= "))
		self.inc=int(input("INC= "))

	def convart2(self):
		self.ft=self.ft*12
		self.d1=self.ft+self.inc
		self.d2=self.d1*2.5
		self.m1=self.d2/100
		self.cm1=self.d2-(self.m1*100)
#ARE ADD1 AND ADD2 CALLED AS PYTHON FRIEND FUNCTION?
#COMMENT
def add1(M1,B1):
	a=M1.m + B1.m1
	b=M1.cm + B1.cm1
	if(b>=100):
		a+=1
		b-=100
	print("Result in m-cm= ",a," m ",b," cm")

def add2(M2,B2):
	c=M2.ft1 + B2.ft;
	d=M2.inc1 + B2.inc
	if(d>=12):
		c+=1
		d-=12
	print("Result in ft-inc= ",c," ft ", d," inc")


#MAIN
m=DM()
b = DB()
x='Y'

while('y' in x or 'Y' in x):
	print("1. Want Result In m-cm")
	print("2. Want Result In ft-inc")
	n=int(input("Enter your choice: "))

	if n==1:
		print("Enter the 1st value in m-cm: ")
		m.m_cm()
		print("Enter the 2nd value in ft-inc: ")
		b.ft_inc()
		b.convart2()
		add1(m,b)
	elif n==2:
		print("Enter the 1st value in m-cm: ")
		m.m_cm()
		m.convart1()
		print("Enter the 2nd value in ft-inc: ")
		b.ft_inc()
		add2(m,b)
	else:
		print("Invalid Input")

	x=input("Do you want to continue? (Y/N): ")

Note:

1 foot = 12 inch

1 m = 100 cm

1 foot = 30cm

1 Inch = 2.5cm

1 Inch = 0.08foot

OUTPUT:

1. Want result in m-cm
2. Want result in ft-inc
Enter your choice: 
1
Enter the 1st value in m-cm
M= 1
CM= 0

Enter the 2nd value in ft-inc
FT= 1
Inc= 0

Result in m-cm= 1 m 30 cm
Do you want to continue? (Y/N): y
1. Want result in m-cm
2. Want result in ft-inc
Enter your choice: 
2
Enter the 1st value in m-cm
M= 1
CM= 0

Enter the 2nd value in ft-inc
FT= 1
Inc= 0

Result in ft-inc= 4 ft 4 inc
Do you want to continue? (Y/N): n

This output is checked and verified. The time complexity of this program is O(n).

Comments