mercredi 22 avril 2020

How do i assign values to my fraction objecct using make_unique()?

include // for std::unique_ptr and std::make_unique

include

class Fraction { private: int m_numerator; int m_denominator;

public: Fraction(int numerator, int denominator) : m_numerator{ numerator }, m_denominator{ denominator } { }

friend std::ostream& operator<<(std::ostream& out, const Fraction &f1)
{
    out << f1.m_numerator << "/" << f1.m_denominator;
    return out;
}
friend operator=(const Fraction &f1,const int numerator,const int denominator){
    f1.m_numerator=numerator;
    f1.m_denominator=denominator;
}

};

int main() { // Create a single dynamically allocated Fraction with numerator 3 and denominator 5 std::unique_ptr f1{ std::make_unique(3, 5) }; std::cout << *f1 << '\n';

// Create a dynamically allocated array of Fractions of length 4
// We can also use automatic type deduction to good effect here
auto f2{ std::make_unique<Fraction[]>(4) };
 f2[0]=(3,5);
 f2[1]=(67,82,5,543345);
std::cout << f2[0] << '\n';
    std::cout << f2[1] << '\n';

return 0;

}

Aucun commentaire:

Enregistrer un commentaire