Pass a reference of a class objet to a function

I am having trouble on how to get a function that is not a class member function (and is not alllowed to be)... homework assignment.... to pass a reference to a class object and then return a value.  I continue to get binary errors; and after sevaral attempts and re-work... still unsuccessful.  I hope someone can take a peek and lead me in the right direction.

Here is my function (let me know if you need to see more code):

//==============================================//
//Metric to English (Distance_MtoE):  passed reference
//  to a Distance object.  Returns the object's distance
// as a double inches.
//=============================================//
double MtoE (const Distance& m)  {
	// 25.4 mm = 1 in
	Distance mm;
	mm = m;
	double inches;
	inches = (mm * ITM) ;
	return inches;
}

Open in new window

gndcdospsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lucky_jamesCommented:
>> Distance mm;  mm = m;

These statements are not necessary. You already have one object to play with.
Moreover, you may need to make a temporary object when you are updating some value of the object while processing it; and you dont want the values in your original object to get change.

>> inches = (mm * ITM) ;
you are multiplying directly with the value of object. Distance must be having some member variable.
either use:
object.variable * multiplier

or overload '=' operator.


Hope it helps.

James
0
lucky_jamesCommented:
>>or overload '=' operator.
sorry, this wont help you. So, ignore it.
0
gndcdospsAuthor Commented:
I just tried the following, and maybe I am not understanding what you are trying to tell me.  But my member variable is protected.  (per instructions)  
Snippet of class definition:
protected:
	double totalmm; //total distance in millimeters
	Type type;
	ShowAs show;
 
double MtoE (const Distance& m)  {
	// 25.4 mm = 1 in
	//Distance mm;
	//mm = m;
	double inches = 0;
	inches = (m.totalmm * ITM) ;
	return inches;
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

lucky_jamesCommented:
yup, u r on right path. :)
see, ways to get the value are:
1. Code a method and return the value of this protected member variable.
in such a case your code will become:
        return m.totalmm * ITM;

2. Make this variable public, instead of protected.
in such a case, your code will become:
        return m.Method_Name() * ITM;


regards,
James


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
I think lucky_jamesmay have transposed his examples for 1 and 2. Below I've changed it to what I believe he meant.

1. Code a method and return the value of this protected member variable.
in such a case your code will become:
        return m.Method_Name() * ITM;

2. Make this variable public, instead of protected.
in such a case, your code will become:
        return m.totalmm * ITM;

To preserve encapsulation, which is a fundamental principle of good object oriented design, you should prefer option 1. The idea is to protect the class from just anyone dipping in and changing its internals, otherwise the class cannot be responsible for its own state. This is the whole point of the protected/private access specification. In fact, I would go as far as to say all data members should be private so even a derived class has to manipulate a base classes member data through accessors.

http://en.wikipedia.org/wiki/Encapsulation_%28classes_-_computers%29

-Rx.
0
lucky_jamesCommented:
oh, yes........ :(

thnx evilrix for pointing that out!! :)
0
evilrixSenior Software Engineer (Avast)Commented:
>> thnx evilrix for pointing that out
No worries :)
0
Infinity08Commented:
>>         inches = (mm * ITM) ;

For this to work, you will have to provide a multiplication operator :

        double operator*(Distance &dist, double &d) {
            return (dist.totalmm * d);
        }

and make it a friend of the Distance class.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.