123 Eng

### Engineering the engineers™

Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

Computer Science / IT
Electronics
Electrical
Mechanical
Chemical
Civil

CAT / MBA

GMAT / Foreign MBA
Latest Jobs

Displaying  Source Code(s)

 Rational (Mini Project) -------------------------------------------------------------------------------- Description : About overloading of operators Code : //main subject:operator-overloading #include class rational { private: int a; int b; public: rational(int=1,int=1); void set_ab(int,int); void print_fraction(); void print_floating(); rational operator+(rational object); rational operator-(rational object); rational operator*(rational object); rational operator/(rational object); rational operator=(rational object); friend ostream &operator<<(ostream &,rational &); friend istream &operator>>(istream &,rational &); int operator==(rational object); int operator!=(rational object); int operator>(rational object); int operator<(rational object); }; /*--------------------------------------------------------*/ rational::rational(int m,int n) { set_ab(m,n); } /*--------------------------------------------------------*/ void rational::set_ab(int x,int y) { int temporary,m,n; m=x; n=y; if(n>m) { temporary=n; n=m; m=temporary; } while(m!=0 && n!=0) { if(m%n==0) break; temporary=m%n; m=n; n=temporary; continue; } a=x/n; b=y/n; } /*--------------------------------------------------------*/ void rational::print_fraction() { cout<>(istream &input,rational &object) { cout<<"Enter a,b:"<>object.a; input>>object.b; return input; } /*--------------------------------------------------------*/ int rational::operator==(rational object) { if(a==object.a && b==object.b) return(1); else return(0); } /*--------------------------------------------------------*/ int rational::operator!=(rational object) { if(a!=object.a || b!=object.b) return(1); else return(0); } /*--------------------------------------------------------*/ int rational::operator>(rational object) { return((a/b)>(object.a/object.b) ? 1:0); } /*--------------------------------------------------------*/ int rational::operator<(rational object) { return((a/b)<(object.a/object.b) ? 1:0); } /*--------------------------------------------------------*/ int main() { rational x(3,4),y(3,4),z1,z2,z3,z4,z5; if(x==y) {cout<<"These 2 fractions are equl."<y) {cout<<"x is bigger than y."<l) {cout<<"k is bigger than l."<

Contribute content or training reports / feedback / Comments
job placement papers