Data Conversion using Type Operator


#include <iostream.h>
#include <string.h>
#include<conio.h>
/*----------------------Class definition-------------------------*/
class data
{
        int no_of_items;
        float cost_per_item;
    public:
        data(int, float);

        // type operator
        operator float() {return (no_of_items * cost_per_item);}
};
data::data(int a, float b)
{
    no_of_items = a;
    cost_per_item = b;
}
/*----------------------Definition ends here---------------------*/
void main(void)
{
    clrscr();
    data d(10, 2.5);

    cout << "The result returned by the type operator : ";
    cout << d.operator float() << "\n";
    getch();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|