Tuesday, 21 April 2015

Write a program that will demonstrate class type conversion to built in type

Write a program that will demonstrate class type conversion to built in type. The program that uses the following properties – class names- dbc; three integer type private member variables- x,y,z; one constructor function dbc(); one conversion function operator int(); one member function show(). dbc() is using to input the value of x,y; operator int() is using to multiply the value of z=x*y and convert class object to int type; Suppose in main(), obj is an object and m is an int type variable. Then the object obj is converted to int type variable m. show() is using to display the value of z as output using the obj.

#include<iostream>
using namespace std;
class dbc
{
    int x,y,z;
public:
    dbc();
    operator int();
    void show();
};
dbc::dbc()
{
    cout<<"Enter the value of x & y is  "<<endl;
    cout<<"Input the value x: ";
    cin>>x;
    cout<<"Input the value y: ";
    cin>>y;
}
dbc::operator int()
{
    z=x*y;
    return z;
}
void dbc::show()
{
    cout<<"The multiplication of Z is : "<<z<<endl;
}
int main()
{
    dbc obj;
    int m;
    m=obj;
    obj.show();
    return 0;
}

No comments:

Post a Comment