Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Name: Nithish P Course: B.Sc.

Register Number: 20BCS0003 Branch: Computer Science

Subject: OBJECT ORIENTED PROGRAMMING

Subject Code: CSC2020 Slot: L37+L38

FAT LAB EXAM

___________________________________________________________________________
__________________________________________________
1.Create two classes named DM and DB to store the values of Distance. DM
stores distances in meters and centimeters and DB in feet and inches. 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. The object that stores the results may be a DM or DB
object depending on units in which the results are required. Your output
display should be either in “feet–inches” or “meter-centimeter” depending
on the object or display.Single line text.

AIM:
In this program we use two classes DM and DB for getting distance in
meter and feet and add two different units using friend function and store results
in DM or DB to display the addition of two distance in any one unit.

DESCRIPTION:
First create two classes DM and DB and each class use function to get
values and display and create friend function for adding two values and by this
friend function we add two units and display the result in one unit.

CODE:

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

using namespace std;

class DB;

class DM
{
float meter,centi;
public:

void getdata()
{
cout<<"\nEnter the distance in(meter-centimeter):";
cin>>meter>>centi;
}

void display()
{
cout<<"\nThe distance is:";
cout<<meter<<" meters and "<<centi<<" centimeter";
}

friend void add(DM &,DB &);


};

class DB
{
float inch,feet;
public:

void getdata()
{
cout<<"\nEnter the distance in(feet-inch):";
cin>>feet>>inch;
}
void display()
{
cout<<"\nThe distance is:";
cout<<feet<<" feet and "<<inch<<" inch";
}

friend void add(DM &,DB &);


};

void add(DM &a,DB &b)


{
int ch;
cout<<"\nPress 1 for meter-centimeter:";
cout<<"\nPress 2 for feet-inch:";
cout<<"\nEnter your choice:";
cin>>ch;
if(ch==1)
{
DM d;
int c=(a.meter*100+a.centi+b.feet*30.48+b.inch*2.54);
if(c>=100)
{
d.meter=c/100;
d.centi=c%100;
}

else
{
d.meter=0;
d.centi=c;
}
d.display();
}

else
{
DB d;
int i=(a.meter*39.37+a.centi*.3937008+b.feet*12+b.inch);
if(i>=12)
{
d.feet=i/12;
d.inch=i%12;
}

else
{
d.feet=0;
d.inch=i;
}
d.display();
}
}

int main()
{
system("color 02");
cout<<"\n NAME : P.NITHISH| REG. NO. : 20BCS0003\n"<<endl;

DM a;
DB b;

a.getdata();
b.getdata();

add(a,b);

getch();
}

OUTPUT:

This output is displayed in unit meter and centimetre.


This output is displayed in unit feet and inches.
CONCLUSION:
This program is useful for addition of two different units and get the
value in one unit

You might also like