Please help implement a '+' operator that takes two intervals and takes the smallest interval and then the largest for the end. Example: Interval(4, 6) + Interval(5, 8) equals to Interval(4, 8), and Interval(4, 6) + Interval(7, 8) equals to Interval(4, 8). !!!!!!!This one below here is the .cpp file i need to implement, only file i can change. I worked on it but i keep getting an error which is shown in the image #include #include "interval.h" using namespace std; Interval::Interval(int lower_bound, int upper_bound) { low = lower_bound; high = upper_bound; } void Interval::print(ostream& out) const { out << "(" << low << ", " << high << ")" << endl; } Interval Interval::operator+(const Interval &other) const { int temp_low, temp_high; if(this->low <= other.low) { temp_low = this->low; } else { temp_low=other.low; } if(this->high >= other.high) { temp_high = this->high; } else { temp_high=other.high; } return Interval(temp_low,temp_high);
Please help implement a '+' operator that takes two intervals and takes the smallest interval and then the largest for the end.
Example: Interval(4, 6) + Interval(5, 8) equals to Interval(4, 8), and Interval(4, 6) + Interval(7, 8) equals to Interval(4, 8).
!!!!!!!This one below here is the .cpp file i need to implement, only file i can change. I worked on it but i keep getting an error which is shown in the image
#include <iostream>
#include "interval.h"
using namespace std;
Interval::Interval(int lower_bound, int upper_bound)
{
low = lower_bound;
high = upper_bound;
}
void Interval::print(ostream& out) const
{
out << "(" << low << ", " << high << ")" << endl;
}
Interval Interval::operator+(const Interval &other) const
{
int temp_low, temp_high;
if(this->low <= other.low) { temp_low = this->low; }
else { temp_low=other.low; }
if(this->high >= other.high) { temp_high = this->high; }
else { temp_high=other.high; }
return Interval(temp_low,temp_high);
}
//this is the tester file i am given, cannot change ANYTHING
#include <iostream>
#include "interval.h"
using namespace std;
int main()
{
Interval result = Interval(3, 4) + Interval(7, 8);
result.print(cout);
cout << "Expected: (3, 8)" << endl;
result = Interval(7, 9) + Interval(4, 8);
result.print(cout);
cout << "Expected: (4, 9)" << endl;
result = Interval(4, 9) + Interval(7, 8);
result.print(cout);
cout << "Expected: (4, 9)" << endl;
result = Interval(4, 5) + Interval(5, 8);
result.print(cout);
cout << "Expected: (4, 8)" << endl;
return 0;
}
//this is the .h file i am given, i cannot change ANYTHING about it
#ifndef INTERVAL_H
#define INTERVAL_H
#include <iostream>
class Interval
{
public:
Interval(int lower_bound, int upper_bound);
void print(std::ostream& out) const;
Interval operator+(const Interval& other) const;
private:
int low;
int high;
};
#endif
Step by step
Solved in 3 steps with 1 images