0% found this document useful (0 votes)
10 views2 pages

Lab 2

Uploaded by

Ali Lak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Lab 2

Uploaded by

Ali Lak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#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;
}

// Ovload insertion optr


friend ostream& operator<<(ostream& output, const Distance& d) {
output << d.feet << " feet, " << d.inches << " inches";
return output;
}
};

int main() {
Distance d;

try {

cout << " enter the distance:" << endl;


cin >> d;

cout << "The entered distance is: " << d << endl;
} catch (const invalid_argument& e) {
cerr << "Error: " << e.what() << endl;
}

return 0;
}

You might also like