C++ Program Temperature Conversion Using Class and Objects

By | July 9, 2019

Task: Write a C++ Program Temperature Conversion Using Class and Objects

C++ Program to design and use a Temperature class

C++ Program to design and use a Temperature class

Source Code For C++ Program Temperature Conversion Class

/* Write a C++ Program to design a class Temperature
with two float variables fahren and celsius
and a member function 'conversion' to convert 
fahrenheit into celsius
*/
#include<iostream>

using namespace std;

// define a class Temperature

class Temperature
{
	private:
	float fahren, celsius;
	public:
	float conversion(float f)
	{
		fahren=f;
		celsius=(fahren-32)* 5.0/9.0;
		return celsius;
	}
	
};
int main()
{
	// define an object of Temperature class
	Temperature t;
	float f;
	cout<<"Enter Temperature in Fahrenheit=";
	cin>>f;
	// call conversion function with object t
	cout<<"Temperature in Celsius="<<t.conversion(f);

return 0;
}

Output and Sample Run of C++ Program for Temperature conversion using class and objects, Object oriented Programming

Enter Temperature in Fahrenheit=98.6
Temperature in Celsius=37
--------------------------------
Process exited after 29.55 seconds with return value 0
Press any key to continue . . .

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *