Intro To C++ Notes
Intro To C++ Notes
Tokens
Keywords
● Definition: Keywords are predefined, reserved words that form the syntax and
control the behavior of the language. They are fundamental to the language structure
and cannot be used for other purposes.
● Characteristics:
○ Fixed Meaning: Each keyword has a specific, unchanging meaning in C++.
○ Syntax Enforcement: Keywords enforce certain syntax rules and dictate the
structure of statements and declarations in C++.
● Examples:
○ int: Declares integer type variables or functions.
○ class: Declares a class, which is a blueprint for creating objects.
○ public: Specifies that members following this keyword are accessible from
outside the class.
○ private: Specifies that members following this keyword are accessible only
within the class.
● Usage: Keywords are used to define data types, control flow, and other core aspects
of programming. For instance, return is used to exit a function and optionally return
a value to the caller.
Identifiers
Constants
● Definition: Constants represent fixed values that do not change during program
execution. They provide a way to use meaningful names for immutable values.
● Characteristics:
○ Literal Constants: Directly used values in the code.
■ Numeric Literals: 10, 3.14, -5.
■ Character Literals: 'a', '1'.
■ String Literals: "Hello", "World".
○ Constant Variables: Declared using the const keyword to prevent
modification after initialization.
○ Example:
cpp
Copy code
const int DAYS_IN_WEEK = 7;
■
● Usage: Constants prevent accidental changes to values and improve code clarity.
For instance, using const for mathematical constants or fixed configuration values
ensures they remain unchanged throughout the program.
Operators
Function Prototyping
Call by Reference
Function Overloading
● Definition: Function overloading allows multiple functions with the same name but
different parameter lists to coexist in the same scope. The appropriate function is
chosen based on the arguments provided during the function call.
● Rules:
○ Different Parameters: Functions must differ in the number or type of
parameters (or both).
○ Same Scope: Overloaded functions must be in the same scope (e.g., within
the same class or namespace).
● Usage:
○ Enhanced Readability: Provides a way to perform similar operations with
different types or numbers of arguments using the same function name.
○ Example:
cpp
Copy code
void print(int i) {
○ cout << "Integer: " << i << endl;
○ }
○
○ void print(double d) {
○ cout << "Double: " << d << endl;
○ }
○
○ void print(string s) {
○ cout << "String: " << s << endl;
○ }
○
Default Arguments
● Definition: Default arguments allow functions to be called with fewer arguments than
specified. If arguments are omitted, default values are used.
○ Syntax:
cpp
Copy code
void greet(string name = "Guest", string salutation =
"Hello") {
○ cout << salutation << ", " << name << "!" << endl;
○ }
●
● Usage:
○ Flexibility: Provides the ability to call functions with varying numbers of
arguments, offering more flexibility in function usage.
○ Example:
cpp
Copy code
int main() {
○ greet(); // Uses default values
○ greet("Alice"); // Uses default salutation
○ greet("Bob", "Hi"); // No default values used
○ return 0;
○ }
●
Inline Functions
● Definition: Inline functions are small functions that are defined with the inline
keyword. The compiler attempts to insert the function’s code directly at the point of
each call to reduce function call overhead.
● Usage:
○ Performance: Suitable for small, frequently called functions where function
call overhead might impact performance.
○ Syntax:
cpp
Copy code
inline int square(int x) {
○ return x * x;
○ }
●
● Considerations:
○ Size of Function: Inline functions are typically used for short functions. Large
functions might increase the size of the binary and negatively affect
performance.
○ Compilation: The inline keyword is a suggestion to the compiler, which
may choose to ignore it based on its own optimization strategies.
Recursive Functions
● Definition: A recursive function is one that calls itself to solve a problem. Recursive
functions are typically used for problems that can be divided into smaller, similar
subproblems.
● Characteristics:
○ Base Case: The condition under which the function stops calling itself. It
prevents infinite recursion.
○ Recursive Case: The part where the function calls itself with modified
arguments to move towards the base case.
○ Example:
cpp
Copy code
int factorial(int n) {
○ if (n <= 1) return 1; // Base case
○ else return n * factorial(n - 1); // Recursive case
○ }
●
Function Templates
● Definition: Function templates allow the creation of functions that can operate with
any data type. They enable generic programming by defining functions with type
parameters.
○ Syntax:
cpp
Copy code
template <typename T>
○ T max(T a, T b) {
○ return (a > b) ? a : b;
○ }
●
● Usage:
○ Generic Programming: Allows functions to handle different data types with a
single definition, reducing code duplication.
○ Example:
cpp
Copy code
int main() {
○ cout << max(10, 20) << endl; // Uses int
○ cout << max(3.14, 2.71) << endl; // Uses double
○ return 0;
○ }
●
Lambda Functions
● Definition: Lambda functions are anonymous functions that can be defined inline
without a name. They are useful for short-term, one-off functions and are often used
with algorithms.
○ Syntax:
cpp
Copy code
[capture](parameters) -> returnType { body }
●
● Usage:
○ Local Scope: Ideal for functions that are used only within a limited scope,
such as a specific function call.
○ Capture List: Allows the lambda to capture variables from its surrounding
scope.
○ Example:
cpp
Copy code
int main() {
○ auto add = [](int a, int b) -> int {
○ return a + b;
○ };
○ cout << add(5, 10) << endl; // Prints 15
○ return 0;
● }
○