Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

operator overloading in C++

//adding two fraction classes together
//Fraction class has a numerator, denominator field with accessors
//A new fraction class is returned with the new numerator and denominator
Fraction operator+(const Fraction& left, const Fraction& right){
    int newDenom = left.denominator() * right.denominator();
    int newNumerator = (left.denominator() * right.numerator()) + (right.denominator() * left.numerator());
    Fraction newFraction(newNumerator, newDenom);
    return newFraction;
};
Source by syntaxdb.com #
 
PREVIOUS NEXT
Tagged: #operator #overloading
ADD COMMENT
Topic
Name
9+3 =