Professional Documents
Culture Documents
C++ MODULE
C++ MODULE
What is C++?
o C++ is a cross-platform language that can be used to create high-performance
applications.
o C++ was developed by Bjarne Stroustrup, as an extension to the C language.
o C++ is a powerful general-purpose programming language.
o It can be used to develop operating systems, browsers, games, and so on.
o C++ supports different ways of programming like procedural, object-oriented,
functional, and so on.
C++ Syntax
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout << "Hello World!";
6. return 0;
7. }
Example explained
Line 1: #include <iostream> is a header file library that lets us work with input and output
objects, such as cout (used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Don't worry if you don't understand how #include <iostream> and using namespace std works.
Just think of it as something that (almost) always appears in your program.
Line 3: A blank line. C++ ignores white space.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a
function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to
output/print text. In our example it will output "Hello World".
Note: Every C++ statement ends with a semicolon ;.
Note: The body of int main() could also been written as:
C++ Operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
Operators in C++ can be classified into 5 types:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
C++ Functions
A function is a block of code which only runs when it is called.
int main() {
myFunction();
myFunction();
myFunction();
Initializing Arrays
You can initialize C++ array elements either one by one or using a single statement as follows −
Double balance [5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ]. Following is an example to assign a single
element of the array −
If you omit the size of the array, an array just big enough to hold the initialization is created.
Therefore, if you write −
Double balance [] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous example.
Balance [4] = 50.0;