Programming Language & Its Applications β C++ Constructs with Objects and Classes, NEC licence examination syllabus (Nepal Engineering Council).
Values the compiler supplies when the caller omits them β one function serving several call shapes.
std::vector<int> v(10) versus std::vector<int> v(10, -1) β the second argument defaults to a value-initialised element. Every graphics call like drawCircle(x, y, radius, colour = black, filled = false) works this way, so simple uses stay short while advanced uses stay possible. Python's def f(a, b=10) is the same feature, but with one famous difference worth knowing: Python evaluates the default once at definition, which is why def f(x, lst=[]) is a notorious bug, while C++ evaluates it at each call. Search "Python mutable default argument" and you will appreciate C++'s choice.#include <iostream>
#include <string>
/* one function, four usable call shapes */
double interest(double principal,
double rate = 10.0,
int years = 1,
int compounds_per_year = 1);
double interest(double p, double r, int y, int n) {
double amount = p;
for (int i = 0; i < y * n; i++)
amount *= (1 + r / (100.0 * n));
return amount - p;
}
void banner(const std::string& text,
char fill = '-', int width = 40) {
std::cout << std::string(width, fill) << "\n"
<< text << "\n"
<< std::string(width, fill) << "\n";
}
int main() {
std::cout << interest(10000) << "\n";
std::cout << interest(10000, 12.0) << "\n";
std::cout << interest(10000, 12.0, 2) << "\n";
std::cout << interest(10000, 12.0, 2, 4) << "\n";
banner("Defaults");
banner("Custom fill", '=');
banner("Narrow", '*', 20);
return 0;
}
Output:
1000
1200
2544
2668.24
----------------------------------------
Defaults
----------------------------------------
========================================
Custom fill
========================================
********************
Narrow
********************
interest(10000) compiles to a call passing all four values β 10000, 10.0, 1, 1 β with the compiler filling in the last three. The function body never knows an argument was omitted, which is exactly why there is no way to ask "did the caller supply this?" the way you can in Python with a None sentinel.
#include <iostream>
/* APPROACH A: three overloads - three function bodies */
int volumeA(int l) { return l * l * l; }
int volumeA(int l, int w) { return l * w * w; }
int volumeA(int l, int w, int h) { return l * w * h; }
/* APPROACH B: one function, defaults - ONE body */
int volumeB(int l, int w = 1, int h = 1) { return l*w*h; }
int main() {
std::cout << volumeA(3) << " " << volumeA(3,4)
<< " " << volumeA(3,4,5) << "\n";
std::cout << volumeB(3) << " " << volumeB(3,4)
<< " " << volumeB(3,4,5) << "\n";
return 0;
}
Output:
27 48 60
3 12 60
#include <iostream>
void f(int a, int b = 10) { std::cout << "two-param " << a+b << "\n"; }
void f(int a) { std::cout << "one-param " << a << "\n"; }
int main() {
f(5, 20); /* only one candidate: the 2-param version */
/* f(5) matches BOTH:
f(int) exactly
f(int, int=10) using the default
-> error: call to 'f' is ambiguous */
/* f(5); COMPILE ERROR */
return 0;
}
Output:
two-param 25
Mixing defaults and overloading on the same parameter count creates an ambiguity the compiler cannot resolve β and it is a compile error, which is the good outcome. Pick one mechanism per function name.
#include <iostream>
#include <string>
class Account {
std::string owner;
double balance;
double rate;
public:
/* ONE constructor covering three usage patterns.
Without defaults this needs three constructors. */
Account(std::string o, double b = 0.0, double r = 5.0)
: owner(o), balance(b), rate(r) {}
void show() const {
std::cout << owner << ": Rs " << balance
<< " at " << rate << "% -> after 1 yr Rs "
<< balance * (1 + rate/100) << "\n";
}
};
int main() {
Account a("Ram"); /* 0.0, 5.0 */
Account b("Sita", 50000); /* rate 5.0 */
Account c("Hari", 100000, 8.5); /* all given */
a.show(); b.show(); c.show();
return 0;
}
Output:
Ram: Rs 0 at 5% -> after 1 yr Rs 0
Sita: Rs 50000 at 5% -> after 1 yr Rs 52500
Hari: Rs 100000 at 8.5% -> after 1 yr Rs 108500
createWindow(800, 600, true, false, true, 2) is unreadable β you cannot tell which boolean is which. The modern answer is a parameter struct: createWindow({.width=800, .height=600, .fullscreen=true}) using C++20 designated initialisers. Search "named parameter idiom C++" to see the three competing solutions and why the language still has not settled this β it is a good window into how real language design trade-offs work.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β¦