C ++ (3) a rational number class

Source: Internet
Author: User

Class rational
{
Public:
Rational (INT Numerator = 1, int demominator = 1 );
Rational (const rational &);
Int getnumerator () const;
Int getdemominator () const;
Void printrational () const;
Void setnumerator (INT numberator );
Void setdemominator (INT demominator );
Const rational operator = (const rational &);
PRIVATE:
Int numerator;
Int demominator;
};

Rational: Rational (INT numerator, int demominator): numerator (numerator ),
Demominator (demominator)
{

}

Rational: Rational (const rational & RHs): numerator (RHS. getnumerator ()),
Demominator (RHS. getdemominator ())
{
 
}

Const rational: Operator = (const rational & RHs)
{
Return rational (RHS. getnumerator (), RHS. getdemominator ());
}

Int rational: getdemominator () const
{
Return demominator;
}

Int rational: getnumerator () const
{
Return numerator;
}

Void rational: setdemominator (INT demominator)
{
Demominator = demominator;
}

Void rational: setnumerator (INT numberator)
{
Numberator = numberator;
}

Void rational: printrational () const
{
Cout <"rational (" <getnumerator () <"," <getdemominator () <")" <Endl;
}

Const rational operator * (const rational & rhs1, const rational & rhs2)
{
Return rational (rhs1.getnumerator () * rhs2.getnumerator (),
Rhs1.getdemominator () * rhs2.getdemominator ());
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.