-->

C++ Tutorial: Some Lab assignments on Objects and Classes

C++ Tutorial: Some Lab assignments on Objects and Classes
    Question: Write a simple program that convert the temperature in degree Celsius to degree Fahrenheit and vice versa using the basic concept of class and object. Make separate class for Centigrade and Fahrenheit which will have the private member to hold the temperature value and make conversion functions in each class for conversion from one to other. For example you will have function toFahrenheit() in class Celsius that converts to Fahrenheit scale and returns the value.
    Solution:
    #include <iostream>
    using namespace std;
    class Celcius{
    private:
    float c;
    public:
    void readCelcius(){
    cout<<"Enter temperature in Celcius: ";
    cin>>c;
    }
    float toFarenheit(){
    return((9.0/5.0)*c+32);
    }
    };
    class Farenheit{
    private:
    float f;
    public:
    void readFarenheit(){
    cout<<"Enter temperature in Farenhiet: ";
    cin>>f;
    }
    float toCelcius(){
    return((5.0/9.0)*(f-32));
    }
    };
    int main(){
    Celcius c1;
    Farenheit f1;
    c1.readCelcius();
    cout<<c1.toFarenheit()<<" F"<<endl;
    f1.readFarenheit();
    cout<<f1.toCelcius()<<" C";
    return 0;
    }

    Read more »
    fardi zayden
    @مرسلة بواسطة
    كاتب ومحرر اخبار اعمل في موقع دراسات تقنية .

    إرسال تعليق