![]() |
VOOZH | about |
C++ is a structured and object-oriented language used in systems programming, game development, competitive coding.
C++ is called a middle-level language because it combines the features of both high-level and low-level programming.
Reasons why C++ is suitable for systems-level development:
'std' stands for Standard. It is a namespace in C++. Namespace is a feature that provides a way to group related identifiers such as variables, functions, and classes under a single name. The line "using namespace std" informs the compiler to add everything under the std namespace and inculcate them in the global namespace. This all inculcation of global namespace benefits us to use "cout" and "cin" without using "std::_operator_".
In C++, references is a way to create an alias for another variable.
Syntax:
int GFG = 10;
// reference variable
int& ref = GFG;
A block scope variable is also known as a local scope variable. A variable that is defined inside a function (like main) or inside a block (anything enclosed with curly braces like loops and if else) is a local variable. A block-scoped variable will not be available outside the block even if the block is inside a function.
The auto keyword is a type inference specifier introduced in C++11, which tells the compiler to automatically deduce the type of a variable from its initializer expression.
Primary Function of auto:
Use Case: With Iterators use this
Instead of this:
std::vector<int>::iterator it = v.begin();
In this programming language to call a function we have 2 methods: Pass by Value and Pass by Reference
Pass by Value | Pass by Reference |
|---|---|
| A copy of a variable is passed. | Either the address of variable or reference to the variable is passed. |
| The changes made in the function are never reflected outside the function on the variable. In short, the original value is never altered in Call by Value. | The changes made in the functions can be seen outside the function on the passed function. In short, the original value is altered in Call by reference. |
| Passed actual and formal parameters are stored in different memory locations. Therefore, making Call by Value a little memory inefficient. | Passed actual and formal parameters are stored in the same memory location. Therefore, making Call by Reference a little more memory efficient. |
Pass by Value:
Pass by Reference:
A token is the smallest individual element of a program that is understood by a compiler. A token comprises the following:
The following table lists the major differences between C and C++:
C | C++ |
|---|---|
C language came before C++ and has subset of features provided by C++ | C++ can be seen as a superset of C. Almost everything written in C compiles in C++ as well except few exceptions. |
| It is a procedural programming language. In simple words, it does not support classes and objects | It supports both procedural and object-oriented programming languages. |
| It does not support any OOP concepts like polymorphism, data abstraction, encapsulation, classes, and objects. | It supports all OOP concepts like classes, objects, inheritance, polymorphism, etc |
| It does not support Exception Handling and Generics | Supports Exception Handling and Generics through templates |
| It is a function-driven language | It is an object-driven language |
C Code (No OOP):
C++ Code with OOP:
In C++, cout belongs to the std namespace. If you forget to use using namespace std; or std::cout, you'll get a compiler error. In large projects, polluting the global namespace can lead to naming collisions
- const int x = 10; // Type safe
- #define y 10 // No type checking
Default arguments are values that are used when a function is called without some parameters.
Modifiers like signed, unsigned, long, short change the size or sign of basic data types.
Example:
unsigned int a = 5;
long double b = 5.66;
Explanation: Instead of manually specifying data types, C++ allows the compiler to deduce the type using:
These features:
Code Example:
Code:
Following are the major difference between prefix and postfix:
prefix | postfix |
|---|---|
| It simply means putting the operator before the operand | It simply means putting the operator after the operand |
| The variable is incremented/decremented first, and then the updated value is used in the expression. | The current value of the variable is used in the expression first, and then the increment/decrement happens afterward. |
| Associativity of prefix ++ is right to left | Associativity of postfix ++ is left to right |
Namespaces enable us to organize named items that would otherwise have global scope into smaller scopes, allowing us to give them namespace scope. This permits program parts to be organized into distinct logical scopes with names. The namespace provides a place to define or declare identifiers such as variables, methods, and classes.
Or we could say that A namespace is a declarative zone that gives the identifiers (names of types, functions, variables, and so on) within it a scope. Namespaces are used to arrange code into logical categories and to avoid name clashes, which might happen when you have many libraries in your code base.
The void keyword, when used as a function return type, indicates that the function does not return a value. You can use return; in such functions, but you cannot return an actual value. When used as a parameter list (e.g., void f(void) in C-style), it means the function takes no parameters.
Many assume both a and b are pointers, but only a is a pointer β b is just an int.
Code Example:
Storage class is used to defines the scope (visibility), lifetime, and linkage of variables or functions. These features usually help in tracing the existence of a variable during the runtime of a program.
Syntax:
storage_class var_data_type var_name;
Some types of storage classes:
The mutable keyword is a storage class specifier used only with non-static data members of a class. It allows a member of a const object to be modified. Normally, if an object is declared const, you cannot modify any of its members, but mutable makes an exception.
Key Points:
Why is mutable Needed: When a member function is marked as const like this
void show() const;
It cannot modify any data members of the object, because this becomes a const pointer (i.e., const ClassName* this). But sometimes, you want to modify an internal variable (e.g., access counter, log flag) that is not part of the logical state of the object. Thatβs where mutable helps.
Example:
Even though log is declared as const, accessCount is being updated, because it is declared mutable.