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 4 of 4.
Kalpana said:
1 decade ago
What is transient and pseudo polymorphism ? and it is a type polymorphism ? then explain more about Ad-hoc polymorphism ?
Rupali said:
1 decade ago
What is mean by transient and pseudo polymorphism ?
Sanil said:
1 decade ago
Can anyone explain more about Ad-hoc polymorphism, transient and psuedo polymorphism ?
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;
}
Varsha said:
1 decade ago
What is meant by Ad-hoc polymorphism, transient and psuedo polymorphism.
Vicky said:
1 decade ago
What is meant by Ad-hoc polymorphism, transient and psuedo polymorphism?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers