InterviewSolution
| 1. |
Write Code To Add Functions, Which Would Work As Get And Put Properties Of A Class? |
|
Answer» This is shown in following code. #include class sample { int DATA ; public: __declspec ( property ( put = fun1, get = fun2 ) ) int x ; VOID fun1 ( int i ) { if ( i < 0 ) data = 0 ; else data = i ; } int fun2( ) { return data ; } } ; void main( ) { sample a ; a.x = -99 ; cout << a.x ; }Here, the function fun1( ) of class sample is used to set the given integer value into data, whereasfun2( ) returns the CURRENT value of data. To set these functions as properties of a class we havegiven the statement as shown below: __declspec ( property ( put = fun1, get = fun2 )) int x ;As a result, the statement a.x = -99 ; would cause fun1( ) to get CALLED to set the value in data. On the other hand, the last statement would cause fun2( ) to get called to return the value of data. This is shown in following code. Here, the function fun1( ) of class sample is used to set the given integer value into data, whereasfun2( ) returns the current value of data. To set these functions as properties of a class we havegiven the statement as shown below: As a result, the statement a.x = -99 ; would cause fun1( ) to get called to set the value in data. On the other hand, the last statement would cause fun2( ) to get called to return the value of data. |
|