You are on page 1of 2

Ex_C++.

txt
/*Write a program using two classes Fahrenheit with data member
f and Celsius with data member c. Overload the operator << and
>> in both class to handle the input and output. Write the
conversion routine in Celsius class that converts the Fahrenheit
into Celsius. Show your conversion result in main(). [Formula :
C=(F-32)*5/9]*/
#include<iostream.h>
#include<conio.h>

class Fahrenheit
{
private:
float f;
public:
Fahrenheit(){f=0;}
friend istream & operator >> (istream & din,Fahrenheit & F)
{
din>>F.f;
return din;
}

friend ostream & operator << (ostream & dout,Fahrenheit & F)


{
dout<<F.f<<" degree Fahrenheit."<<endl;
return dout;
}

float getf() //to convert into celsius in Celsius class


{
return f;
}
};

class Celsius
{
private:
float c;

Página 1
Ex_C++.txt
public:
Celsius(){c=0;}
Celsius(Fahrenheit F) //constructor for conversion
{
c=(F.getf()-32)*5/9;
}

friend istream& operator >> (istream& din,Celsius& C)


{
din>>C.c;
return din;
}
friend ostream& operator << (ostream& dout,Celsius& C)
{
dout<<C.c<<" degree Celsius."<<endl;
return dout;
}
};

void main()
{
clrscr();
Celsius c1;
cout<<"Input value of c1 : ";
cin>>c1;
cout<<c1;
Fahrenheit f1;
cout<<"Input value of f1 : ";
cin>>f1;
cout<<f1;
cout<<"Converting f1 to c1 .... "<<endl;
c1=f1;
cout<<endl<<f1<<" = ";
cout<<c1;
getch();
}

Página 2

You might also like