Unit 4 5
Unit 4 5
Key Points:
1. The std namespace is commonly used (e.g., std::cout for standard output).
2. You can bring a namespace into scope using:
o using namespace myNamespace; (all members).
o using myNamespace::myVariable; (specific member).
# Identifiers:-
An identifier is the name used to identify variables, functions, arrays, or other user-defined elements.
Rules for identifiers:
1. Must begin with a letter or an underscore (_).
2. Can contain letters, digits, and underscores.
3. Cannot use reserved keywords (e.g., int, class, return).
4. Case-sensitive (e.g., myVar and MyVar are different).
# Constants:-
A constant is a value. that doesn’t change during the program’s execution.
Declared using:
1. The const keyword.
2. The constexpr keyword for compile-time constants.
3. Enumerations.
# Variables:
A variable is used to storage the location for data. that can change during program execution.
Types of Variables:
1. Local Variables: Declared inside functions or blocks.
2. Global Variables: Declared outside all functions.
3. Static Variables: Retain their value between function calls (static keyword).
4. Dynamic Variables: Allocated using new and deallocated with delete.
# keywords:-
C++ has a set of keywords. That are reserved for specific purposes and cannot be used as identifiers (names for
variables, functions, etc.). These keywords represent the core syntax and functionalities of the C++ language.
# Typecasting:-
Typecasting in C++ is the process of converting a variable from one data type to another.
1. Implicit Typecasting (Type Coercion): It is Done automatically by the compiler. when it is converting a
smaller data type to a larger one (e.g., int to float). It is also called type promotion.
2. Explicit Typecasting (Type Conversion): It is used to performed manually by the programmer.
Two approaches:
a. C-Style Cast
b. C++ Cast Operators
3. Static_cast: It is used to for standard type conversions. It is also used to ensures compatibility between types
during compile time.
4. Dynamic_cast: It is used to safely cast pointers/references between base and derived classes (requires
polymorphism). It is used to performs runtime checks.
# Call by value:-
1. A copy of the value is passed into the function.
2. Changes made inside the function are limited.
3. The value of the actual parameter do not change by changing the formal parameter.
4. Actual and formal argument are created at the different memory location.
{
Int t=a;
A=b;
B=t;
}
# Call by reference:-
1. An address of value is passed into the function.
2. Changes made inside the function are not limited.
3. The value of the actual parameter do change by changing the formal parameter.
4. Actual and formal arguments are created at the same memory location.
{
If(x>y)
Return x;
Else
Return y;
}
# Macros:-
1. A macro in C++ is a preprocessor. It is defined using #define.
2. Macros are simple text replacements.
3. Macros is widely used.
4. Macros is very much used in competitive programming.
5. It is terminated by new line.
Benefits of Macros:
1. Performance: Like inline functions, macros can avoid function call.
2. Flexibility: Macros can represent code. That isn’t possible with functions (like conditional code inclusion).
#define MACRO_NAME replacement_code