Here, you'll find beginner-friendly programs tutorials, essential data structures, algorithms, and file operations. All with C++.
Universities prefer teaching programming fundamentals using C++ due to its efficiency, versatility, industry relevance, support for object-oriented programming principles, emphasis on memory management, and its role as a transitional language. C++ provides students with a solid foundation in low-level concepts, such as memory management and pointers, while also preparing them for a wide range of industries and programming domains. Its widespread use in industries like game development, systems programming, and scientific computing ensures that students acquire skills that are highly sought after in the job market. Additionally, C++ serves as a stepping stone for learning other languages, making it an ideal choice for introducing students to the core principles of programming. (ChatGPT)
C++ is a middle-level programming language, positioned between high-level languages like Python add javaScript and low-level languages like Assembly. It inherits features from both. C++ has the ability to perform low-level operations like memory management, similar to the C language. This makes it a suitable choice for understanding these operations, especially since it is not as difficult to learn as C or other low-level languages. Additionally, C++ provides high-level features such as object-oriented programming (OOP) as well as standard library support for data structures and algorithms.
To use C++ with VS Code, you'll need a compiler. By default, VS Code doesn't support C++ compilers, so you must install one such as GCC. There are various methods to set this up, including using the Mingw-w64 executable or the MSYS2
command-line interface (CLI). See this resource:
Data types exist in all programming languages, although there may be small differences between them. For example, in C++, number data types include int
, float
, and double
, while in TypeScript, there's only the number
data type for numbers. Data types define the kind of data that a variable can hold, such as integers, floating point numbers, characters, and more. Here are the basic data types in C++:
Data Type | Description | Size/Length |
---|---|---|
int |
Used for integers | 2 or 4 bytes |
float |
Used for floating point numbers | 4 bytes |
double |
Used for double sized floating point numbers | 8 bytes |
char |
Used for single characters | 1 byte |
bool |
Used for boolean values (true/false) | 1 byte |
auto |
Automatically detects data type (C++11 version) | Variable |
// datatype variableName = value;
int x = 5;
char c = 'A';
bool isDone = true;
auto y = x; // int
In C++, the standard library iostream
is commonly included in most .cpp
files to control input and output operations.
The cin
object, along with the >>
extraction operator, is utilized for input operations. It allows users to input data from the console into variables. On the other hand, the cout
object, paired with the <<
insertion operator, is used for output operations and displaying data on the console.
It's important to note that when interacting with cin
, input values should not contain spaces, as a space typically signifies the end of an input task. To input an entire line of text, the getline
function can be used, which will be introduced later.
Additionally, the cerr
and clog
objects of the iostream
library classes, they are used for error and logging purposes.
// Declare an integer variable to store the user's input
int number;
// Display a prompt for the user to enter a number
cout << "Enter a number: ";
// Read the user's input from the console and store it in the 'number' variable
cin >> number;
// Display the entered number
cout << "The number is: " << number << endl;
- GeeksforGeeks - Basic Input / Output in C++
- Simplilearn - Understanding the Concept of Cin Object in C++ for Beginners
- GeeksforGeeks - Difference between cerr and clog
Control structures are fundamental to every programming language. They dictate the flow of a program, determining how it executes instructions based on sequence, selection, and iteration.
-
Sequence Logic: Sequence logic involves executing code in the order it's written, following a linear flow. Each line of code is executed in sequence, and any subsequent lines can override previous ones. For instance, setting a variable
x
to 0 and then setting it to 5 on the next line will result inx
having a final value of 5.int x = 0; x = 5; // The program will take this value
-
Selection Logic (Conditional): Selection logic means executing specific blocks of code based on some conditions. This is achieved using conditional statements such as
if
,else if
,else
, andswitch
.bool isTrue = false; if (isTrue) { cout << "True"; // Will not be displayed } else { cout << "False"; // Will be displayed }
-
Iteration Logic: Iteration logic makes dynamic repetition through loops such as
while
,for
, anddo while
. These loops repeatedly execute a block of code until a specific condition is met.for (int i = 0; i < 5; i++) { cout << i << " "; }
The code above will print numbers from 0 to 4 dynamically.
- GeeksforGeeks - Control Structures in Programming Languages
- W3Schools - C++ If ... Else
- Introduction to Iterators in C++
Functions are essential components present in all high-level programming languages, whether functional or object-oriented. They are used as modular blocks of code designed to accomplish specific tasks without repeating the same code.
In C++, functions are integral to the program's structure, as the main()
serves as the entry point for execution.
Functions are "self-contained" modules of code that execute a specific task. They typically accept input data, process it, and return a result. Once defined, functions can be invoked repeatedly from within other functions. (University of Utah Kahlert School of Computing)
There are two main categories of functions:
-
Predefined Functions: Also known as built-in functions, these functions are provided by the language itself or from standard library headers in C++. Such as:
length()
,size()
, and math functions. -
User-defined Functions: These functions are created and implemented by the programmer to accomplish specific requirements.
Functions can be categorized based on their return type to:
-
Value-returning Functions: These functions return a value of a specified data type.
-
Void Functions: These functions do not return any value (
void
).
In C++, defining a function by specifying:
- The return type (Not required in all languages but in C++).
- The function name (following naming conventions).
- Parameters enclosed within parentheses.
- The function body enclosed within curly braces, containing the code to be processed and executed.
// Void function
void sayHello() {
cout << "Hello World";
}
// Value returning function
int calc() {
int a = 0;
int b = 2;
return a + b; // returns `2`
}
Parameters will be introduced later.
W3Schools - C++ Functions University of Utah Kahlert School of Computing
Operators in C++ can be categorized into 5 different types. And also categorized two 3 kinds based on their functionality and the number of operands they involve.
- Arithmetic Operators: For mathematical operations.
- Assignment Operators: For assigning values to variables.
- Relational Operators: For comparing values.
- Logical Operators: For logical operations (and / or).
- Bitwise Operators: Used for bit-level manipulation.
- Unary Operators: Operate on a single operand.
- Binary Operators: Operate on two operands.
- Ternary Operator: Operates on three operands.
First: Arithmetic operators
Operator | Kind | Example |
---|---|---|
+ |
Unary | +5 (output is positive value of 5) |
Binary | 5 + 3 (output is 8) |
|
- |
Unary | -5 (output is negative of 5) |
Binary | 10 - 3 (output is 7) |
|
* |
Binary | 5 * 3 (output is 15) |
/ |
Binary | 10 / 2 (output is 5) |
% |
Binary | 10 % 3 (output is 1) |
++ |
Unary | int x = 5; ++x; (Called: pre-increment. output is x = 6) |
-- |
Unary | int x = 5; x--; (Called: post-increment. output is x = 4) |
Note that the +
and -
operators can act as both unary and binary operators depending on the context in which they are used, +5 here it's unary while here 2 + 5 it's binary.
Second: Assignment Operators:
It's clear from the name that these operators assign values to variables. And they are binary operators. These operators are commonly used to update the value of variables based on calculations or conditions in the program.
Operator | Example |
---|---|
= |
int x = 5; |
+= |
x += 3; (equivalent to x = x + 3; ) |
-= |
x -= 2; (equivalent to x = x - 2; ) |
*= |
x *= 4; (equivalent to x = x * 4; ) |
/= |
x /= 2; (equivalent to x = x / 2; ) |
%= |
x %= 3; (equivalent to x = x % 3; ) |
Arithmetic operators before the =
sign will do a mathematical operation and then the =
will assign the result to the variable.
For example x += 2
it's the same as x = x + 2
so the 2
will be added to the value of x
rhen result of addition will be assigned to x
as a new value.
Notes: - Both the variable and the value should be of the same data type. - The variable should always be on the left side of the assignment operator, and the value to be assigned should be on the right side.
Third: Relational Operators: Relational Operators are binary operators used for comparing purposes. They evaluate expressions and return either true or false based on the comparison result, and the are the core of programming languages. They are commonly used in conditional statements.
They are:
Operator | What is it | Example |
---|---|---|
== |
Equal to | 5 == 5 (true) |
!= |
Not equal to | 5 != 3 (true) |
> |
Greater than | 10 > 5 (true) |
< |
Less than | 3 < 8 (true) |
>= |
Greater than or equal | 5 >= 5 (true) |
<= |
Less than or equal | 8 <= 8 (true) |
Fourth: Logical Operators:
Pending ..
Thanks goes to these wonderful people (emoji key):
Khaled Alshibani 💻 📖 💡 🤔 ✅ |
This project follows the all-contributors specification. Contributions of any kind welcome!