hi, i read in ebook about overleaded iostream operator for class, but im a bit confused about this one :
ostream &operator << (ostream &strm, const FeetInches &obj)
{
strm << obj.feet << " feet, " << obj.inches << " inches";
return strm;
}
Notice the function has two parameters: an ostream reference object and a const
FeetInches reference object. The ostream parameter will be a reference to the actual ostream object on the left side of the << operator. The second parameter is a reference to a FeetInches object. This parameter will reference the object on the right side of the << operator. This function tells C++ how to handle any expression that has the following form:
ostreamObject << FeetInchesObjectSo, when C++ encounters the following statement, it will call the overloaded operator << function:
cout << distance;Notice that the function's return type is ostream &. This means that the function returns a reference to an ostream object. When the return strm; statement executes, it doesn't return a copy of strm, but a reference to it. This allows you to chain together several expressions using the overloaded << operator, such as:
cout << distance1 << " " << distance2 << endl;
my question is : the statement that says
**the function returns a reference to an ostream object, when the "return strm" statement executes it doesn't return a copy of strm, but a reference to it, this allows yout to chain together several expressions using the overloaded << operator. **
so whats the problem when it returns a copy of strm? i dont get it , my head is confused because of the parameter already a reference of an ostream object, and the function then need to return a reference ostream object in order to chain several expression using << operator.... anyone can explain?
ermm and 1 more problem... after i read it, i tried to make a simple program that uses and test the overloaded operator, and it has problems..
here's the main.cpp:
#include "FeetInches.h"
#include <iostream>
using namespace std;
int main()
{
//define FeetInches object and passing arguments to constructor
FeetInches one(6, 32);
FeetInches two(5, 4);
/*using overloaded operator "+" to add FeetInches object named "one" and "two" and store the value to "three" */
FeetInches three = one + two;
cout<<"one + two = "<<three.getFeet()<<" "<<three.getInches()<<endl;
cout<<one<<endl<<two<<endl<<three;
}
here's the FeetInches class declaration :
#ifndef FEETINCHES_H
#define FEETINCHES_H
#include <iostream>
#include <cstdlib>
using namespace std;
class FeetInches;
class FeetInches
{
private:
int feet;
int inches;
public:
FeetInches(int f = 0, int i = 0)
{
feet = f;
inches = i;
}
int getInches() const
{
return inches;
}
int getFeet() const
{
return feet;
}
void simplify();
FeetInches operator=(const FeetInches&);
FeetInches operator+(const FeetInches&);
FeetInches operator-(const FeetInches&);
FeetInches operator++();
FeetInches operator++(int);
bool operator>(const FeetInches&);
bool operator<(const FeetInches&);
friend ostream& operator<<(ostream & , const FeetInches &);
friend istream& operator>>(istream & , FeetInches &);
};
#endif
FeetInches implementation file:
#include "FeetInches.h"
#include <iostream>
#include <cstdlib>
using namespace std;
void FeetInches::simplify()
{
if(inches > 0)
{
feet = feet + (inches / 12);
inches = inches % 12;
}
else if(inches < 0)
{
feet = feet - ((abs(inches) / 12) +1);
inches = 12 - (abs(inches) % 12);
}
}
FeetInches FeetInches::operator=(const FeetInches& right)
{
feet = right.feet;
inches = right.inches;
return *this;
}
FeetInches FeetInches::operator+(const FeetInches& right)
{
FeetInches temp;
temp.feet = feet + right.feet;
temp.inches = inches + right.inches;
temp.simplify();
return temp;
}
FeetInches FeetInches::operator-(const FeetInches& right)
{
FeetInches temp;
temp.feet = feet - right.feet;
temp.inches = inches - right.inches;
temp.simplify();
return temp;
}
FeetInches FeetInches::operator++()
{
inches++;
simplify();
return *this;
}
FeetInches FeetInches::operator++(int)
{
FeetInches temp(feet, inches);
inches++;
simplify();
return temp;
}
bool FeetInches::operator>(const FeetInches& right)
{
bool status;
if(feet > right.feet)
{
status = true;
}
else if(feet == right.feet && inches > right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
bool FeetInches::operator<(const FeetInches& right)
{
bool status;
if(feet < right.feet)
{
status = true;
}
else if(feet == right.feet && inches < right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
ostream& FeetInches::operator<<(ostream& strm, const FeetInches right&)
{
strm<<rigth.feet<<" feet"<<right.inches<<" inches";
return strm;
}
istream& FeetInches::operator>>(istream& strm, FeetInches right&)
{
cout<<"enter feet ";
strm>>right.feet;
cout<<"enter inches ";
strm>>right.inches;
right.simplify();
return strm;
}
i have problems with it, it has some errors T_T.. anyone can help me?
im really sorry for this long post, and alot of question.. its because im learning on my own and i cant ask anyone except here, i already tried to use search engine for further explanation, but i still dont get clear answer..
note: sorry for the bad english