Object Pointer

/* 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

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!