c++
c++
C++ is one of the oldest yet most popular programming languages in the world due to its
performance and efficiency.
• To learn C++, you need to learn the syntax (grammar) of the language as well as C++
Standard Library, which is a collection of pre-written C++ code for solving common
problems.
• To run C++ applications, first we have to compile our C++ code to machine code.
THE BASICS
• To declare a variable, we should specify its type and give it a meaningful name.
• We should initialize variables before using them. Using an uninitialized variable can
lead to unexpected behavior in our programs since these variables hold garbage values.
• Unlike variables, the value of constants don’t change.
• We use cout (pronounced sea-out) to write characters to the Standard Output Stream
which represents the terminal or console window.
• We use cin (pronounced sea-in) to read data from the Standard Input Stream which
represents the keyboard.
• We use the Stream Insertion Operator (<>) to read data from a stream.
• The Standard Template Library (STL) consists of several files each containing
functions for different purposes.
• To use functions in the Standard Library, we should include the corresponding files
using the #include directive.
• Using comments we can explain what cannot be expressed in code. This includes
why’s, how’s, and any assumptions we made while writing code.
DATA TYPES
C++ has several built-in data types for storing integers (whole numbers), floating-point
numbers (numbers with a decimal point), characters, and Boolean values (true/false).
• Whole numbers are interpreted as int by default. To represent a long, we have to use
the L suffix (eg 10L).
• Using the auto keyword, we can let the compiler infer the type of a variable based on
its initial value.
• Numbers can be represented using the decimal, binary, and hexadecimal systems.
• Using the sizeof() function, we can see the number of bytes taken by a data type.
• We can use stream manipulators to format data sent to a stream. The most common
manipulators are setw, fixed, setprecision, boolalpha, left, and right.
• Array elements can be accessed using an index. The index of the first element in an
array is 0.
• When we store a smaller value in a larger data type, the value gets automatically cast
(converted to) the larger type. When storing a large value in a smaller data type, we have
to explicit cast the value.
• C-style casting involves prefixing a variable with the target data type in parentheses. In
C++, we use the static_cast operator.
• C++ casting is safer because conversion problems can be caught at the compile-
time. With C-style casting, we won’t know about conversion issues until the run-time
DECISION MAKING
• With Logical operators we can combine Boolean expressions and represent more
complex conditions.
• With the logical AND operator (&&) both operands should be true. If either of them is
false, the result will be false.
• With the logical OR operator (||), the result is true if either of the operands is true.
• Using if and switch statements, we can control the logic of our programs.
• An if statement can have zero or more else if clauses for evaluating additional
conditions.
• Using the conditional operator we can simplify many of the if/else statements.
• A switch block often has two or more case labels and optionally a default label.
• Case labels should be terminated with a break statement; otherwise, the control
moves to the following case label.
• Switch statements are not as flexible as if statements but sometimes they can make
our code easier to read.