Friday, June 28, 2024

Temperature Converter Using Encapsulation in C++

 




#include <iostream>


class TemperatureConverter {

private:

    double celsius;

    double fahrenheit;


public:

    TemperatureConverter() : celsius(0.0), fahrenheit(32.0) {}


    void setCelsius(double c) {

        celsius = c;

        fahrenheit = (celsius * 9.0/5.0) + 32.0;

    }


    void setFahrenheit(double f) {

        fahrenheit = f;

        celsius = (fahrenheit - 32.0) * 5.0/9.0;

    }


    double getCelsius() const {

        return celsius;

    }


    double getFahrenheit() const {

        return fahrenheit;

    }

};


int main() {

    TemperatureConverter converter;


     std::cout << "\n\tTemperature Converter Using Encapsulation in C++\n\n";

    // Convert from Celsius to Fahrenheit

    converter.setCelsius(35.0);

    std::cout << "\t35 degrees Celsius is equal to " << converter.getFahrenheit() << " degrees Fahrenheit.\n";


    // Convert from Fahrenheit to Celsius

    converter.setFahrenheit(112.3);

    std::cout << "\n\tEnd of Program\n\n";

    return 0;

}


No comments:

Post a Comment