Get Website Learn Coding Tech Story

C++ Programming Examples

/* Temperature converter */
#include<iostream>
#include<iomanip>
using namespace std;

class converter {
	float celsius;
	public:
		converter(float fahrenheit) {
			celsius = (fahrenheit - 32) * 5/9;
		}

		void display_celsius() {

			/* 'setprecision(2)' use to print upto two decimal point */
			cout << "Celsius: " << setprecision(2) << celsius << endl;
		}
};

int main() {
	int fahrenheit;
	cout << "Program to convert Fahrenheit to Celsius" << endl;
	cout << "Enter Temperature in Fahrenheit:" << endl;
	cin >> fahrenheit;
	
	converter temperature(fahrenheit), *temp_pointer;

	/* Store address of the 'temperature' object into pointer 'temp_pointer' */
	temp_pointer = &temperature;
	
	/* Access member function using member access operator  */
	temp_pointer->display_celsius();
	return 0;
}


          /****** Output ******/
          

Program to convert Fahrenheit to Celsius


Enter Temperature in Fahrenheit:


80


Celsius: 27

Learn more about Pointer