[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Utility or Helper Functions

Posted on 2008-02-10
10
Medium Priority
?
1,060 Views
Last Modified: 2011-10-19
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

0
Comment
Question by:gndcdosps
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20861483
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20861488
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20861499
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
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.

 
LVL 40

Expert Comment

by:evilrix
ID: 20861506
>> 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
 

Author Comment

by:gndcdosps
ID: 20861556
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
 
LVL 86

Expert Comment

by:jkr
ID: 20861567
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 20861597
>> 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
 

Author Comment

by:gndcdosps
ID: 20861686
>>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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 20861700
>>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
 
LVL 40

Expert Comment

by:evilrix
ID: 20861718
>> 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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question