Programming in C++ In this assignment you will create a Measurement class. A Mea
ID: 3703471 • Letter: P
Question
Programming in C++
In this assignment you will create a Measurement class. A Measurement object will store an integer value for the size in feet and an integer value for the size in inches. A Measurement object should store the value of 30" (inches) as 2 feet, 6 inches (2' 6"). When a Measurement object is created or modified the values for feet and inches may need to be adjusted. For instance, given A Measurement object storing feet = 5, inches = 11 if you add 7 feet 9 inches to it you would get 12 feet, 20 inches. This would need to be simplified to get the inches to be less than 12. 20 / 12 --> 1, 20 % 12 = 8 so the new values would be 13 feet, 8 inches.
Overload operators for:
addition
subtraction
multiplication by an integer [ provide for an expession like m1 * 3 where m1 is a Measurement object ]
assignment
relational operators ( <, <=, >, >=, ==, != )
stream operators ( stream extraction >> and stream insertion << )
Provide appropriate get and set functions. Provide a function to display the "state" of a Measurement object. For example: [5 feet, 7 inches]
Your class declaration (aka specification) should be stored in a header file. Your class definition (aka implementation) should be stored in a C++ source file.
Create a driver program that will create Measurement objects and execute the different operations provided by the Measurement class (object creation, input, output, assigment, comparison, addition, subtraction, multiplication, and display the state of the object). Only integer values should be used for the feet and inches.
Note: the inches should never exceed 11, that is 5 feet and 18 inches should really be stored as 6 feet and 6 inches! Make sure you simplify as part of your addition, subtraction, and multiplication by an integer member function. You can look at the Date class in chapter 10 to see an example of how to "simplify" in a Date object.
Explanation / Answer
//Measurement.h
#ifndef _MEASUREMENT
#define _MEASUREMENT
#include<iostream>
using namespace std;
class Measuremant
{
public:
int feet;
float inches;
Measuremant();
Measuremant(int, float);
void getM();
void showM();
Measuremant operator+(Measuremant)const;
Measuremant operator*(int)const;
Measuremant operator-(Measuremant)const;
bool operator<(Measuremant)const;
bool operator<=(Measuremant)const;
bool operator>(Measuremant)const;
bool operator>=(Measuremant)const;
bool operator==(Measuremant)const;
bool operator!=(Measuremant)const;
friend istream & operator>>(istream &is, Measuremant &M);
friend ostream & operator<<(ostream &os, const Measuremant &M);
};
#endif
//Measurement.cpp
#include"Measurement.h"
#include<iostream>
using namespace std;
Measuremant::Measuremant()
{
feet=0;
inches=0.0;
}
Measuremant::Measuremant(int fe, float in)
{
feet =fe;
inches=in;
}
void Measuremant::getM()
{
cout<<"enter feet-->";
cin>>feet;
cout<<"Enter inches-->";
cin>>inches;
}
void Measuremant::showM()
{
cout<<feet<<"'-"<<inches<<'"';
}
Measuremant Measuremant::operator+(Measuremant M2)const
{
int f = feet +M2.feet;
float i = inches+M2.inches;
if(i>=12.0)
{
i-=12.0;
f++;
}
return Measuremant(f,i);
}
Measuremant Measuremant::operator-(Measuremant M2)const
{
int f = feet -M2.feet;
float i = inches-M2.inches;
if(i>=12.0)
{
i-=12.0;
f++;
}
return Measuremant(f,i);
}
bool Measuremant::operator<(Measuremant M)const
{
float bf1=feet+inches/12;
float bf2=M.feet+M.inches/12;
return(bf1<bf2)?true:false;
}
bool Measuremant::operator<=(Measuremant M)const
{
float bf1=feet+inches/12;
float bf2=M.feet+M.inches/12;
return(bf1<=bf2)?true:false;
}
bool Measuremant::operator>(Measuremant M)const
{
float bf1=feet+inches/12;
float bf2=M.feet+M.inches/12;
return(bf1>bf2)?true:false;
}
Measuremant Measuremant:: operator*(int n)const
{
int f = n*feet ;
float i = n*inches;
if(i>=12.0)
{
i-=12.0;
f++;
}
return Measuremant(f,i);
}
bool Measuremant::operator>=(Measuremant M)const
{
float bf1=feet+inches/12;
float bf2=M.feet+M.inches/12;
return(bf1>=bf2)?true:false;
}
bool Measuremant::operator==(Measuremant M)const
{
float bf1=feet+inches/12;
float bf2=M.feet+M.inches/12;
return(bf1==bf2)?true:false;
}
bool Measuremant::operator!=(Measuremant M)const
{
float bf1=feet+inches/12;
float bf2=M.feet+M.inches/12;
return(bf1!=bf2)?true:false;
}
istream& operator>>(istream& is, Measuremant& M)
{
cout<<"Enter feet";
is>>M.feet;
cout<<"Enter inches";
is>>M.inches;
return is;
}
ostream& operator<<(ostream& os, const Measuremant& M)
{
cout<<M.feet<<"'-"<<M.inches<<'"'<<endl;
return os;
}
//main.cpp
#include<iostream>
#include"Measurement.h"
using namespace std;
void main()
{
Measuremant m1(5,4.5),m2,m3;
cin>>m2;
m3=m1+m2;
cout<<m3;
m3=m1-m2;
cout<<m3;
m1=m1*3;
cout<<m1;
if(m1==m2)
{
cout<<"true";
}
//m3.showM();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.