• ### Announcements

#### Archived

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

## 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

{
public:
// Constructors

// Destructors
{		}

// Members
double MyVar;

// Assignment

// Functions
};

#endif

#include "ShodaGrad.h"

{
}


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)

{
return *this;
}

##### 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 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 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 on other sites

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 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 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]

##### 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 on other sites
operator= doesn''t convert, constructor does

• ### Forum Statistics

• Total Topics
628294
• Total Posts
2981876

• 11
• 10
• 10
• 11
• 17