Machine-translated by Google. Questions are authored in English.
Top

Discussion

class C {
public:
  void f(int a) {cout \(\lt\lt\) a;}
  void f(int a, int b) {cout \(\lt\lt\) a+b;};
  void f(int a, int b, int c) {cout \(\lt\lt\) a+b+c;}
};
int main( )
{
  C obj;
  obj.f(10);
  obj.f(10, 20);
  obj.f(10, 20, 30);
  return 0;
}

The above C++ source code exemplifies which of the following principle with respect to object-oriented programming ?

  • A.Abstraction
  • B.Encapsulation
  • C.Inheritance
  • D.Polymorphism

Answer: D

The code exemplifies polymorphism.

Class C defines three member functions with the same name f but different parameter lists. The compiler selects the right one from the arguments at each call, which is function overloading, a form of compile-time (static) polymorphism. The program prints 10, then 30, then 60.

Nothing here hides implementation details behind an interface (abstraction) or bundles data with access control as the point of the example (encapsulation), and there is no base or derived class, so it is not inheritance.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.