overloading operators...?

Started by
7 comments, last by Krisc 21 years, 3 months ago
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)
Advertisement
Inside your class add:

ShodaGrad& operator=(int newval);


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

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)
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
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)
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;
}
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
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 workstd::ostream& ShodaGrad::output(std::ostream&) {...}// this is a non-member function that just passes on the requeststd::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]
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]
operator= doesn''t convert, constructor does
ShodaGrad(double x) {}

This topic is closed to new replies.

Advertisement