C++ (Qt)class Rational {public: friend std::ofstream & operator << ( std::ofstream & strm, const Rational & r ); ...}
C++ (Qt)class Rational {public: template <class stream> friend stream & operator << ( stream & strm, const Rational & r ); ...}
#include <QCoreApplication>#include <QTextStream>#include "rational.h"QTextStream cin(stdin);QTextStream cout(stdout);int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); // Перемножаем дроби и выводим результат на экран Rational a(2, 3), b(1, 2), c(3, 4), product; product = a * b * c; cout << "product = " << product << endl; cout.flush(); return app.exec();}
class Bar{public: Bar(int i = 0) : _i(i) { } ~Bar() { } int _i;};Bar operator+(const Bar& b1, const Bar& b2){ Bar result; result = b1; result._i += b2._i; return result;}int main(int argc, char *argv[]){ Bar b1(1); Bar b2(2); Bar r = b1 + b2; return 0;}
C++ (Qt)#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QString &);Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QString &);#endif
friend QTextStream& operator<<(QTextStream& stream, const Rational& r);
C++ (Qt)class Bar{public: Bar(int ii = 0) : i(ii) { } int i;}; QTextStream& operator<<(QTextStream& stream, const Bar& bar){ stream << QString::number(bar.i); return stream;}int main(int argc, char *argv[]){ Bar b1(1); QString s; QTextStream out(&s); out << b1; return 0;}
C++ (Qt)#include <QCoreApplication>#include <QTextStream>#include "rational.h" QTextStream cin(stdin);QTextStream cout(stdout); int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); Rational a(2, 3); cout << "a = " << a << endl; cout.flush(); return app.exec();}
C++ (Qt)#ifndef RATIONAL_H#define RATIONAL_H #include <QTextStream> namespace RationalStuff {class Rational;} class Rational{public: Rational(int numerator = 0, int denominator = 1); int numerator() const; void setNumerator(const int numerator); int denominator() const; void setDenominator(const int denominator); // friend const Rational operator*(const Rational& lhs, const Rational& rhs);// friend const Rational operator+(const Rational& lhs, const Rational& rhs);// friend const Rational operator-(const Rational& lhs, const Rational& rhs); friend QTextStream& operator<<(QTextStream& stream, const Rational& r); private: int mNumerator; int mDenominator;}; #endif // RATIONAL_H
C++ (Qt)#include "rational.h" Rational::Rational(int numerator, int denominator) : mNumerator(numerator), mDenominator(denominator){} int Rational::numerator() const{ return mNumerator;} void Rational::setNumerator(const int numerator){ mNumerator = numerator;} int Rational::denominator() const{ return mDenominator;} void Rational::setDenominator(const int denominator){ mDenominator = denominator;} QTextStream &Rational::operator<<(QTextStream &stream, const Rational &r){ stream << r.mNumerator << "/" << r.mDenominator; return stream;}
C++ (Qt)QTextStream &operator<<(QTextStream &stream, const Rational &r){ stream << r.mNumerator << "/" << r.mDenominator; return stream;}