❖ C++ is a general-purpose programming language that
was first developed by Bjarne Stroustrup in the early
1980s as an extension of the C programming language.
C++ is an object-oriented language that supports various
programming paradigms such as procedural
programming, functional programming, and generic
programming. Here are some basic concepts and syntax
of C++:
1. Variables
• In C++, a variable is a named storage location that holds a value. Before using a
variable in C++, you need to declare it by specifying its data type and name. Here's
an example of how to declare and initialize a variable in C++:
int num = 10;
In this example, int is the data type of the variable num, and 10 is its initial value.
C++ supports several data types for variables, including:
• int: used for integer values (e.g., -10, 0, 100)
• double: used for floating-point values with double precision (e.g., 3.14159, 2.71828)
• float: used for floating-point values with single precision (e.g., 3.14f, 2.71f)
• char: used for character values (e.g., 'a', 'b', '1', '?')
• bool: used for Boolean values (e.g., true or false)
Here are some examples of declaring variables with different data types:
double pi = 3.14159;
float temperature = 25.5f;
, char letter = 'A';
bool is_sunny = true;
In addition to declaring variables with a specific data type, you can also use modifiers such
as const and volatile.
The const modifier specifies that the variable's value cannot be modified once it has been
initialized:
const int num = 10;
The volatile modifier tells the compiler that the variable's value may change
unexpectedly, and it should not make any assumptions about the variable's value:
volatile int sensor_value;
Variables are an essential concept in C++, as they allow you to
store and manipulate data values within your program.
2.Operators
In C++, operators are used to perform operations on variables and
values. There are several types of operators in C++, including:
Arithmetic operators: used to perform basic mathematical operations
such as addition, subtraction, multiplication, and division. Here
are some examples:
int a = 5, b = 10;
int sum = a + b; // sum = 15
int difference = a - b; // difference = -5
int product = a * b; // product = 50
int quotient = b / a; // quotient = 2
int remainder = b % a; // remainder = 0
2.Comparison operators:
used to compare two values and return a Boolean value (true or
false). Here are some examples:
int a = 5, b = 10;
bool is_greater = (a > b); // false