please write overloaded assignment for the following rat class: #include <iostre
ID: 3557405 • Letter: P
Question
please write overloaded assignment for the following rat class:
#include <iostream>
using namespace std;
class Rat{
private:
int n;
int d;
public:
// constructors
// default constructor
Rat(){
n=0;
d=1;
}
// 2 parameter constructor
Rat(int i, int j){
n=i;
d=j;
}
// conversion constructor
Rat(int i){
n=i;
d=1;
}
//accessor functions (usually called get() and set(...) )
int getN(){ return n;}
int getD(){ return d;}
void setN(int i){ n=i;}
void setD(int i){ d=i;}
//arithmetic operators
Rat operator+(Rat r){
Rat t;
t.n=n*r.d+d*r.n;
t.d=d*r.d;
return t;
}
// 2 overloaded i/o operators
friend ostream& operator<<(ostream& os, Rat r);
friend istream& operator>>(istream& is, Rat& r);
}; //end Rat
// operator<<() is NOT a member function but since it was declared a friend of Rat
// it has access to its private parts.
ostream& operator<<(ostream& os, Rat r){
os<<r.n<<" / "<<r.d<<endl;
return os;}
// operator>>() is NOT a member function but since it was declared a friend of Rat
// it has access to its provate parts.
istream& operator>>(istream& is, Rat& r){
is>>r.n>>r.d;
return is;
}
also please tell me whether overloaded assignment is member function or not ?
Explanation / Answer
#include <iostream>
using namespace std;
class Rat{
private:
int n;
int d;
public:
// constructors
// default constructor
Rat(){
n=0;
d=1;
}
// 2 parameter constructor
Rat(int i, int j){
n=i;
d=j;
}
// conversion constructor
Rat(int i){
n=i;
d=1;
}
//accessor functions (usually called get() and set(...) )
int getN(){ return n;}
int getD(){ return d;}
void setN(int i){ n=i;}
void setD(int i){ d=i;}
//arithmetic operators
Rat operator+(Rat r){
Rat t;
t.n=n*r.d+d*r.n;
t.d=d*r.d;
return t;
}
void operator=(Rat r){
this->n=r.n;
this->d=r.d;
}
// 2 overloaded i/o operators
friend ostream& operator<<(ostream& os, Rat r);
friend istream& operator>>(istream& is, Rat& r);
}; //end Rat
// operator<<() is NOT a member function but since it was declared a friend of Rat
// it has access to its private parts.
ostream& operator<<(ostream& os, Rat r){
os<<r.n<<" / "<<r.d<<endl;
return os;}
// operator>>() is NOT a member function but since it was declared a friend of Rat
// it has access to its provate parts.
istream& operator>>(istream& is, Rat& r){
is>>r.n>>r.d;
return is;
}
________________________________main___________________________________
int main(){
Rat R1=Rat();
Rat R2=Rat(2);
Rat R3=Rat(4,5);
cout<<endl<<endl;
cout<<"R1"<<endl;
cout<<"R1.n "<<R1.getN()<<endl;
cout<<"R1.d "<<R1.getD()<<endl;
cout<<endl<<endl;
cout<<"R2"<<endl;
cout<<"R2.n "<<R1.getN()<<endl;
cout<<"R2.d "<<R1.getD()<<endl;
cout<<endl<<endl;
cout<<"R3"<<endl;
cout<<"R3.n "<<R1.getN()<<endl;
cout<<"R3.d "<<R1.getD()<<endl;
cout<<endl<<endl;
cout<<"R3=R1+R2"<<endl;
R3=R1+R2;
cout<<"R3.n "<<R3.getN()<<endl;
cout<<"R3.d "<<R3.getD()<<endl;
cout<<"enter n and d for RAT1 ";
cin>>R1;
cout<<endl<<endl;
cout<<"R1"<<endl;
cout<<R1;
cout<<" ";
cout<<"R1=R2 ";
R1=R2;
cout<<"R1 "<<R1<<endl;
cout<<"R2 "<<R2<<endl;
return 0;
}
------------------------------------------------------------------------------------
opartaor+ as declared in class is memeber function
but overloaded operators >> and << were not as they are declared friend and declared outside of class without using class scope :: operator still they canm access private variables as they are friend of class Rat.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.