Implement a class Distance with 2 data members feet, inches.
Here feet is integer and inches are float, include constructors to initialize
data members and member functions as following:
A. Method a.compareTo(b) which compares 2 distances and then
retuns -1 if d1>d2, a zero when equal or +1 for correct order.
B. Method to add two distances, it should return a distance
object.
C. Method to subtract two distances, it should return a
distance object.
D. Write a mian program that inputs 10 distances and
arranges them in ascending order.
SOLUTION:
#include<iostream.h>
#include<conio.h>
class distance
{
int feet;
float inch;
public:
distance()
{
cout<<"Constructor for initialization of data members and funtions.";
}
void comparison();
void addition();
void subtraction();
};
void distance::comparison()
{
float d1,d2;
cout<<"\nEnter the 2 distances for comparision:";
cin>>d1>>d2;
if (d1>d2)
cout<<"-1";
else if (d1==d2)
cout<<"0";
else
cout<<"1";
}
void distance::addition()
{
float d1,d2;
cout<<"\nEnter the 2 distances for addition:";
cin>>d1>>d2;
inch=d1+d2;
cout<<"\nThe total distance object is:"<<inch;
}
void distance::subtraction()
{
float d1,d2;
cout<<"\nEnter the 2 distances for subtraction:";
cin>>d1>>d2;
inch=d1-d2;
cout<<"\nThe remaining distance object is:"<<inch;
}
void main()
{
int d[10],i,j,temp, ch;
clrscr();
distance d1;
cout<<"\n\nEnter the 10 distances:";
for (i=0;i<10;i++)
{
cin>>d[i];
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if (d[i]>d[j])
{
temp=d[j];
d[j]=d[i];
d[i]=temp;
}
}
}
cout<<"The 10 distances sorted in ascending order are:\n";
for(i=0;i<10;i++)
cout<<"\n"<<d[i];
cout<<"\n\nPress\n1.For Comparison of 2 distances\n2.For Addition of 2 distances\n3.For Subtraction of 2 distances";
cout<<"\nEnter you choice:";
cin>>ch;
switch (ch)
{
case 1:
d1.comparison();
break;
case 2:
d1.addition();
break;
case 3:
d1.subtraction();
break;
default:
cout<<"\nYou didnot the correct option:";
break;
}
getch();
}
OUTPUT:
No comments:
Post a Comment