x

Choose Country Code

x

Direction

x

Ask a Question

  • Ask a Question
  • Scan a Question
  • Post MCQ
  • Note: File extension must be of jpg, jpeg, png, bmp format and file size must not exceed 5 MB
x

Ask a Question

x

x
x
x
Hire a Tutor

Answers and Solutions

What's Your Question?
Answer

Polymorphism is a key feature of object-oriented programming that allows a function or object to perform in different ways depending on how it is used[1][3][6]. In C++, polymorphism is achieved through inheritance, overriding, and overloading[6]. 

In simple terms, polymorphism allows a single function to be used for multiple purposes. This is achieved through two types of polymorphism in C++: compile-time polymorphism and runtime polymorphism[3]. 

- **Compile-time polymorphism**: This type of polymorphism is achieved through function overloading or operator overloading. Function overloading allows multiple functions with the same name to be defined, but with different parameters[1][3]. Operator overloading allows operators such as + and - to be redefined for user-defined types[6].

- **Runtime polymorphism**: This type of polymorphism is achieved through function overriding. Function overriding allows a derived class to provide its own implementation of a virtual function that is already defined in the base class[1][3][6]. 

A real-life example of polymorphism is a person who exhibits different behavior in different situations. For instance, a man can be a father, a husband, and an employee at the same time, and he exhibits different characteristics in each of these roles[1]. 

In C++, a class that declares or inherits a virtual function is called a polymorphic class[5]. The virtual keyword is used to declare a virtual function in the base class, and the same function can be overridden in the derived class[5]. 

Polymorphism is an important concept in C++ programming that allows for code reuse and more streamlined programs[6].

 

Answer

Polymorphism means taking more than one form.

Eg: A person can act as a son to his parents

Same person act as a Student in his school

Same person act as a brother to his younger one.

Same person act as a customer when he goes to shop, etc

Person is same but he taken many forms.

Similarly the function name will be same but it can have different no. of arguments, different data types in arguments, different return types.

int function()

int function(int a)

int function(int a, int b)

float function(int a, float b), etc

Answer

"Poly" in Polymorphism is a Greek word which means many. So, in OOPs, "polymorphism" is the representation of a single function name with different number or sequence of parameters. For example,

  • void fun(int a);
  • void fun(char a);
  • void fun(int a, int b);
  • void fun(int a, long b);
  • void fun(long a, int b);

all of the above declared functions are identical/different.

 

Return type does not matter in the case of polymorphism. For example,

  • void fun(int a);
  • int fun(int a);
  • void fun(int a, int b);
  • int fun(int a, int b);

all the functions declared here will be treated as same.

 

In C++ polymorphism is of three type,

  • Function Overloading,
  • Function overriding and
  • Operator Overloading.
Answer
Polymorphism means many forms. For example a function can have different forms regarding parameters passed or return type. Eg void fn(int a, int b) Or char fn(char a, char b)

Post Answer and Earn Credit Points

Get 5 credit points for each correct answer. The best one gets 25 in all.

Post Answer