Lesson 4 - C++ Basics
Lesson 4 - C++ Basics
C++
Basics
Let’s
pray…
Review…
• The Programming Process
Defining the Problem
Planning the Solution
Coding the Program
Testing the Program
Documenting the
Program
Today, we’ll talk
about…
C++ Basics
What is C++?
C++ Installation
Your First C++
Program
C++ Output
Look…
There are three
things wrong in this
picture. Find them.
What is C++?
C++ is a popular cross-platform language that can
be used to create from simple programs to
performance applications like high- operating
browsers, video-games, art applications, systems,
and so on.
default (standard).
Your First C++
Program
• int main () - it is the main function where the program execution
begins. It is followed by curly brackets {} indicating the start and
end of the function.
• //This program displays "Hello World" on the screen. – This is a
comment, it does not contribute to how the program works or display in
the
program execution. This is
only used to keep
programmers be guided
writing long lines of codes.
Your First C++
Program
• cout<<”Hello World”!; - This is a command to display the “Hello
World”
message on the screen. Syntax for output.
• return 0; - It tells the compiler that it is the end of the main() function,
thus, terminating the program.
Note: Each line in the function must
always end with a semi-colon (;).
It serves as a statement
terminator, this means the
compiler must proceed to the
next line.
Testing Your C++
Program
To see if the program is working, we need to compile
and run it. To do so, click the “Execute” menu,
select “Compile and Run” or just simply press F11 in your
keyboard.
Using
C++ Output
To display any data from the program, we will use a
predefined object for output, cout. In our example, the
cout object is used to display a single line of text “Hello
World”.
To do so, type cout, followed by << and the text.
The text must be enclosed with double quotation marks
(“”), then end with a semi-colon (;).
Example:
cout<<”This is an output”;
C++ Output
We can also display multiple lines of texts. To do so, we
will use the object “endl”. The endl is used to insert a new
line of the output stream. To do so, put two open brackets
(<<) after the text, add the “endl”, and end with a semi-
colon (;). Another way of inserting a new line is to put “\n”
inside the text body.
For example:
cout<<“This is a the first line.”<<endl;
cout<<“This is the second line.\n”;
cout<<“This is the last line.”;
Code
Output
C++ Output
We can also use cout to call out the values
of variables to perform more complex functions. It will
be discussed in the next topic in the upcoming classes.
Assignmen
ttext:
W rite a C++ program that will display the following