Public Member Functions

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 {
      return totalmm;
}
//==============================================//
//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;
}
*/
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.

jkrCommented:
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.
0
jkrCommented:
BTW, after removing one of these, it compiles fine.
0
josgoodCommented:
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 {
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Jaime OlivaresSoftware ArchitectCommented:
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);
0
gndcdospsAuthor 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;
}
0
gndcdospsAuthor Commented:
josgood - - Hang on... I think I may have figured it out.
0
jkrCommented:
>>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.
0
josgoodCommented:
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;
}
0
gndcdospsAuthor 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 {
// **need to ask about this - Set Distance is for double values
      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);
0
jkrCommented:
>>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...
0
gndcdospsAuthor 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!!!
0
josgoodCommented:
I think my last answer answers your last question.

<G> couldn't resist phrasing it that way!
0
gndcdospsAuthor Commented:
Sure does... let me try it... be right back!
0
jkrCommented:
>>Not sure where to start with this??

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

result = totalmm * MTM;

All you need to do is storing teh results for further use.
0
gndcdospsAuthor 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";
0
jkrCommented:
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*)
0
josgoodCommented:
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..
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
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.