Types
Fundamental Types
- Integral Types (store data exactly in equivalent binary form and can be signed or unsigned)
- bool - stores a logical value (true or false)
- char
- int - short, long, long long
- Floating Point Types (store data to a specified precision - can store very small and very large values)
- float
- double - long double
Compound Types
- Array (store multiple values of the same type)
- Pointer (store the memory address of another variable)
- Reference (alias to another variable)
- Class/Structure (store multiple values of different types)
Scope
The scope of a variable is the region of the program where the variable is accessible.
- global scope - visible to the entire program
- file scope - visible to the source code within the file
- function scope - visible to the source code within the function
- class scope - visible to the member functions of the class
- block scope - visible to the code block
Shadowing
Shadowing occurs when a variable in an inner scope has the same name as a variable in an outer scope.
#include <iostream>
using namespace std;
int x = 10; // global variable
int main() {
int x = 20; // local variable
cout << x << endl; // Output: 20
return 0;
}
Overloading
Function overloading allows multiple functions with the same name but different parameters. The compiler determines which function to call based on the number and types of arguments.
#include <iostream>
using namespace std;
void print(int x) {
cout << "Integer: " << x << endl;
}
void print(double x) {
cout << "Double: " << x << endl;
}
int main() {
print(10); // Output: Integer: 10
print(3.14); // Output: Double: 3.14
return 0;
}
Prototype
A function prototype is a declaration of a function that tells the compiler about the function's name, return type, and parameters.
#include <iostream>
using namespace std;
void print(int x); // function prototype
int main() {
print(10); // Output: 10
return 0;
}
// function definition
void print(int x) {
cout << x << endl;
}
Default Arguments
Default arguments are used to provide a default value for a function parameter if no argument is passed.
#include <iostream>
using namespace std;
void print(int x, int y = 0) {
cout << "x: " << x << ", y: " << y << endl;
}
int main() {
print(10); // Output: x: 10, y: 0
print(10, 20); // Output: x: 10, y: 20
return 0;
}
References
A reference is an alias to another variable. It allows you to work with the original variable directly.
- A reference must be initialized when declared.
- A reference cannot be null and re-assigned to refer to another variable.
&
is used to declare a reference.
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &y = x; // reference to x
y = 20; // changes the value of x
cout << x << endl; // Output: 20
return 0;
}
Array of Pointers
An array of pointers is an array where each element is a pointer to another variable.
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20, z = 30;
int *arr[3] = {&x, &y, &z}; // array of pointers
cout << *arr[0] << endl; // Output: 10
cout << *arr[1] << endl; // Output: 20
cout << *arr[2] << endl; // Output: 30
return 0;
}