0% found this document useful (0 votes)
26 views2 pages

131 10.Retional-Number PDF

This document defines a Rational class that represents rational numbers as a ratio of integers. The Rational class includes private data members to store the numerator and denominator, various constructors, and member functions to set and get the numerator and denominator. It also includes an operator+ function to add two Rational numbers and returns a new Rational number, and an ostream operator<< to print Rational numbers to the output stream. The main function demonstrates adding two Rational numbers and printing the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

131 10.Retional-Number PDF

This document defines a Rational class that represents rational numbers as a ratio of integers. The Rational class includes private data members to store the numerator and denominator, various constructors, and member functions to set and get the numerator and denominator. It also includes an operator+ function to add two Rational numbers and returns a new Rational number, and an ostream operator<< to print Rational numbers to the output stream. The main function demonstrates adding two Rational numbers and printing the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program with a Class Rational Number

#include<iostream>
using namespace std;
class Rational
{
private:
int p;
int q;
public:
Rational()
{
p=1;
q=1;
}
Rational(int p,int q)
{
this->p=p;
this->q=q;
}
Rational(Rational &r)
{
this->p=r.p;
this->q=r.q;
}
int getP(){return p;}
int getQ(){return q;}
void setP(int p)
{
this->p=p;
}
void setQ(int q)
{
this->q=q;
}
Rational operator+(Rational r)
{
Rational t;
t.p=this->p*r.q+this->q*r.p;
t.q=this->q*r.q;
return t;
}
friend ostream & operator<<(ostream &os,Rational &r);
};
ostream & operator<<(ostream &os,Rational &r)
{
os<<r.p<<"/"<<r.q;
return os;
}
int main()
{
Rational r1(3,4),r2(2,5),r3;

r3=r1+r2;
cout<<"Sum of "<<r1<<" and "<<r2<<" is "<<r3<<endl;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy