Lab 2
Lab 2
#include <stdexcept>
using namespace std;
class Distance {
private:
int feet;
float inches;
public:
// DConstr
Distance() : feet(0), inches(0.0) {}
// PC
Distance(int f, float in) {
if (f < 0 || in < 0) {
throw invalid_argument("Distance values cannot be negative.");
}
feet = f;
inches = in;
}
// OL extraxt optr
friend istream& operator>>(istream& input, Distance& d) {
cout << "Enter feet: ";
input >> d.feet;
if (d.feet < 0) {
throw invalid_argument("Feet cannot be negative.");
}
cout << "Enter inches: ";
input >> d.inches;
if (d.inches < 0) {
throw invalid_argument("Inches cannot be negative.");
}
return input;
}
int main() {
Distance d;
try {
cout << "The entered distance is: " << d << endl;
} catch (const invalid_argument& e) {
cerr << "Error: " << e.what() << endl;
}
return 0;
}