Add to Favorites    Make Home Page 2175 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Algorithms Home » Rational (Mini Project)

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Rational (Mini Project)
Author Fatemeh Nezhadian
Author Email janwin63 [at] yahoo.com
Description About overloading of operators

Category C++ » Algorithms
Hits 381339
Code Select and Copy the Code
Code : //main subject:operator-overloading #include <iostream.h> 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<<a<<"/"<<b<<"="; } /*--------------------------------------------------------*/ void rational::print_floating() { cout<<(float)a/b<<endl; } /*--------------------------------------------------------*/ rational rational::operator+(rational object) { rational temporary; temporary.a=a*object.b+b*object.a; temporary.b=b*object.b; return(temporary); } /*--------------------------------------------------------*/ rational rational::operator-(rational object) { rational temporary; temporary.a=a*object.b-b*object.a; temporary.b=b*object.b; return(temporary); } /*--------------------------------------------------------*/ rational rational::operator*(rational object) { rational temporary; temporary.a=a*object.a; temporary.b=b*object.b; return(temporary); } /*--------------------------------------------------------*/ /* rational rational::operator/(rational object) { rational temporary; temporary.a=a*object.b; temporary.b=b*object.a; return(temporary); } */ /*--------------------------------------------------------*/ rational rational::operator/(rational object) { a=a*object.b; b=b*object.a; return(*this); } /*--------------------------------------------------------*/ rational rational::operator=(rational object) { a=object.a; b=object.b; return(*this); } /*--------------------------------------------------------*/ ostream &operator<<(ostream &output,rational &object) { output<<object.a<<"/"<<object.b<<endl; return output; } /*--------------------------------------------------------*/ istream &operator>>(istream &input,rational &object) { cout<<"Enter a,b:"<<endl; input>>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."<<endl;} if(x!=y) {cout<<"These 2 fractions are not equl."<<endl;} if(x>y) {cout<<"x is bigger than y."<<endl; } if(x<y) {cout<<"x is smaller than y."<<endl;} z1=x+y; cout<<"z1=x+y="; z1.print_fraction(); z1.print_floating(); z2=x-y; cout<<"z2=x-y="; z2.print_fraction(); z2.print_floating(); z3=x*y; cout<<"z3=x*y="; z3.print_fraction(); z3.print_floating(); z4=x/y; cout<<"z4=x/y="; z4.print_fraction(); z4.print_floating(); z5=y=x; cout<<"x="; x.print_fraction(); x.print_floating(); cout<<"y="; y.print_fraction(); y.print_floating(); cout<<"z5="; z5.print_fraction(); z5.print_floating(); /* rational k(1,1),l(1,1),z6,z7,z8,z9,z10; cin>>k>>l; if(k==l) {cout<<"These 2 fractions are equl."<<endl;} if(k!=l) {cout<<"These 2 fractions are not equl."<<endl;} if(k>l) {cout<<"k is bigger than l."<<endl;} if(k<l) {cout<<"k is smaller than l."<<endl;} z6=k+l; z7=k-l; z8=k*l; z9=k/l; z10=l=k; cout<<"z6="<<z6<<"z7="<<z7<<"z8="<<z8<<"z9="<<z9; cout<<"k="<<k<<"l="<<l<<"z10="<<z10; */ return(0); }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=859


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review