Utility or Helper Functions

My assignment is to create stand-alone C style utility or helper functions.  These are to consist of 4 separate functions.  I am confused on how to use these functions along with the Class.  I am suppose to run a test file that illustrates how the EtoM actually alters the pased Distance object?
//Distance Class Implementation:
/*==============================================*/
/* Distance:  default constructor, no parameters
/*      use 0 as the distance
/*==============================================*/
Distance::Distance() : totalmm (0) { }
//==============================================//
//GetDistance: pass Type enum and based on its value
//  returns distance as double mm or inches
//==============================================//
double Distance::GetDistance (Type t) const {
	switch(t)
	{
	case Distance::Metric:
		return totalmm;
	case Distance::English:
		 return  (totalmm * ITM);
	}
 	return -1; // Error condition (or throw an exception)
}
//==============================================//
//SetDistance:  pass Type enum as 1st parameter 
//   and then  a double which represents either mm or in
//   according to enum value
//==============================================//
void Distance::SetDistance(Type t, double mm) {
	type = t;
	totalmm = mm;
}
/*==================================*/
/* Distance:  constructors
/*==================================*/
//Accept double number of millimeters
Distance::Distance (Type t, double mmormiles) {
	switch(t)
	{
	case Distance::Metric:
		totalmm = mmormiles;
	case Distance::English:
		 totalmm = mmormiles * MTM; 
	}
}
//Accept trio of longs - meters, centimeters, & millimeters
Distance::Distance (Type t, long m, long cm, double mm) { 
	type = t;
    mm = 0;
	totalmm = (long) mm + cm * 100 + m * 1000;
}
//Accept 2 ints - feet & inches
Distance::Distance (Type t, long ft, double in) { 
	type = t;
    in = 0;
	totalmm = EtoM(in);
}
//==============================================//
//Metric to English (Distance_MtoE):  passed reference
//  to a Distance object.  Returns the object's distance
// as a double inches.
//=============================================//
double MtoE (const Distance &dist) {
	return (dist.totalmm * ITM);  // return dble inches
}
//=============================================//
//Distance English to Metric (Distance_ EtoM):  
//  passes a reference to a Distance object & a 
//  double inches.  After converting inches to mm, it
//  stores that measurement in the Distance object
//  and reutrns the mm as a double
//============================================//
double EtoM (const Distance &dist, double inches) {
	inches = (dist.totalmm * ITM);
	inches = dist.totalmm;
	return dist.totalmm; // return double mm
}
 
//Then I have additional C++ style functions as instructed outside the class
//==============================================//
//Metric to English (double_MtoE): returns double mm 
// representing the inches that this distance represents
//=============================================//
const double MtoE (double mm) {
	double inches;
	mm = 0;
	inches = mm / ITM;
	 return inches;
}
 
 
 
//============================================//
//English to Metric (double_EtoM): passed double inches
//  and returns that value converted to a double mm
//============================================//
const double EtoM (double inches)  {
	double mm;
    inches = 0;
	mm = inches * ITM;
	return mm;
}

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.

Infinity08Commented:
>> I am confused on how to use these functions along with the Class.

You can call a standalone function without a problem from a class method.
0
Infinity08Commented:
So, basically, place the utility functions in a separate .cpp file. Then create a header for that file, and put the function prototypes in there. You can then include the header file wherever you need to use the utility functions, and you're good to go.
0
evilrixSenior Software Engineer (Avast)Commented:
Distance::Metric vs. Distance::English

Like I said before, "English" should be called "Imperial" -- we also use metric in the UK :)

"The United Kingdom completed its legal transition to SI units in 1995"
http://en.wikipedia.org/wiki/Imperial_units
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

evilrixSenior Software Engineer (Avast)Commented:
>> I am confused on how to use these functions along with the Class
Can you elaborate on what is your confusion? Your question is a little open ended and I'm not sure exactly what it is you are asking.
0
gndcdospsAuthor Commented:
First I apologize for not using the proper terminology...  

In this assignment the functions are no longer part of the class, as in the one I completed last week (see Attachment).  The instructions had us change the 'EtoM' and 'MtoE' functions to the utility / helper functions:

