Program to create a class Distance


Write a program to create a class Distance with
 Feet and inches as data members
 Member function to input distance
 Member function to output distance
 Member function to add two distance objects.

PROGRAM

#include<math.h>
#include<iostream>
using namespace std;
class Distance
{
float feet,inch;
public: Distance()
{
feet=0.0;
inch=0.0;
}
void read_dist();
void disp_dist();
void add(Distance , Distance);
};
void Distance::read_dist()
{
cout<<“Enter distance(feet and inches):”;

cin>>feet>>inch;
}
void Distance::disp_dist()
{
cout<<“Distance Feet:”<<feet<<“, Inches:”<<inch<<“\n”;
}
void Distance::add(Distance x,Distance y)
{
inch=x.inch+y.inch;
feet=x.feet+y.feet;
if(inch>=12.0)
{
feet=x.feet+y.feet+(inch/12.0);
inch=(int)inch%12;
}
}
main()
{
Distance d1,d2,d3;
cout<<“Enter first measure:\n”;
d1.read_dist();
cout<<“Enter second measure:\n”;
d2.read_dist();
d3.add(d1,d2);
d3.disp_dist();
}

 

OUTPUT
Enter first measure:
Enter distance(feet and inches): 25 36
Enter second measure:
Enter distance(feet and inches): 25 14
Distance Feet:54, Inches:20

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s