Data Conversion
1. Conversion
between Basic to User Defined Data Types
/*
conversion from basic data type to user defined data type */
/*
to convert NPR to Dollar */
#include<iostream>
#include<conio.h>
using
namespace std;
class
conv{
double dol,cent;
public:
conv()
{
}
conv(float fnpr)
{
dol=fnpr/103.7;
cent=int((fnpr/103.7-int(fnpr/130.7))*100);
dol=int(fnpr/103.7);
}
void show()
{
cout<<dol<<"$
"<<cent<<" Cent";
}
};
int
main()
{
conv dollar;
float npr;
cout<<"NPR:"<<endl;
cin>>npr;
dollar=npr;
dollar.show();
getch();
}
2. Conversion
from User Defined to Basic Data Types
/*
conversion from user defined data type to basic data type */
#include<iostream>
#include<conio.h>
using
namespace std;
class
dollar{
float dol, cent;
public:
dollar(int d=0, int c=0)
{
dol=d;
cent=c;
}
operator float()
{
return((dol+cent/100)*103.7);
}
};
int
main()
{
dollar d1(3,40);
float np;
np=d1;
cout<<"Money in
NPR::"<<np;
}
Comments
Post a Comment