Programming Language & Its Applications — C++ Constructs with Objects and Classes, NEC licence examination syllabus (Nepal Engineering Council).
Several functions, one name, chosen by their arguments — resolved entirely at compile time.
cout << 42 and then cout << "hello" and then cout << 3.14, three different operator<< functions are being selected — that is overloading doing the work. In C the same job needs three differently-named functions: compare C's abs, labs, llabs, fabs, fabsf with C++'s single std::abs that handles all of them. Python solves this differently again (one function, runtime type checks), which is why len() works on everything. Search "C++ overload resolution rules" when you want the full algorithm — it is surprisingly intricate.#include <iostream>
#include <string>
/* same job, different argument shapes */
int area(int side) { return side * side; }
int area(int len, int wid) { return len * wid; }
double area(double r) { return 3.14159 * r * r; }
double area(double b, double h) { return 0.5 * b * h; }
void show(int i) { std::cout << "int " << i << "\n"; }
void show(double d) { std::cout << "double " << d << "\n"; }
void show(char c) { std::cout << "char " << c << "\n"; }
void show(const std::string& s) { std::cout << "string " << s << "\n"; }
int main() {
std::cout << "square side 5 = " << area(5) << "\n";
std::cout << "rect 4 x 6 = " << area(4, 6) << "\n";
std::cout << "circle r = 2.0 = " << area(2.0) << "\n";
std::cout << "triangle b=3 h=8 = " << area(3.0, 8.0) << "\n";
show(42); show(3.14); show('A'); show(std::string("hi"));
return 0;
}
Output:
square side 5 = 25
rect 4 x 6 = 24
circle r = 2.0 = 12.5664
triangle b=3 h=8 = 12
int 42
double 3.14
char A
string hi
#include <iostream>
void f(int) { std::cout << "f(int)\n"; }
void f(double) { std::cout << "f(double)\n"; }
void g(long) { std::cout << "g(long)\n"; }
void g(float) { std::cout << "g(float)\n"; }
int main() {
f(10); /* exact -> f(int) */
f(10.5); /* exact -> f(double) */
f('x'); /* promotion -> f(int) */
f(true); /* promotion -> f(int) */
f(10.5f); /* promotion -> f(double) */
/* g(10) is AMBIGUOUS: int->long and int->float are
BOTH standard conversions of equal rank.
Uncomment to see the error: */
/* g(10); error: call to 'g' is ambiguous */
g(10L); /* exact -> g(long) */
return 0;
}
Output:
f(int)
f(double)
f(int)
f(int)
f(double)
g(long)
Notice that f(10.5f) picks f(double), not f(int). It looks backwards — a float is "more like" an int in that both are numbers of the same size — but float→double is a promotion (rank 2, lossless) while float→int is a conversion (rank 3, lossy). The ranking is about information preserved, not about type similarity.
#include <iostream>
#include <cmath>
/* THE C WAY: distinct names, caller must pick correctly.
int abs(int);
long labs(long);
double fabs(double);
float fabsf(float);
Pick the wrong one and you get silent truncation:
fabs(-3.7) -> 3.7 but
abs(-3.7) -> 3 (argument truncated to int!) */
/* THE C++ WAY: one name, compiler picks */
int my_abs(int x) { return x < 0 ? -x : x; }
long my_abs(long x) { return x < 0 ? -x : x; }
double my_abs(double x) { return x < 0 ? -x : x; }
int main() {
std::cout << my_abs(-7) << "\n"; /* int */
std::cout << my_abs(-7L) << "\n"; /* long */
std::cout << my_abs(-3.7) << "\n"; /* double - EXACT */
/* the C trap, reproduced deliberately */
std::cout << "C-style abs(-3.7) would give "
<< my_abs(static_cast<int>(-3.7)) << "\n";
return 0;
}
Output:
7
7
3.7
C-style abs(-3.7) would give 3
area for square/rectangle/circle/triangle is the classic) and to trace which overload a given call selects. Know that overloading is compile-time (static) polymorphism and contrast it with runtime overriding.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loading…