Create a class called DISTANCE with data members as feet (int) and inches (float), use parameterised constructor to get the input values of the data members. Write a function to display the output. Create another class called DIST_KM with a data member kilometre (float). Use a parameterized constructor to assign input and a function to display it. In the main function, write a menu-driven program with 1. Convert feet and inches into kilometres 2.Check whether both the distances are equal or not. For option 1, write the conversion function in the source class. For option 2, create a separate object to assign the kilometres and check the equality of distances by overloading the operator (==). [Note: 1 feet=0.00305km]
#include <iostream>
#include <iomanip>
using namespace std;
class Distance {
private:
//data members as feet (int) and inches (float)
int feet;
float inches;
public:
//use parameterised constructor to get the input values of the data members.
Distance(int f,float inch){
this->feet=f;
this->inches=inch;
if(inch>=12){
int feets=int(inch/12);
this->inches-=feets*12;
this->feet+=feets;
}
}
// a function to display the output.
void display(){
cout<<feet<<"'-"<<fixed<<setprecision(2)<<inches<<"\"\n";
}
int getFeet(){
return feet;
}
float getInches(){
return inches;
}
};
// class called DIST_KM with a data member kilometre (float).
class DIST_KM {
private:
float kilometre;
public:
//Use a parameterized constructor to assign input and a function to display it.
DIST_KM(float km){
this->kilometre=km;
}
void convert(Distance distance){
this->kilometre=distance.getFeet()*0.0003048+distance.getInches()*0.0000254;
distance.display();
cout<<"Kilometre = "<<fixed<<setprecision(10)<<kilometre<<"\n\n";
}
bool operator==(DIST_KM otherKM){
return (kilometre==otherKM.kilometre);
}
};
int main () {
//In the main function, write a menu-driven program with
int ch=-1;
while(ch!=3){
cout<<"1. Convert feet and inches into kilometres\n";
cout<<"2. Check whether both the distances are equal or not\n";
cout<<"3. Exit\n";
cout<<"Your choice: ";
cin>>ch;
//1. Convert feet and inches into kilometres
//For option 1, write the conversion function in the source class.
if(ch==1){
int feet;
float inches;
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
Distance distance(feet,inches);
DIST_KM DISTKM(0);
DISTKM.convert(distance);
}else if(ch==2){
//2.Check whether both the distances are equal or not.
//For option 2, create a separate object to assign the kilometres and check the equality of distances by overloading the operator (==).
int feet1;
float inches1;
cout<<"Enter feet for distance 1: ";
cin>>feet1;
cout<<"Enter inches for distance 2: ";
cin>>inches1;
Distance distance1(feet1,inches1);
DIST_KM DISTKM1(0);
DISTKM1.convert(distance1);
int feet2;
float inches2;
cout<<"Enter feet for distance 2: ";
cin>>feet2;
cout<<"Enter inches for distance 2: ";
cin>>inches2;
Distance distance2(feet2,inches2);
DIST_KM DISTKM2(0);
DISTKM2.convert(distance2);
if(DISTKM1==DISTKM2){
cout<<"\nBoth the distances are equal.\n\n";
}else{
cout<<"\nBoth the distances are NOT equal.\n\n";
}
}else if(ch==3){
}else{
cout<<"\nWrong menu item.\n\n";
}
}
system("pause");
return 0;
}
Comments
Leave a comment