448,974 Members | 1,586 Online
Need help? Post your question and get tips & solutions from a community of 448,974 IT Pros & Developers. It's quick & easy.

# How can I adding two class object into a variable...?

 P: n/a the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 should be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: a) Adding two Rational numbers. The result should be stored in reduced form. b) Subtracting two Rational numbers. The result should be stored in reduced form. c) Multiplying two Rational numbers. The result should be stored in reduced form. d) Dividing two Rational numbers. The result should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is numerator and b is denominator. f) Printing Rational numbers in floating-point format. I have writen the code like the following: #ifndef RATIONAL_H #define RATIONAL_H class rational { public : rational(); void get_f1(int, int); void get_f2(int, int); void add(rational&, int, int, int ); private : int numer; int denom; int result; int result_n; int result_d; }; #endif #include #include "rational.h" using namespace std; rational :: rational() {numer = denom = result = result_n = result_d = 0 ;} void rational :: get_f1(int numer, int denom) { do { cout << "please input the first fraction's numerator: " ; cin >> numer; cout << "please input the first fraction's denominator: "; cin >> denom; cout << "the fraction is: " << numer << "/" << denom <> numer; cout << "please input the second fraction's denominator: "; cin >> denom; cout << "the fraction is: " << numer << "/" << denom < #include "rational.h" using namespace std; int main () { rational rational_1, rational_result; int numer, denom, result, result_n, result_d; rational_1.get_f1(numer, denom); rational_1.get_f2(numer, denom); rational_result.add(rational_1, result, result_n, result_d); return 0; } Jul 22 '05 #1
5 Replies