CPP

CPP

Pointers to Members has 9 Questions

Which is referred by pointers to member?

static members of class objects
Non-static members of class objects
Refering to whole class
None of the mentioned
What should be used to point to a static class member?

Smart pointer
Dynamic pointer
Normal pointer
None of the mentioned
Which operator is used in pointer to member function?

.*
->*
Both a & b
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class X 
  4.     {
  5.         public:
  6.         int a;
  7. 		void f(int b) 
  8. 		{
  9. 			cout<< b << endl;
  10. 		}
  11.     };
  12.     int main() 
  13.     {
  14.         int X :: *ptiptr = &X :: a;
  15.         void (X :: * ptfptr) (int) = &X :: f;
  16.         X xobject;
  17.         xobject.*ptiptr = 10;
  18.         cout << xobject.*ptiptr << endl;
  19.         (xobject.*ptfptr) (20);
  20.     }

10 20
20 10
20
10
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class Car
  4.     {
  5.         public:
  6.         int speed;
  7.     };
  8.     int main()
  9.     {
  10.         int Car :: *pSpeed = &Car :: speed;
  11.         Car c1;
  12.         c1.speed = 1;           
  13.         cout << c1.speed << endl;
  14.         c1.*pSpeed = 2;     
  15.         cout  << c1.speed << endl;
  16.         return 0;
  17.     }

1
2
Both a & b
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class Foo
  4.     {
  5.         public:
  6.         Foo(int i = 0){ _i = i;}
  7.         void f()
  8.         {
  9.             cout << "Executed"<<endl;
  10.         }
  11.         private:
  12.         int _i;
  13.     };
  14.     int main()
  15.     {
  16.         Foo *p = 0;
  17.         p -> f();
  18.     }

Executed
Error
Runtime error
None of the mentioned
What is the operation for .*?

It combines the first operand and the second operand
It seperates the first operand and the second operand
It reduces the data size
None of the mentioned
Which is the best design choice for using pointer to member function?

Interface
Class
Structure
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class bowl 
  4.     {
  5.         public:
  6.         int apples;
  7.         int oranges;
  8.     };
  9.     int count_fruit(bowl * begin, bowl * end, int bowl :: *fruit)
  10.     {
  11.         int count = 0;
  12.         for (bowl * iterator = begin; iterator != end; ++ iterator)
  13.             count += iterator ->* fruit;
  14.         return count;
  15.     }
  16.     int main()
  17.     {
  18.         bowl bowls[2] = {{ 1, 2 },{ 3, 5 }};
  19.         cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: apples) << " apples\n";
  20.         cout << "I have " << count_fruit(bowls, bowls + 2, & bowl :: oranges) << " oranges\n";
  21.         return 0;
  22.     }

I have 4 apples I have 7 oranges
I have 3 apples I have 5 oranges
I have 1 apples I have 5 oranges
None of the mentioned