4. Remove MtoE and EtoM as class member functions. They are now to be stand-alone C style
utility or helper functions.
MtoE is passed a double millimeters measurement and returns that value
converted into a double inches.
MtoE is passed a reference to a Distance object. It returns the objects distance
as a double inches.
EtoM is passed a double inches measurement and returns that value converted to
a double millimeters.
EtoM is passed a reference to a Distance object and a double inches. After converting
the inches into millimeters, it stores that measurement in the Distance object and
returns the millimeters as a double.
 
So now I have these two friend functions and the two C style functions (and I am not even sure if they are coded correctly), but I do not know how to use them in main (or the client program) to test that they are doing what they are suppose to do.  

Pgm01-5.pdf
0
jkrCommented:
Assuming that you correctly declared the data members of your class as non-public, you will either have to provide accessor methods for that data or make these functions 'friends' of your class, i.e.
class Distance
{
 
friend double MtoE (const Distance &dist);
friend double EtoM (const Distance &dist, double inches);
 
//...
 
};
 
// *or*
 
class Distance {
 
public:
 
  double get_totalmm () const { return totalmm;}
 
//...
 
};
 
//==============================================//
//Metric to English (Distance_MtoE):  passed reference
//  to a Distance object.  Returns the object's distance
// as a double inches.
//=============================================//
double MtoE (const Distance &dist) {
        return (dist.get_totalmm() * ITM);  // return dble inches
}
//=============================================//
//Distance English to Metric (Distance_ EtoM):  
//  passes a reference to a Distance object & a 
//  double inches.  After converting inches to mm, it
//  stores that measurement in the Distance object
//  and reutrns the mm as a double
//============================================//
double EtoM (const Distance &dist, double inches) {
        inches = (dist.get_totalmm() * ITM);
        //inches = dist.totalmm; this line makes NO sense
        return dist.get_totalmm(); // return double mm <--- are you sure?
}
 
//Then I have additional C++ style functions as instructed outside the class
//==============================================//
//Metric to English (double_MtoE): returns double mm 
// representing the inches that this distance represents
//=============================================//
const double MtoE (double mm) {
        double inches;
        mm = 0;
        inches = mm / ITM;
         return inches;
}

Open in new window

0
Infinity08Commented:
>> but I do not know how to use them in main (or the client program) to test that they are doing what they are suppose to do.  

You can just call the functions ... For example :

        double inches = MtoE(20.5);        // <--- returns 20.5 millimeters in inches

Also take a look at my second post, suggesting how exactly to do this.
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
gndcdospsAuthor Commented:
>>So, basically, place the utility functions in a separate .cpp file. Then create a header for that file, and put the function prototypes in there. You can then include the header file wherever you need to use the utility functions, and you're good to go.

I have done this.  And thank you for the example.  

One more question if you do not mind...  How do I test the friend functions?
(Instructions:  EtoM is passed a reference to a Distance object and a double inches. After converting
the inches into millimeters, it stores that measurement in the Distance object and
returns the millimeters as a double.)
double EtoM (const Distance &dist, double inches) {
	inches = (dist.totalmm * ITM);
	inches = dist.totalmm;
	return dist.totalmm; // return double mm
}

Open in new window

0
jkrCommented:
>>One more question if you do not mind...  How do I test the friend functions?

Just like

Distance dist(Distance::Metric,42.0);

double res = EtoM(dist,2.0);

The compiler will complain if anything is incorrect.
0
evilrixSenior Software Engineer (Avast)Commented:
>> How do I test the friend functions?

Are you talking about unit testing your code?  If so, your test code should call the function you wish to test with values to which you already know the outcome. You then have your code test the outcome to ensure it matches pre-calculated results, erroring if it doesn't. In this case, call EtoM with dist and index set to values to which you know what EtoM should return. Then, test that EtoM does return the value you expect. You should perform this test a number of times with different pre-calculated values and results to ensure you are getting good test coverage.

http://en.wikipedia.org/wiki/Unit_testing
http://en.wikipedia.org/wiki/Code_coverage

-Rx.
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.