Program to illustrate Function Returning Object


#include <iostream.h>
#include<conio.h>
class Rational
{
        int numerator;
        int denominator;
    public :
        void set(int, int);
        int get_numerator () { return numerator; }
        int get_denominator () { return denominator; }
        void print();
        Rational sum (Rational);
};
void Rational::set(int a, int b)
{
    numerator = a;
    denominator = b;
}
void Rational::print()
{
    cout << numerator << " / " << denominator;
    cout << "\n";
}
Rational Rational::sum(Rational n)
{
     Rational Tobject;
    int temp = 0;
    temp += numerator * n.get_denominator();
    temp += denominator * n.get_numerator();
    Tobject.numerator = temp;
    Tobject.denominator = denominator * n.get_denominator();
    return Tobject;
}
void main(void)
{
    Rational a,b,c;
    clrscr();
    a.set(2,5);
    b.set(3,7);
    c.set(0,0);
    c = a.sum(b);
    cout << "a = "; a.print();
    cout << "b = "; b.print();
    cout << "c = "; c.print();
    getch();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|