Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Krisc

overloading operators...?

This topic is 5723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am having trouble overloading the = operators. My AP Comp Sci book doesn''t have a good example of how to do this. Here is my class ShodaGrad.h
#ifndef SHODAGRAD_H
#define SHODAGRAD_H

class ShodaGrad
{
	public:
		// Constructors
			ShodaGrad();

		// Destructors
			~ShodaGrad()
				{		}

		// Members
			double MyVar;

		// Assignment


		// Functions
};

#endif
 
ShodaGrad.cpp
#include "ShodaGrad.h"

ShodaGrad::ShodaGrad()
{
}

 
I was wondering how to add the = and << operators... with the = operator, it needs to be able to take in an integer, not a shodagrad... Thanks for any code or links... <- Digital Explosions -> "Discipline is my sword, faith is my shield do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)

Share this post


Link to post
Share on other sites
Advertisement
Inside your class add:

ShodaGrad& operator=(int newval);


ShodaGrad& ShodaGrad::operator=(int newval)
{
/* add code here */;
return *this;
}

Share this post


Link to post
Share on other sites
What about the << operator...? Thanks a LOT by the way!

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)

Share this post


Link to post
Share on other sites
just look up "operator overloading C++" -r "C++ tutorial" on google and you''ll get examples and a generalized explaination for the process. it''ll end up being much easier than asking again for every specific type of overloading you are doing.

-me

Share this post


Link to post
Share on other sites
hmm never thought of google, but its ok, I have figured it out, thanks a lot everyone!

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)

Share this post


Link to post
Share on other sites
For overloading the << operator:

friend ostream & operator<<(ostream &, const ShodaGrad &);

ostream & operator<<(ostream &ost, const ShodaGrad &obj)
{
ost << "Whatever you want to be outputted ..., maybe the MyVar variable";
ost << MyVar << ", etc";

return *this;
}

Share this post


Link to post
Share on other sites
the << operator has to be a non-member function.

So, to make it work people usually make it a friend and then it can get access to all the internals of the class to put them to the stream.

I prefer (but it doesn't gain you much really) to have a member function called 'output' which has the same signature as the usual << function. Then that function is simply called by the non-member << function.

ie

  // this is the member function that does all the work

std::ostream& ShodaGrad::output(std::ostream&) {
...
}

// this is a non-member function that just passes on the request

std::ostream& operator<<(std::ostream& os, const ShodaGrad& sdg) {
return sdg.output(os);// calls member of ShodaGrad

}


[edited by - petewood on January 17, 2003 4:12:00 PM]

Share this post


Link to post
Share on other sites
Ok, now I am having problems with overloading the + operator, it says that it cannot convert a double to a ShodaGrad, but with the = operator, it should work, shouldn't it? Here is some code...


ShodaGrad& ShodaGrad::operator = ( double value )
{
MyVar = value;

return *this;
}

ShodaGrad operator + ( const ShodaGrad &lhs, const ShodaGrad &rhs )
{
double sum = lhs.value() + rhs.value();

ShodaGrad ans = sum;

return ans;
}

ShodaGrad operator + ( const ShodaGrad &lhs, double &rhs )
{
double sum = lhs.value() + rhs;

ShodaGrad ans = sum;

return ans;
}

ShodaGrad operator + ( const ShodaGrad &lhs, int &rhs )
{
double sum = lhs.value() + rhs;

ShodaGrad ans = sum;

return ans;
}


EDIT: By the way, this class, has a wierd name for one reason, my two friends took their names and meshed them together for this name ShodaGrad...they invented a number system without 0 or infinite. The way it works is the number line is actually a loop. It starts at 1/Google where Google = 10 to the one hundreth power. It gets halfway around an reaches a google. At this point, it turns into negative a google and goes down to -1/google. it forms a loop and google and -google and 1/google and -1/google meet.

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)

[edited by - Krisc on January 17, 2003 4:23:11 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!