Objects as Function Arguments -by value


#include <iostream.h>
#include<conio.h>
class Rational
{
        int numerator;
        int denominator;
    public :                                 
        void set(int, int); // prototypes declared
        void print();
        void sum (Rational, Rational);
};
void Rational::set(int a, int b)
{
    numerator = a;
    denominator = b;
}
void Rational::print()
{
    cout << numerator << " / " << denominator;
    cout << "\n";
}
void Rational::sum(Rational n1, Rational n2)     // n1,n2 are objects                                     
{                                           
    int temp = 0;
    temp += n1.numerator * n2.denominator;
    temp += n1.denominator * n2.numerator;

    numerator = temp;
    denominator = n1.denominator* n2.denominator;
}
void main(void)
{
    Rational a, b, c;
    clrscr();
    a.set(2,5);
    b.set(3,7);
    c.set(0,0);
    c.sum(a,b);        // Objects passed as arguments-by value
    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|