SECTION 2: Function Overloading, Defaults, Inline, Constants Flashcards

(8 cards)

1
Q

What is function overloading in C++?

A

Having multiple functions with the same name but different parameter types or counts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are three valid ways to overload functions in C++?

A

Different parameter types

Different number of parameters

Different parameter sequence

void add(int a, int b);
void add(double a, double b);
void add(int a, double b);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a default function parameter?

A

A value automatically used if no argument is passed for that parameter.
void greet(string name = “User”); // greet() uses “User”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Rule for default parameters in a function?

A

All parameters to the right of a default argument must also have default values.

// ❌ Invalid:
int sum(int x, int y, int z = 0, int w);
// ✅ Valid:
int sum(int x, int y, int z = 0, int w = 0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an inline function in C++?

A

A function where the compiler replaces the call with the actual code.

inline int square(int x) { return x * x; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Difference between inline functions and macros?

A

Inline functions are handled by the compiler, whereas macros are handled by the preprocessor before compilation.

Inline functions are type-safe and can access class members, while macros are not type-checked and cannot access class members.

The inline keyword is used to declare inline functions, whereas macros are defined using #define.

Inline functions evaluate arguments only once, while macros may evaluate them multiple times, which can lead to unintended behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a const parameter in C++?

A

A variable that cannot be modified once initialized.

void display(const int& x); // x cannot be changed inside

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Rules for const variables?

A

Must be initialized when declared

Cannot be reassigned later

Offers optimization and safety benefits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly