Thread: Thermometer Class

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Thermometer Class

    i need to creat a themometer that can show the temperature in degrees C and in Degrees F depending on what the user puts in.

    i am very new with clases and i am wondering if i am on the right tracks with this begining code?

    Code:
    //CLASSES
    
    #include <iostream>
    using namespace std;
    
    class C_Themometer
    {
    private:
    float mDegreesC, mDegreesF;
    
    public:
    int SetDegreesC(float DegreesC, float DegreesF)
    {
    mDegreesC = DegreesC;
    mDegreesF = DegreesF;
    
    }
    
    
    void SetDegreesF()
    {
    
    
    }
    
    int GetDegreesC(float& DegreesC, float& DegreesF)
    {
    cout <<"Please enter a temperature";
    return mDegreesC = (9.0/5.0 * mDegreesC) + (32.0);
    }
    
    
    void GetDegreesF()
    {
    
    
    }
    };
    
    
    void main()
    {
    C_Themometer Deg1, Deg2;
    int mDegreesC;
    int mDegreesF;
    
    
    system("pause");
    }
    if anyone can give me a few pointers that will help then it would be very helpfull.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here are a few pointers:
    Code:
    int *degrees;
    double *celsius;
    float *fahenheit;
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    1. What you are writing isn't really a thermometer class, it's a temperature conversion class.

    2. If you are asking the user to input the temperature (as inGetDegreesC) you need to tell them whether the temperature they are entering is supposed to be in Celsius or Farenheit.

    3. If the user is supplying the temperature to be converted each time, you don't have to store it.

    4. Do you really need a class? What you are tring to achieve is really 2 simple functions. If you want to use a class, ask yourself what "extra" value the class can give you.

    5. You could always expand whatever you do to include (say) Kelvin.

  4. #4
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yes we need to put it into a class.

    here is the question we have been asked. might make a bit more sence.

    Code:
    Write a class called C_Thermometer.  
    This is based on the solution to a similar question in the first set of lab exercises. 
     The class needs to know everything it needs required to support itself as a thermometer.  
    For example, it needs to store the temperature in degrees Celsius.  
    You should write member functions SetDegreesC, SetDegreesF, GetDegreesC and GetDegreesF.  
    Write a program to test your class.  
    You need to think about appropriate function parameters and return values.

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    OK, so your class only need store the temperature in Celsius (as a private data member). The SetDegreesC member function should simply save its argument into the private data member. The GetDegreesC member function should simply return the value of the private data member. The SetDegreesF member function should convert its argument from F->C before saving it into the private data member. The GetDegreesF member function should return the private data member converted C->F. You do not need to ever store the temperature in F.

    The question is unclear as to what the initial state of the thermometer class should be, so your constructor could either be a default one (initializing temperature in Celsius to (say) zero), or one that takes an argument (the initial temperature in C) or you could write both of course!

  6. #6
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i think the second part of the question that we have to do next asks us to set it to zero (default constructor).

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Now all you have to do is write something like this:
    Code:
    Deg1.GetDegreesC(mDegreesC, mDegreesF);
    Deg1.SetDegreesC(mDegreesC,mDegreesF);
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I don't understand why you are making such strange functions, but I would do a thermometer like this:
    Code:
    #include <iostream>
    
    class Thermometer{
        private:
            double mDegreesC;
        public:
            Thermometer(){
                mDegreesC=0.00f;
            }
            void SetDegreesC(double DegreesC){
                mDegreesC = DegreesC;
                return;
            }
            void SetDegreesF(double DegreesF){
                mDegreesC = (double)100/(double)180*(DegreesF-(double)32);
                return;
            }
            double GetDegreesC(){
                return mDegreesC;
            }
            double GetDegreesF(){
                return 1.8*mDegreesC+32;
            }
    };
    
    int main(void){
        Thermometer therm1;
        char buffer[256];
        std::cout<<"Enter a temperature in Celsius:\n";
        std::cin.getline(buffer,256,'\n');
        therm1.SetDegreesC(strtod(buffer,NULL));
        std::cout<<"It is "<<therm1.GetDegreesF()<<" in Fahrenheit.\n";
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    brain
    Code:
    int *degrees;
    double *celsius;
    float *fahenheit;
    off topic but why not just int all of the temps or double all of the temps or float all of the temps? Keep them all the same? Why did you do it like that? thank you

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    I think it was supposed to be sarcasm.

  11. #11
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah that is the right type of program but i also need it to do it backwards to, from degrees C to degrees F and then Degrees F to degrees C.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    With my class, you can do that.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    how can i get it to output the other way round as it stops after it has figured out degreesF?

    will i just need to call [code[therm1.SetDegreesF(strtod(buffer,NULL));[/code]

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This asks for Celsius and then outputs Fahrenheit degrees:
    Code:
        therm1.SetDegreesC(strtod(buffer,NULL));
        std::cout<<"It is "<<therm1.GetDegreesF()<<" in Fahrenheit.\n";
    but this asks for Fahrenheit and then outputs Celsius degrees:
    Code:
        therm1.SetDegreesF(strtod(buffer,NULL));
        std::cout<<"It is "<<therm1.GetDegreesC()<<" in Celsius.\n";
    Notice that you only need to replace C with F and reverse.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  15. #15
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah thanks i got it but i have changed the code a bit to make it different but i have a problem with the outcome of DegreesF.

    i dont know how to change the equation in the GetDegreesF() function so it will output the correct answer because at the moment it just goes up i am using an example temperature of 30 so in degreesf it is 86 but then when i convert it back it goes to 97 or something like that.

    here is the code i have and changed.

    Code:
    #include <iostream>
    using namespace std;
    
    class Thermometer{
        private:
            double mDegreesC;
        public:
            Thermometer()
            {
                mDegreesC;
            }
            
            void SetDegreesC(double DegreesC)
            {
                mDegreesC = DegreesC;
                return;
            }
            
            void SetDegreesF(double DegreesF)
            {
                mDegreesC = 9.0/5.0*(DegreesF-32.0);
                return;
            }
            
            double GetDegreesC()
            {
                return mDegreesC;
            }
            
            double GetDegreesF()
            {
                return 1.8*mDegreesC+32;
            }
    };
    
    int main(void){
        Thermometer therm1, therm2;
        
       float input;
    
       cout<<"Please enter a value for degrees C: \n";
       cin>>input;
       therm1.SetDegreesC(input);
        cout<<"It is "<<therm1.GetDegreesF()<<" in Fahrenheit.\n";
        
        cout<<"Please enter a value for degrees F: \n";
        cin >>input;
        therm2.SetDegreesF(input);
        cout <<therm2.GetDegreesC();
        
        system("pause");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM