Professional Documents
Culture Documents
C++ Basics
What is C++?
C++ is a compiled, object-oriented language.
It is the “successor” to C, a procedural language.
(The “++” is called the successor operator in C++).
C was derived from a language called B which was in turn derived from
BCPL.
C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs.
C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T
Bell Labs.
Most of C is a subset of C++.
Structure of C++ Program
A C++ program has the following structure
[Comments]
[Preprocessor directives]
[The main function]
[Starting of main function]
[Body of function]
[Return the function]
[End of the function]
// my first program in C++
This is a comment line. All lines beginning with two slash signs (//) are
considered comments and do not have any effect on the behavior of the
program.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor.
In this case the directive #include <iostream> tells the preprocessor to
include the iostream standard file.
This specific file (iostream) includes the declarations of the basic standard
input-output library in C++
using namespace std;
All the elements of the standard C++ library are declared within what is called a
namespace, the namespace with the name std.
This line is very frequent in C++ programs that use the standard library
int main ()
This line corresponds to the beginning of the definition of the main function.
The main function is the point by where all C++ programs start their execution,
independently of its location within the source code.
The word main is followed in the code by a pair of parentheses (()). That is
because it is a function declaration
Right after these parentheses we can find the body of the main function
enclosed in braces ({}).
cout << "Hello World!";
This line is a C++ statement.
A statement is a simple or compound expression that can actually
produce some effect.
cout represents the standard output stream in C++
The meaning of the entire statement is to insert a sequence of
characters (in this case the Hello World sequence of characters) into
the standard output stream (which usually is the screen).
cout is declared in the iostream standard file within the std
namespace
Notice that the statement ends with a semicolon character (;)
return 0;
You can create more than one variable of the same type in one
statement by writing the type and then the variable names,
separated by commas.
For example:
int myAge, myWeight; // two int variables
long area, width, length; // three longs
myAge and myWeight are each declared as integer variables.
The second line declares three individual long variables named
area, width, and length.
However keep in mind that you cannot mix types in one definition
statement
Variable initialization
You assign a value to a variable by using the assignment operator (=)
Thus, you would assign 5 to Width by writing
int Width;
Width = 5;
You can combine these steps and initialize Width when you define
it by writing
int width = 5;
Just as you can define more than one variable at a time, you can initialize
more than one variable at creation. For example:
// create two int variables and initialize them
int width = 5, length = 7;
VARIABLE SCOPE
A scope is a region of the program and broadly speaking
there are three places, where variables can be declared:
Inside a function or a block which is called local
variables,
In the definition of function parameters which is
called formal parameters.
Outside of all functions which is called global
variables.
Local Variables
Variables that are declared inside a function or block are local variables
They can be used only by statements that are inside that function or block of code
Following is the example using local variables:
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Global Variables
Global variables are defined outside of all the functions,
usually on top of the program
Following is the example using global variables:
#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main ()
{
// Local variable declaration:
int g = 10;
cout << g;
return 0;
}
Basic Data Types
When you define a variable in C++, you must tell the compiler what
kind of variable it is: an integer, a character, and so forth
Several data types are built into C++.
The varieties of data types allow programmers to select the type
appropriate to the needs of the applications being developed
Basic (fundamental) data types in C++ can be conveniently divided
into numeric and character types
Numeric variables can further be divided into integer variables and
floating-point variables
Integer variables will hold only integers whereas floating number
variables can accommodate real numbers
C++ data types and their ranges
= n = 25
+= n += 25 n = n + 25
-= n -= 25 n = n – 25
*= n *= 25 n = n * 25
/= n /= 25 n = n / 25
%= n %= 25 n = n % 25
|= n |= 0xF2F2 n = n | 0xF2F2
^= n ^= 0xF2F2 n = n ^ 0xF2F2
Modulo is the operation that gives the remainder of a division of two values.
For example, if we write:
a = 11 % 3; 2
Increase and decrease (++, --)
the increase operator (++) and the decrease operator (--) increase or
reduce by one the value stored in a variable
They are equivalent to +=1 and to -=1, respectively
c++;
c+=1;
c=c+1;
In Example 1, B is increased before its value is copied to A. While in Example 2, the
value of B is copied to A and then B is increased
Relational and equality operators ( ==, !=, >, <, >=, <= )
&& Operator
The operator && corresponds with Boolean logical operation AND
This operation results true if both its two operands are true, and false otherwise
a b A&&b
True True True
True False False
False True False
false False False
|| operator
The operator || corresponds with Boolean logical operation OR
This operation results true if either one of its two operands is true,
thus being false only when both operands are false themselves
a b a || b
True True True
True False True
False True True
false False False
Conditional operator ( ? )
The conditional operator evaluates an expression returning a value if that
expression is true and a different one if the expression is evaluated as false
Its format is:
condition ? result1 : result2
If condition is true the expression will return result1, if it is not it will return
result2
Example:-
7 == 5 ? 4:3 //returns 3, since 7 is not equal to 5.
7 == 5+2 ? 4:3 //returns 4, since 7 is equal to 5+2.
// conditional operator
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7; Output
7
c = (a>b) ? a : b;
cout << c;
return 0;
}
Precedence of operators