Freezing & Boiling Points (Temperature Class) in C++
Posted by Samath
Last Updated: January 18, 2024

The following table lists the freezing and boiling points of several substances.

Design a class that stores a temperature in a temperature member variable and has the appropriate getter and setter functions. In addition to appropriate constructors, the class should have the following member functions:

Write a program (driver.cpp) that demonstrates the class. The program should ask the user to enter different temperatures, and then display a list of the substances that will freeze at that temperature and those that will boil at that temperature. 

Temperature.h 

#ifndef TEMPH
#define TEMPH
 
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
class Temperature
{
      public:
             Temperature(int temp);
			 bool isEthylFreezing();
			 bool isEthylBoiling();
			 bool isOxygenFreezing();
			 bool isOxygenBoiling();
			 bool isWaterFreezing();
			 bool isWaterBoiling();
			 void setTemperature(int temp);
			 int getTemperature();
             
             
      private:
		  int temperature;
		  

};
 
#endif

Temperature.cpp 

#include <iostream>
#include <cstdlib>
#include "Temperature.h"
 
using namespace std;
 
             Temperature::Temperature(int temp)
             {
                  temperature = temp; 
             }


			 bool Temperature::isEthylFreezing()
			 {
				 if(temperature<=-173)
				 {
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			 }

			 bool Temperature::isEthylBoiling()
			 {
				 if(temperature>=172)
				 {
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			 }
			 bool Temperature::isOxygenFreezing()
			 {
				 if(temperature<=-362)
				 {
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			 }

			 bool Temperature::isOxygenBoiling()
			 {
				 if(temperature>=-306)
				 {
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			 }

			 bool Temperature::isWaterFreezing()
			 {
				 if(temperature<=32)
				 {
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			 }

			 bool Temperature::isWaterBoiling()
			 {
				 if(temperature>=212)
				 {
					 return true;
				 }
				 else
				 {
					 return false;
				 }
			 }

			 void Temperature::setTemperature(int temp)
			 {
				 temperature = temp;
			 }

			 int Temperature::getTemperature()
			 {
				 return temperature;
			 }

driver.cpp 

#include <cstdlib>
#include <iostream>
#include "Temperature.h"
 
using namespace std;
 
int main(int argc, char *argv[])
{
    
   int my_temp;
   cout<<"Enter a Temperature: ";
   cin>>my_temp;

   Temperature temp(10);
   temp.setTemperature(my_temp);

   if(temp.isEthylFreezing())
   {
	   cout<<"Ethyl Alcohol will Freeze at this Temperature.\n";
   }

   if(temp.isOxygenFreezing())
   {
	   cout<<"Oxygen will Freeze at this Temperature.\n";
   }

   if(temp.isWaterFreezing())
   {
	   cout<<"Water will Freeze at this Temperature.\n";
   }

   if(temp.isEthylBoiling())
   {
	   cout<<"Ethyl Alcohol will Boil at this Temperature.\n";
   }

   if(temp.isOxygenBoiling())
   {
	   cout<<"Oxygen will Boil at this Temperature.\n";
   }

   if(temp.isWaterBoiling())
   {
	   cout<<"Water will Boil at this Temperature.\n";
   }

    system("PAUSE");
    return 0;
}
Related Content
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024