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?
 
lucky_jamesConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
 
Infinity08Connect With a Mentor Commented:
>>         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
All Courses

From novice to tech pro — start learning today.