#include typedef long long fraction_part; class Fraction { private: fraction_part numerator; fraction_part denominator; static size_t instanceCount; public: Fraction(fraction_part numerator = 0, fraction_part denominator = 1) : numerator(numerator) , denominator(denominator) { //numerator = 0; // no usar, esto es asignacion, no inicializacion //denominator = 1; simplify(); ++instanceCount; } void print() { std::cout << numerator << '/' << denominator; } static size_t getInstanceCount() { return instanceCount; } /// @return Returns true if this fraction was changed bool simplify() { fraction_part common = gcd(); numerator /= common; denominator /= common; if ( denominator < 0 ) { numerator *= -1; denominator *= -1; } return common > 1; } fraction_part gcd() { return gcd(numerator, denominator); } // greatest common divisor static fraction_part gcd(fraction_part a, fraction_part b) { a = a >= 0 ? a : -a; b = b >= 0 ? b : -b; while ( b > 0 ) { fraction_part t = b; b = a % b; a = t; } return a; } Fraction operator+(const Fraction& other) const { return Fraction(this->numerator * other.denominator + other.numerator * denominator, /*this->*/denominator * other.denominator); } }; size_t Fraction::instanceCount = 0; int cout; int main(/*int argc, char* argv[]*/) { /* // Espacios de nombres: long cout; // variable local tapa global ::cout = 1; // el int global cout = 2; // el long local std::cout << ::cout; // el objeto cout << el int */ Fraction f1(6, -4); Fraction f2(-6); Fraction f3; std::cout << "f1 = "; f1.print(); std::cout << "\nf2 = "; f2.print(); std::cout << "\nf3 = "; f3.print(); std::cout << "\nf1 + f2 = "; Fraction f4 = f1 + f2; f4.print(); std::cout << std::endl << Fraction::getInstanceCount() << " fracciones\n"; return 0; }