Professional Documents
Culture Documents
Ex C++
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;
}
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;
}
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