Tuesday, 21 April 2015

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

Write a program that will demonstrate built in type conversion to class type. The program that uses the following properties – class names- test, one integer type private member variables- a,one constructor function test(int x), one member function output(). Suppose inside main(), object is ob(0) and value is one int type variable. We like to insert the amount of value from the keyboard. Then this int type value will be converted to the object ob. output() will display the value of object as output.

#include<iostream>
using namespace std;
class test
{
    int a;
public:
    test(int x);
    void output();
};
test::test(int x)
        {
            a=x;
        }
void test::output()
            {
              cout<<"The value of object is : "<<a<<endl;
            }
int main()
{
    test ob(0);
    int value;
    cout<<"Enter the integer value: ";
    cin>>value;
    ob=value;
    ob.output();
    return 0;
}

No comments:

Post a Comment