C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 18)
18.
Which of the following correctly describes overloading of functions?
Discussion:
36 comments Page 1 of 4.
Aniket said:
1 decade ago
Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two ints and the + operator, it adds them together. Given two std::strings it concatenates them together. This is called overloading.
Here is a concrete example that implements function add for ints and strings,
#include <iostream>
#include <string>
int add(int a, int b) {
return a + b;
}
std::string add(const char *a, const char *b) {
std::string result(a);
result += b;
return result;
}
int main() {
std::cout << add(5, 9) << std::endl;
std::cout << add("hello ", "world") << std::endl;
}
Ad-hoc polymorphism also appears in C++ if you specialize templates. Returning to the previous example about max function, here is how you'd write a max for two char *,
template <>
const char *max(const char *a, const char *b) {
return strcmp(a, b) > 0 ? a : b;
}
Here is a concrete example that implements function add for ints and strings,
#include <iostream>
#include <string>
int add(int a, int b) {
return a + b;
}
std::string add(const char *a, const char *b) {
std::string result(a);
result += b;
return result;
}
int main() {
std::cout << add(5, 9) << std::endl;
std::cout << add("hello ", "world") << std::endl;
}
Ad-hoc polymorphism also appears in C++ if you specialize templates. Returning to the previous example about max function, here is how you'd write a max for two char *,
template <>
const char *max(const char *a, const char *b) {
return strcmp(a, b) > 0 ? a : b;
}
Sudhanshu said:
1 decade ago
The Four Polymorphisms in C++.
When people talk about polymorphism in C++ they usually mean the thing of using a derived class through the base class pointer or reference, which is called subtype polymorphism. But they often forget that there are all kinds of other polymorphisms in C++, such as parametric polymorphism, ad-hoc polymorphism and coercion polymorphism.
These polymorphisms also go by different names in C++,
Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two 'int' values and the '+' operator, it adds them together. Given two 'std::strings' it concatenates them together. This is called overloading.
When people talk about polymorphism in C++ they usually mean the thing of using a derived class through the base class pointer or reference, which is called subtype polymorphism. But they often forget that there are all kinds of other polymorphisms in C++, such as parametric polymorphism, ad-hoc polymorphism and coercion polymorphism.
These polymorphisms also go by different names in C++,
Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two 'int' values and the '+' operator, it adds them together. Given two 'std::strings' it concatenates them together. This is called overloading.
Sanjay nannaware said:
9 years ago
The Four Polymorphisms in C++.
When people talk about polymorphism in C++ they usually mean the thing of using a derived class through the base class pointer or reference, which is called subtype polymorphism. But they often forget that there are all kinds of other polymorphisms in C++, such as parametric polymorphism, ad-hoc polymorphism and coercion polymorphism.
These polymorphisms also go by different names in C++,
Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two 'int' values and the '+' operator, it adds them together. Given two 'std::strings' it concatenates them together. This is called overloading.
When people talk about polymorphism in C++ they usually mean the thing of using a derived class through the base class pointer or reference, which is called subtype polymorphism. But they often forget that there are all kinds of other polymorphisms in C++, such as parametric polymorphism, ad-hoc polymorphism and coercion polymorphism.
These polymorphisms also go by different names in C++,
Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two 'int' values and the '+' operator, it adds them together. Given two 'std::strings' it concatenates them together. This is called overloading.
(3)
Gelu Menumorut said:
9 years ago
It seems like pseudo and transient polymorphisms are only made-up word associations that sound like something real, but they are not? Could they be also: dual or multi polymorphism, virtual polymorphism (nobody asks about it, hmmm), constant or mutable polymorphism, friendly polymorphism, volatile polymorphism, atomic polymorphism, mutual polymorphism, you can name it in too many combinations.
Shweta said:
1 decade ago
In programming languages, ad-hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. It is also known as function overloading or operator overloading.
Lovish said:
9 years ago
These polymorphisms also go by different names in C++,
- Subtype polymorphism is also known as runtime polymorphism.
- Parametric polymorphism is also known as compile-time polymorphism.
- Ad-hoc polymorphism is also known as overloading.
- Coercion is also known as (implicit or explicit) casting.
- Subtype polymorphism is also known as runtime polymorphism.
- Parametric polymorphism is also known as compile-time polymorphism.
- Ad-hoc polymorphism is also known as overloading.
- Coercion is also known as (implicit or explicit) casting.
Sam said:
4 years ago
Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
(7)
Ankita said:
1 decade ago
Ad hoc polymorphism is also known as operator overloading.
Ad hoc polymorphism is a specific case of polymorphism, where different operators have different implementations depending on their arguments.
Ad hoc polymorphism is a specific case of polymorphism, where different operators have different implementations depending on their arguments.
Gopal said:
1 decade ago
Ad-hoc means compiled time polymorphism in which two things operator and function overloading means same name different argument at time or different work.
Pavithra said:
1 decade ago
Ad-hoc polymorphism behaves differently for different data types with same name.
Can anyone explain about pseudo polymorphism and transient polymorphism?
Can anyone explain about pseudo polymorphism and transient polymorphism?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers