C++输出输出重载

/* class_opt_cin_out overload */

#include

using namespace std;

class Distance {

private:

int feets;

int inches;

public:

Distance() {

this->feets = 0;

this->inches = 0;

}

Distance(int f, int i) {

this->feets = f;

this->inches = i;

}

friend ostream &operator<< (ostream &output, const Distance &D) {

output << "F:" << D.feets << " I: " << D.inches;

return output;

}

friend istream &operator>>(istream &input, Distance &D) {

input >> D.feets >> D.inches;

return input;

}

};

int main() {

Distance D1(11, 10);

Distance D2(5,11);

Distance D3;

cin >> D3;

cout << "D1:" << D1 << endl;

cout << "D2:" << D2 << endl;

cout << "D3:" << D3 << endl;

return 0;

}

你可能感兴趣的:(C++输出输出重载)