C++ is a general-purpose programming language that was developed in
1979 by Bjarne Stroustrup as an extension of the C programming
language. It is an object-oriented language, which means it allows
programmers to define data types in the form of classes and objects,
and to create and manipulate those objects using methods.
C++ is a statically typed language, which means that variables must
be declared with a specific data type before they can be used. It is
also a compiled language, which means that source code written in
C++ must be converted into machine code (a series of instructions
that can be executed by a computer's processor) before it can be
run.
-
Learning C++ requires a solid foundation in computer science
concepts and a willingness to practice and learn from your
mistakes. With dedication and practice, you can become proficient
in C++ and use it to build a wide range of applications.
-
C++ has a rich standard library that provides a wide range of
functions and data types for common programming tasks, such as
input/output, string manipulation, and mathematical operations.
This guide assumes you have the following basic background:
-
A general understanding of the Internet and the World Wide Web
(WWW).
-
Some programming experience. If you are new to programming, try
one of the tutorials linked on the main page about JavaScript.
C++ is an object-oriented programming language that is an
extension of the C programming language. C is a procedural
programming language, which means that it focuses on a sequence of
actions that are executed one after the other. C++ adds support
for object-oriented programming, which involves organizing code
into classes and objects that can interact with each other.
One of the main differences between C and C++ is that C++ supports
object-oriented programming, while C does not. C++ also has
additional features such as function overloading, templates, and
exception handling, which are not present in C.
C++ is a more powerful and flexible language than C, and it is
often used for building large, complex software systems. However,
C is still widely used because it is a simple and efficient
language, and it is easy to learn. C++ is often used in situations
where the programmer needs more control over the program and the
hardware, such as in systems programming or low-level programming.
Hello World
To get started with writing C++, open the Vscode and write
your first "Hello world" c++ code:
- #include <'iostream>
- int main()
- {
- std::cout << "Hello, World!" << std::endl;
- return 0;
- }
Variables
In C++, a variable is a named location in memory that is used
to store a value. When you declare a variable, you specify its
type and name, and you can then use the variable to store and
manipulate data in your program.
Declaring Variables
There are several ways to declare variables in C++. The most
basic way is to simply specify the data type and the name of
the variable:
With the keyword int. For example,
- int x = 42.
Once a variable is declared, you can use it to store and
manipulate data in your program. For example, you can assign a
new value to the variable, or you can use it in an expression:
- x = 30; // Assign a new value to the variable "x"
-
int newX = x + 5; // Use the variable "x" in an expression
to calculate a new value
Variable scope
In C++, the scope of a variable refers to the region of the
program in which the variable is visible or can be accessed. A
variable's scope determines where it can be used in the
program and how long it will exist in memory.
There are two main types of variable scope in C++: local scope
and global scope.
Global variables
a global variable is a variable that is defined outside of any
block of code, such as a function or loop, and is therefore
visible and accessible throughout the entire program. Global
variables are created when the program starts and are
destroyed when the program ends.
Global variables are useful in situations where you need to
share data between different parts of your program, such as
between functions or across multiple source files. However,
they can also introduce complexity and can make it more
difficult to understand the behavior of your code, especially
if they are used extensively.
Constants
A constant is a value that cannot be modified once it has been
set. Constants are often used to define values that are used
frequently throughout a program and that should not be changed
by accident.
There are several ways to define constants in C++. One way is
to use the "const" keyword to define a constant variable:
-
const int MAX_ITEMS = 100; // Define a constant called
"MAX_ITEMS" with the value 100
Another way to define constants in C++ is to use the "define"
directive:
-
#define MAX_ITEMS 100 // Define a constant called
"MAX_ITEMS" with the value 100
It's important to use constants to define values that should
not be changed, as it helps to make your code more
maintainable and less prone to errors. Constants also make
your code more readable, as they provide a meaningful name for
the value rather than just using a hard-coded number.
Data types
there are several different data types that you can use to
declare variables. The most basic data types are the built-in
types, which include:
-
Integer types: "int", "short", "long", and "long long"
-
Floating-point types: "float", "double", and "long double"
- Boolean type: "bool"
- Character type: "char"
In addition to the built-in types, C++ also allows you to
define your own custom data types using user-defined types,
such as "struct" (structure) and "class" (class). These types
allow you to create complex data structures and objects that
can represent real-world entities in your program.
If else statement
In C++, the "if-else" statement is a control flow statement
that allows you to execute a block of code conditionally based
on the value of a Boolean expression. The Boolean expression
is evaluated, and if it is found to be true, the code in the
"if" block is executed. If the Boolean expression is false,
the code in the "else" block is executed instead.
- if (condition) { statement_1; } else { statement_2; }
You may also compound the statements using else if to have
multiple conditions tested in sequence, as follows:
-
if (condition_1) { statement_1; } else if (condition_2) {
-
statement_2; } else if (condition_n) { statement_n; } else {
- statement_last; }
In the case of multiple conditions only the first logical
condition which evaluates to true will be executed. To execute
multiple statements, group them within a block statement ({
... }) . In general, it's good practice to always use block
statements, especially when nesting if statements:
-
if (condition) { statement_1_runs_if_condition_is_true;
- statement_2_runs_if_condition_is_true; } else {
- statement_3_runs_if_condition_is_false;
- statement_4_runs_if_condition_is_false; }
While statement
the "while" statement is a control flow statement that allows
you to execute a block of code repeatedly as long as a given
condition is true. The "while" statement continuously
evaluates a Boolean expression and, if the expression is found
to be true, it executes the code in the loop body. The loop
continues to execute until the Boolean expression is false.
- while (condition) statement
The "while" loop is a useful tool for executing a block of
code repeatedly, and it is often used when you don't know in
advance how many times the loop will need to execute. It is
important to ensure that the Boolean expression used in the
"while" loop eventually becomes false, or the loop will
execute indefinitely and your program will become stuck in an
infinite loop.
Function declarations
A function definition (also called a function declaration, or
function statement) consists of the function keyword, followed
by:
- The name of the function.
-
A list of arguments to the function, enclosed in parentheses
and separated by commas.
-
The C++ statements that define the function, enclosed in
curly brackets, { }.
For example, the following code defines a simple function
named add:
- int add(int x, int y) {
- return x + y; // Return the sum of "x" and "y"
- }
Reference
-
All the documentation in this page is taken from
MDN