Link to home
Start Free TrialLog in
Avatar of gndcdosps
gndcdosps

asked on

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

Avatar of lucky_james
lucky_james
Flag of India image

>> 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
>>or overload '=' operator.
sorry, this wont help you. So, ignore it.
Avatar of gndcdosps
gndcdosps

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of lucky_james
lucky_james
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of evilrix
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.
oh, yes........ :(

thnx evilrix for pointing that out!! :)
>> thnx evilrix for pointing that out
No worries :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial