C++ (Qt)friend QTextStream& operator<<(QTextStream& stream, const Rational& r);
C++ (Qt)#ifdef QT_VERSION...#endif
C++ (Qt)#include <QCoreApplication>#include <QTextStream>#include "shape.h"#include "circle.h"#include "rectangle.h" QTextStream cin(stdin);QTextStream cout(stdout); void printShapeInfo(Shape *ps) { cout << "p = " << ps->perimeter() << endl; cout << "s = " << ps->area() << endl; cout.flush();} int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Shape *pc = new Circle(); printShapeInfo(pc); pc->calcPerimeter(2.5); return a.exec();}
C++ (Qt)#ifndef SHAPE_H#define SHAPE_H class Shape{public: Shape(); virtual ~Shape(); virtual void calcPerimeter() = 0; virtual void calcArea() = 0; double perimeter(); double area(); protected: double mPerimeter; double mArea;}; #endif // SHAPE_H
C++ (Qt)#include "shape.h" Shape::Shape() : mPerimeter(0.0), mArea(0.0){} Shape::~Shape(){} double Shape::perimeter(){ return mPerimeter;} double Shape::area(){ return mArea;}
C++ (Qt)#ifndef CIRCLE_H#define CIRCLE_H #include "shape.h" class Circle : public Shape{public: Circle(); virtual double calcPerimeter(double radius); virtual double calcArea(double radius);}; #endif // CIRCLE_H
C++ (Qt)#include "circle.h"#include <cmath> Circle::Circle(){} double Circle::calcPerimeter(double radius){ mPerimeter = 2.0 * M_PI * radius; return mPerimeter;} double Circle::calcArea(double radius){ mArea = M_PI * radius * radius; return mArea;}
C++ (Qt)#ifndef RECTANGLE_H#define RECTANGLE_H #include "shape.h" class Rectangle : public Shape{public: Rectangle(); virtual double calcPerimeter(double a, double b); virtual double calcArea(double a, double b);}; #endif // RECTANGLE_H
C++ (Qt)#include "rectangle.h" Rectangle::Rectangle(){} double Rectangle::calcPerimeter(double a, double b){ mPerimeter = 2.0 * (a + b); return mPerimeter;} double Rectangle::calcArea(double a, double b){ mArea = a * b; return mArea;}
C++ (Qt) template <typename stream_type> friend stream_type& operator<<(stream_type& stream, const Rational& r);
C++ (Qt)template <typename stream_type>stream_type &operator<<(stream_type &stream, const Rational &r){ stream << r.mNumerator << "/" << r.mDenominator; return stream;}
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 SHAPE_H#define SHAPE_H class Shape{ // ... virtual void calcPerimeter() = 0; virtual void calcArea() = 0; // ...};
C++ (Qt)#ifndef CIRCLE_H#define CIRCLE_H #include "shape.h" class Circle : public Shape{public: Circle(); double calcPerimeter(double radius); double calcArea(double radius);}; #endif // CIRCLE_H
C++ (Qt)#ifndef RECTANGLE_H#define RECTANGLE_H #include "shape.h" class Rectangle : public Shape{public: Rectangle(); double calcPerimeter(double a, double b); double calcArea(double a, double b);}; #endif // RECTANGLE_H
C++ (Qt)#ifndef CIRCLE_H#define CIRCLE_H #include "shape.h" class Circle : public Shape{public: Circle(double radius); double calcPerimeter(); double calcArea();}; #endif // CIRCLE_H
C++ (Qt)#ifndef RECTANGLE_H#define RECTANGLE_H #include "shape.h" class Rectangle : public Shape{public: Rectangle(double a, double b); double calcPerimeter(); double calcArea();}; #endif // RECTANGLE_H
C++ (Qt)class Rectangle : public Shape{public: Rectangle(); double calcPerimeter(double a, double b); double calcArea(double a, double b);};
C++ (Qt)public: Rectangle(); static double calcPerimeter(double a, double b); static double calcArea(double a, double b);
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); // 2/3 cout << "a = " << a << endl; cout.flush(); return app.exec();}
C++ (Qt)#ifndef RATIONAL_H#define RATIONAL_H namespace RationalStuff {class Rational;} class Rational{public: Rational(int numerator = 0, int denominator = 1); int numerator() const; void setNumerator(int numerator); int denominator() const; void setDenominator(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); template <typename stream_type> friend stream_type& operator<<(stream_type& stream, const Rational& r); private: int mNumerator; int mDenominator;}; template <typename stream_type>stream_type &operator<<(stream_type &stream, const Rational &r){ stream << r.mNumerator << "/" << r.mDenominator; return stream;} #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(int numerator){ mNumerator = numerator;} int Rational::denominator() const{ return mDenominator;} void Rational::setDenominator(int denominator){ mDenominator = denominator;} const Rational operator*(const Rational &lhs, const Rational &rhs){ return Rational(lhs.mNumerator * rhs.mNumerator, lhs.mDenominator * rhs.mDenominator);} const Rational operator+(const Rational &lhs, const Rational &rhs){ Rational result; // Общий знаменатель int commonDenominator = lhs.denominator() * rhs.denominator(); // Числитель int a = lhs.numerator() * rhs.denominator(); int b = rhs.numerator() * lhs.denominator(); result.setNumerator(a + b); // Знаменатель result.setDenominator(commonDenominator); return result;} const Rational operator-(const Rational &lhs, const Rational &rhs){ Rational result; // Общий знаменатель int commonDenominator = lhs.denominator() * rhs.denominator(); // Числитель int a = lhs.numerator() * rhs.denominator(); int b = rhs.numerator() * lhs.denominator(); result.setNumerator(a - b); // Знаменатель result.setDenominator(commonDenominator); return result;}
C++ (Qt)#include <QCoreApplication>#include <QTextStream>#include "shape.h"#include "circle.h"#include "rectangle.h" QTextStream cin(stdin);QTextStream cout(stdout); void printShapeInfo(Shape *ps) { cout << "p = " << ps->perimeter() << endl; cout.flush(); cout << "s = " << ps->area() << endl; cout.flush();} int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Shape *pc = new Circle(3.0); pc->calcPerimeter(); pc->calcArea(); printShapeInfo(pc); delete pc; Shape *pr = new Rectangle(5, 10); pr->calcPerimeter(); pr->calcArea(); printShapeInfo(pr); delete pr; return a.exec();}
C++ (Qt)#ifndef SHAPE_H#define SHAPE_H class Shape{public: Shape(); virtual ~Shape(); virtual double calcPerimeter() = 0; virtual double calcArea() = 0; double perimeter() const; double area() const; protected: double mPerimeter; double mArea;}; #endif // SHAPE_H
C++ (Qt)#include "shape.h" Shape::Shape() : mPerimeter(0.0), mArea(0.0){} Shape::~Shape(){} double Shape::perimeter() const{ return mPerimeter;} double Shape::area() const{ return mArea;}
C++ (Qt)#ifndef CIRCLE_H#define CIRCLE_H #include "shape.h" class Circle : public Shape{public: Circle(double r = 0.0); /*virtual*/ double calcPerimeter(); /*virtual*/ double calcArea(); void setRadius(double r); double radius() const; private: double mRadius;}; #endif // CIRCLE_H
C++ (Qt)#include "circle.h"#include <cmath> Circle::Circle(double r) : mRadius(r){} /*virtual*/ double Circle::calcPerimeter(){ mPerimeter = 2.0 * M_PI * mRadius; return mPerimeter;} /*virtual*/ double Circle::calcArea(){ mArea = M_PI * mRadius * mRadius; return mArea;} void Circle::setRadius(double r){ mRadius = r;} double Circle::radius() const{ return mRadius;}
C++ (Qt)#ifndef RECTANGLE_H#define RECTANGLE_H #include "shape.h" class Rectangle : public Shape{public: Rectangle(double l = 0.0, double w = 0.0); /*virtual*/ double calcPerimeter(); /*virtual*/ double calcArea(); void setLength(double l); double length() const; void setWidth(double w); double width() const; private: double mLength; double mWidth;}; #endif // RECTANGLE_H
C++ (Qt)#include "rectangle.h" Rectangle::Rectangle(double l, double w) : mLength(l), mWidth(w){} /*virtual*/ double Rectangle::calcPerimeter(){ mPerimeter = 2.0 * (mLength + mWidth); return mPerimeter;} /*virtual*/ double Rectangle::calcArea(){ mArea = mLength * mWidth; return mArea;} void Rectangle::setLength(double l){ mLength = l;} double Rectangle::length() const{ return mLength;} void Rectangle::setWidth(double w){ mWidth = w;} double Rectangle::width() const{ return mWidth;}