Skip to content
doubtly

Implement a class temperature to convert degree Fahrenheit value to degree Celsius

Implement a class temperature to convert degree Fahrenheit value to degree Celsius value [Hint:  C/5 = F – 32 / 9 where C is temperature in degree Celsius and F is temperature in Fahrenheit) [March 2006, March 2009]
Doubtly Asked Updated Student doubt Save WhatsApp Report

1 answer

Best answer

Answer: Read F, compute \(C = \dfrac{5(F - 32)}{9}\) inside a class named temperature, and display C.

#include<iostream.h>
#include<conio.h>

class temperature
{
    private:
        float F, C;
    public:
        void getdata()
        {
            cout << "Enter temperature in Fahrenheit: ";
            cin >> F;
        }
        void convert()
        {
            C = 5.0 * (F - 32) / 9.0;
        }
        void display()
        {
            cout << "Temperature in Celsius = " << C;
        }
};

void main()
{
    temperature t;
    clrscr();
    t.getdata();
    t.convert();
    t.display();
    getch();
}

Explanation: from the hint \(\frac{C}{5} = \frac{F - 32}{9}\) we get \(C = \frac{5}{9}(F - 32)\). The class keeps F and C as private data, and the public member functions read F, convert it, and print C. Writing 5.0 and 9.0 keeps the division in floating point. Example: F = 212 gives C = 100.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.