C++ program for Rational Number Addition using multiple constructor.

#include<iostream.h>
#include<conio.h>
class rational{
int n,d;
public:
rational(){n=d=0;}
rational(int num,int den){
n=num;
d=den;
}
rational sum(rational r);
void show();
};
rational rational::sum(rational r){
rational t;
t.n=r.d*n+d*r.n;
t.d=d*r.d;
return t;
}
void rational::show(){
cout<<"Rational Number is="<<n<<"/"<<d<<endl;
}
void main(){
rational r1(2,5),r2(3,7),r3;
clrscr();
//Display initial values of objects.
r1.show(
);
r2.show();
r3.show();
getch();
r3=r1.sum(r2);
r3.show();
getch();
}

No comments:

Post a Comment