C++ Programming Questions and Answers – Pointers to Members

«
»
This section on C++ language interview questions and answers focuses on “Pointers to Members”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ language interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions on “Pointers to Members” along with answers, explanations and/or solutions:

1. Which is referred by pointers to member?
a) Static members of class objects
b) Non-static members of class objects
c) Referring to whole class
d) Dynamic members of class objects
View Answer

Answer: b
Explanation: We cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.

2. What should be used to point to a static class member?
a) Smart pointer
b) Dynamic pointer
c) Normal pointer
d) Static pointer
View Answer

Answer: c
Explanation: Normal pointer is sed to point to a static class member.

3. Which operator is used in pointer to member function?
a) .*
b) ->*
c) Both .* & ->*
d) $*
View Answer

Answer: c
Explanation: The pointer to member operators .* and ->* are used to bind a pointer to a member of a specific class object.

4. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  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.     }

a)

   10
   20

b)

   20
   10

c) 20
d) 10
View Answer

Answer: a
Explanation: In this program, We are assigning 10 and printing it in the
main function and then for value 20, We are passing the value to class and
printing it.
Output:

$ g++ ptm.cpp
$ a.out
10
20

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     class Testpm 
  4.     {
  5.         public:
  6.         void m_func1() 
  7.         { 
  8.             cout << "func1\n";
  9.         }
  10.         int m_num;
  11.     };
  12.     void (Testpm :: *pmfn)() = &Testpm :: m_func1;
  13.     int Testpm :: *pmd = &Testpm :: m_num;
  14.     int main() 
  15.     {
  16.         Testpm ATestpm;
  17.         Testpm *pTestpm = new Testpm;
  18.         (ATestpm.*pmfn)();
  19.         (pTestpm ->* pmfn)();
  20.         ATestpm.*pmd = 1;
  21.         pTestpm ->* pmd = 2;
  22.         cout << ATestpm.*pmd << endl
  23.         << pTestpm ->* pmd << endl;
  24.     }

a) func1
b)

   func1
   func1

c)

   1
   2

d)

   func1
   func1
   1
   2
View Answer
Answer: d
Explanation: In this program, As we are passing the value twice to the method
in the class, It is printing the func1 twice and then it is printing the given
value.
Output:

$ g++ ptm1.cpp
$ a.out
func1
func1
1
2
 
 

advertisement

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

a) 1
b) 2
c) Both 1 & 2
d) 4
View Answer

Answer: c
Explanation: In this program, We are printing the value by direct access and another one by using pointer to member.
Output:

$ g++ ptm2.cpp
$ a.out
1
2

7. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

a)

   I have 4 apples
   I have 7 oranges

b)

advertisement
   I have 3 apples
   I have 5 oranges

c)

   I have 1 apples
   I have 5 oranges

d)

   I have 1 apples
   I have 7 oranges
View Answer
Answer: a
Explanation: In this program, We are passing the value to the class and adding the values and printing it in the main.
Output:

$ g++ ptm3.cpp
$ a.out
I have 4 apples
I have 7 oranges
 
 

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  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.     }

a) Executed
b) Error
c) Runtime error
d) 10
View Answer

Answer: a
Explanation: In this program, We passes the value to the class and printing it.
Output:

$ g++ ptm4.cpp
$ a.out
Executed

9. Which is the best design choice for using pointer to member function?
a) Interface
b) Class
c) Structure
d) Block
View Answer

Answer: a
Explanation: Interface is the best design choice for using pointer to member function.

10. What is the operation for .*?
a) It combines the first operand and the second operand
b) It separates the first operand and the second operand
c) It reduces the data size
d) It combines the first operand and the second operand and terminates third operand
View Answer

Answer: a
Explanation: The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn