We help IT Professionals succeed at work.

# Public Member Functions

on
I am working on a homework assignment and am at a lost on what to do

First, I am to create a class to encapsulate linear distances.  I am to have only 1 data memeber; double millimeters.  Next, I need 5 constructors.  Then I am to create public member functions.

So here is my header file... I think this is correct?

#pragma once
#include <iostream>

using namespace std;

/*==================================*/
/* Declare constant variables
/*==================================*/
// Convert meters to feet
const double MTF = 3.28083;

// Convert miles to millimeters
const double MTM = 160934;

// Convert inches to millimeters
const double ITM = 25.4;

// Inches
const int I = 12;

/*==================================*/
/*Distance Class
/*==================================*/
class Distance {
/*==================================*/
/* class data
/*==================================*/
protected:
double totalmm; //total distance in millimeters
/*==================================*/
/* class function
/*==================================*/
public:
//constructors
Distance ();
Distance (long millimeter);
Distance (long meter, long centimeter, long millimeter);
Distance (int feet, int inches);
Distance (double miles);

//Access Functions
double GetDistance () const;  // distance storing converted to mm
void SetDistance (double mm); // set the stored distance accordingly

//Conversion functions
//Returns double representing feet that this distance represents
double MtoE () const;

// Will use TWICE:  one to pass 2 ints, & one to pass a double, both returns dble millimeters
double EtoM () const;

//Determine how to print the distance
char* Paint () const; // single char
};

Of course it is the implementation I am having trouble with....

#include <iostream>
#include <iomanip>
using namespace std;

#include "Distance.h"
/*==============================================*/
/* Distance:  default constructor, no parameters
/*      use 0 as the distance
/*==============================================*/
Distance::Distance() {
totalmm = 0; //using 0 as distance
}

//==============================================//
//GetDistance:  returns a double which is the
//  distance I am storing converted to mm  -----------------------> Tested & Worked
//==============================================//
double Distance::GetDistance () const {
}
//==============================================//
//SetDistance:  accepts a double mm - set stored
//   distance accordingly                      -----------------------> Tested & Worked
//==============================================//
void Distance::SetDistance(double mm) {
totalmm = mm;
}

/*==================================*/
/* Distance:  constructors                      -----------------------> I think these are right?
/*==================================*/
//Accept long number of millimeters
Distance::Distance (long mm) { }

//Accept trio of longs - meters, centimeters, & millimeters
Distance::Distance (long m, long cm, long mm) { }

//Accept 2 ints - feet & inches
Distance::Distance (int ft, int in) { }

//Accept double miles
Distance::Distance (double mi) { }

//==============================================//
//Metric to English (MtoE): returns double representing
// the feet that this distance represents                           -----------------------> Tested & Worked
//=============================================//
double Distance::MtoE () const {
double result;
result = (totalmm * MTF) / 1000;
return result;
}

//=============================================//
//English to Metric (EtoM):  two ints (feet and inches),
// and returns the double millimeters    -----------------------> Confused here.  I do not think the coding
is correct.
1.  I am getting an error message:
'const' already has a body , but
I am to use the same function name, but
one accepts 2 ints, the other is a dble
Both are to return a double.
2.  I am testing with ....
Distance d (10, 11);
cout << d.EtoM;
//============================================//
double Distance::EtoM () const {

double result;
// (feet * 12 + inches) / (12 * 25.4)
result = (totalmm * I + totalmm) / I * ITM;
return result;
}

//============================================//
//English to Metric (EtoM): double miles and returns
// the double millimeters                                                  -----------------------> Tested & Worked
//============================================//
double Distance::EtoM () const {

double result;
// miles * 160934
result = totalmm * MTM;
return result;
}

/*
//============================================//
//Paint: char which is sed to determine how to print             ----------------.> No question, here yet...
// distance                                                                                                       Still working on it.
//===========================================//
char Distance::Paint (char unit) const {
if (toupper (unit) == 'M')
cout << "Distance is = " << mm << " millimeters \n";
if (toupper (unit) == 'F')
cout << "Distance is = " << ft << " feet \n";
if (toupper (unit) == 'I')
cout << "Distance is = " << in << " inches \n";
if (toupper (unit) == 'L')
cout << "Distance is = " << mi << " miles \n";

else {
cerr << "Error";
}
}

//============================================//
//Fun:                                                                                        ----------------.> No question, here yet...
//                                                                                                                         Still working on it.
//===========================================//

Distance Distance::Fun (const Distance &x) const {
Distance y, z;
cout << "fun:  passed value: ";
x.Paint ('M');
y = z = x;
y.SetDistance (1234.);
cout << "fun:  alterd value: ";
y.Paint ('M');
return z;
}
*/
Comment
Watch Question

## View Solutions Only

CERTIFIED EXPERT
Top Expert 2012

Commented:
You have two implementations of the same function in

//============================================//
double Distance::EtoM () const {

double result;
// (feet * 12 + inches) / (12 * 25.4)
result = (totalmm * I + totalmm) / I * ITM;
return result;
}

