Answer to Question #233001 in C++ for SOM

Question #233001

Create two classes which stores distance in feet, inches and meter, centimeter format respectively. Write a function which compares distance in object of these classes and displays the larger one.


1
Expert's answer
2021-09-04T00:34:04-0400
#include <iostream>
using namespace std;




class FeetInches{
private:
	int feet;
	int inches;
public:
	FeetInches(int feet,int inches){
		this->feet=feet;
		this->inches=inches;
	}


	int getFeet(){
		return this->feet;
	}
	int getInches(){
		return this->inches;
	}


	void setFeet(int feet){
		this->feet=feet;
	}
	void setInches(int inches){
		this->inches=inches;
	}
};


class MeterCentimeter{
private:
	int meter;
	int centimeter;
public:
	MeterCentimeter(int meter,int centimeter){
		this->meter=meter;
		this->centimeter=centimeter;
	}


	int getMeter(){
		return this->meter;
	}
	int getCentimeter(){
		return this->centimeter;
	}


	void setMeter(int meter){
		this->meter=meter;
	}
	void setCentimeter(int centimeter){
		this->centimeter=centimeter;
	}
	int compare(FeetInches feetInches){
		float centimetersMeterCentimeter=this->meter*100+this->centimeter;
		float centimetersFeetInches=feetInches.getFeet()*30.48 + feetInches.getInches()* 2.54;
		if(centimetersMeterCentimeter>centimetersFeetInches){
			return 1;
		}
		if(centimetersMeterCentimeter<centimetersFeetInches){
			return -1;
		}
		return 0;
	}
};
 


int main(){


	int feet;
	int inches;
	int meter;
	int centimeter;


	cout<<"Enter feet: ";
	cin>>feet;
	cout<<"Enter inches: ";
	cin>>inches;
	cout<<"\nEnter meter: ";
	cin>>meter;
	cout<<"Enter centimeter: ";
	cin>>centimeter;


	FeetInches feetInches(feet,inches);
	MeterCentimeter meterCentimeter(meter,centimeter);


	if(meterCentimeter.compare(feetInches)==1){
		cout<<"\nMeterCentimeter is larger than FeetInches\n";
	}
	if(meterCentimeter.compare(feetInches)==-1){
		cout<<"\nFeetInches is larger than MeterCentimeter\n";
	}
	if(meterCentimeter.compare(feetInches)==-1){
		cout<<"\nFeetInches is equal to MeterCentimeter\n";
	}


	system("pause");
	return 0;
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS