6 Replies - 887 Views - Last Post: 01 May 2010 - 06:28 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

overloading stream operators

Posted 01 May 2010 - 04:59 PM

Hey everyone, I was wondering how I could incorporate my overloaded stream operators into my project...

I have currently built the entire project and have everything working and finished, except the overloaded operators and I need to do both the istream and ostream operators.

I was wondering how I can go abouts doing this?

How I think they work:
class example
{
protected:
float saveDep, checkDep;
string acctName;
public:
friend istream& operator>>(istream &, example &)
friend ostream& operator<<(ostream &, example &)
};


I know that that's how to declare them, but when it comes to using them, IDK how to...

Lets say I want to read in some information from a user and than write it to a file, do I define them as:
 istream& operator>>(istream &strm, example &Ex)
{
cout << "Enter a deposit: ";
cin >> checkDep;
cout << "Enter name on account: ";
cin >> acctName;
strm >> Ex.checkDep >> Ex.acctName;
}

ostream& operator<<(ostream &strm, example &Ex)
{
strm << ex.checkDep << Ex.acctName << endl;
}



???

This post has been edited by IngeniousHax: 01 May 2010 - 05:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: overloading stream operators

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: overloading stream operators

Posted 01 May 2010 - 05:07 PM

http://www.learncpp....e-io-operators/
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: overloading stream operators

Posted 01 May 2010 - 05:15 PM

So are you able to declare the overloaded op with a different name than your class? just when you go to use it, do a cout << S << endl; even though your class may be defined as "save"?
Was This Post Helpful? 0
  • +
  • -

#4 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 94
  • Joined: 18-April 10

Re: overloading stream operators

Posted 01 May 2010 - 05:38 PM

The idea is that you're overloading the operator -- that is, reusing it in a new way. The syntax on the application level stays the same as if you were printing a primitive to the console.

In your implementation, you need to return the ostream (as per the function signature).
Was This Post Helpful? 1
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: overloading stream operators

Posted 01 May 2010 - 06:03 PM

Thanks...

IDK I have been having a hard time coming to terms with the overloaded operators.

I personally see no real desire for them, but, they are there and obviously serve a purpose further than my knowledge.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: overloading stream operators

Posted 01 May 2010 - 06:18 PM

overloading operators is pretty much for convince when handling your objects.


It helps when you need to compare values within different classes. To sum it up it will make your objects more use friendly you could say
#include <iostream>

using namespace std;

class Money
{
	double amount;

public:
	Money(double money)
		:amount(money)
	{}

	friend bool operator<(const Money &arg1, const Money &arg2)
	{
		return arg1.amount<arg2.amount;
	}
};

int main()
{
	Money me(1.43);
	Money you(3.44);

	if(me<you)
		cout<<"YOU HAVE MORE MONEY"<<endl;
	else
		cout<<"I HAVE MORE MONEY"<<endl;

	cin.ignore();
	cin.get();
	return 0;
};



if you did not overload the < operator you would have to go and create a function to check the values.
Was This Post Helpful? 0
  • +
  • -

#7 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 94
  • Joined: 18-April 10

Re: overloading stream operators

Posted 01 May 2010 - 06:28 PM

View PostIngeniousHax, on 01 May 2010 - 05:03 PM, said:

Thanks...

IDK I have been having a hard time coming to terms with the overloaded operators.

I personally see no real desire for them, but, they are there and obviously serve a purpose further than my knowledge.

Operator overloading is a little messy in C++, but its in the spirit of the OO paradigm. It wasn't until I learned Java that I started using operator overloading in C++. Java's equivalent implementation is a lot different and not quite as liberal, but I feel its a bit more sane -- implementing interfaces like Comparable and overriding functions such as equals() and toString().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1