//============================================//
//English to Metric (EtoM): double miles and returns
// the double millimeters                                                  -----------------------> Tested & Worked
//============================================//
double Distance::EtoM () const {

double result;
// miles * 160934
result = totalmm * MTM;
reurn result;
}

You will have to remove one of them.
CERTIFIED EXPERT
Top Expert 2012

Commented:
BTW, after removing one of these, it compiles fine.
Top Expert 2007

Commented:
Both
double Distance::EtoM () const {
have the same signature, so the compiler can't tell them apart.

Methods of the same name are distinguished by the arguments, such as
double Distance::EtoM (int feet, int inches) const {
double Distance::EtoM (double miles) const {
Software Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
About the general design of your class, I suggest to switch to the following:
you can use a unique constructor like:

public:
Dimension(double value, enum Unit unit);

so you can use as:

Dimension x(10.0, Milimeters);

Commented:
jkr - I have to leave the naming of the functions the same - per assignment.

josgood - Once I do as you suggested... I get this error message:
Distance_Test_Program.obj : error LNK2019: unresolved external symbol "public: double __thiscall Distance::EtoM(void)const " (?EtoM@Distance@@QBENXZ) referenced in function _main
My confusion now, is how to call it in main, and is the function correct?

Here is how I was testing it in main:
f.SetDistance (1.23);
cout << "Testing EtoM member function 1.23 miles equals "
<< f.EtoM () << " millimeters\n";

double Distance::EtoM (double miles) const {

double result;
// miles * 160934
result = totalmm * MTM;
return result;
}

Commented:
josgood - - Hang on... I think I may have figured it out.
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>I have to leave the naming of the functions the same - per assignment.

Yes, but one of them is superfluous and causes ambiguities - and that's what the compiler is complaining about. You *have* to remove one of them, preferrably the one that is not labeled as "tested and works fine".

You cannot have two functions that are declared identically in one class. If the assignment requires that, something is going wrong here, since you cannot get that to work by definition.
Top Expert 2007

Commented:
Since some time has passed, I assume you're still having difficulties

You have all the pieces for your solution, let me do a small bit of organization for one part...
The ITM constant is really millimeters per inch (25.4 mm per 1 inch), hence multiplication required

void main() {
Distance dist(1/*feet*/,2/*inches*/);
}

Distance::Distance (int ft, int in) {
totalmm = EtoM(ft,in);
}

double Distance::EtoM (int feet, int inches) const {

double result;
// (feet * 12 + inches) / (12 * 25.4)
result = (feet * I + inches) * ITM;
return result;
}

Commented:
josgood - - Thank you very much for your help.  I did get the coding to work...

I did this..

double Distance::EtoM (int feet, int inches) const {
double result;
// (feet * 12 + inches) / (12 * 25.4)
result = (feet * I + inches) / I * ITM;
return result;
}

And I tested it with this...
int feet = 10;
int inches =11;
cout << "Testing EtoM member function 10 feet, 11 inches equals "
<< d.EtoM (feet, inches) << " millimeters\n\n";

I just keep confusing myself, because our teach had us do only one Set function that accepts only a double.
He wants us to test with the following... Now if my set function can only accept a double, and this is the only one I am suppose to do.... How do I get at "Distance d (10, 11)"??
Distance d (10, 11);
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>How do I get at "Distance d (10, 11)"??

Like

Distance d (10, 11);

?

Yoiu have one constructor defined that takes

Distance (int feet, int inches);

so all you  need to do is performing the conversion in that constructor's body also - and maybe storing the variables passed in in their converted form...

Commented:
-- all you  need to do is performing the conversion in that constructor's body

Not sure where to start with this??  Sorry to be such a pain!!!
Top Expert 2007

Commented:

<G> couldn't resist phrasing it that way!

Commented:
Sure does... let me try it... be right back!
CERTIFIED EXPERT
Top Expert 2012

Commented:

By using the code you already have, i.e.

result = totalmm * MTM;

All you need to do is storing teh results for further use.

Commented:
I am still being difficult...

I did exactly as josgood suggested... It is the testing file I am having trouble with, now... SetDistance only takes one argument, a double...

Here is what I tried and it did not work...

Distance d (10 /*feet*/, 11/*inches*/);

double feet = 0;
d.SetDistance (10, 11);
cout << "Testing EtoM member function 10 feet, 11 inches equals "
<< d.EtoM (feet) << " millimeters\n\n";

Now this worked fine....

double miles = 0;
f.SetDistance (1.23);
cout << "Testing EtoM member function 1.23 miles equals "
<< f.EtoM (miles) << " millimeters\n\n";
CERTIFIED EXPERT
Top Expert 2012
Commented:
Both are inherently different, you are doing teo different this by using two different constructors here. And you still aren't storing any data (*hint*)
Top Expert 2007
Commented:
By constructing a Distance object with
Distance d (10 /*feet*/, 11/*inches*/);

you have set the instance variable totalmm to the total millimeters, as shown in the constructor code
Distance::Distance (int ft, int in) {
totalmm = EtoM(ft,in);
}

There is no need to call SetDistance, because the constructor has already set the instance variable..