C++ Lec1-Introduction+Output+Newline
C++ Lec1-Introduction+Output+Newline
C++ Introduction
Programming language:
program.
C++ like FORTRAN, BASIC, Pascal are high-level language (they are closer to
is a general-purpose language.
Machine language: the only programs that can be used to operate a computer.
Assembly language: substitution of a word like symbols for the binary of codes
(operating codes) and both decimal numbers and labels for memory address.
Source Code (Source program): programs are written in a computer language, this
must be translated into the machine language of the computer on which a compiler will
What is C++?
C++ was developed by Bjarne Stroustrup at Bell labs in 1979, as an extension to the C language.
C++ gives programmers a high level of control over system resources and memory.
The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and C++17.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives a clear structure to programs and
allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa
Programming C++ Samir Bilal Practical & Theoretical
3
A compiler, like GCC, to translate the C++ code into a language that the computer will
understand
There are many text editors and compilers to choose from. We will use an IDE.
An IDE (Integrated Development Environment) is used to edit AND compile the code.
Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be
used to both edit and debug C++ code.
Write the following C++ code and save the file as: Myfirstprogram.cpp (File > Save File as):
myfirstprogram.cpp
Then, go to Build > Build and Run to run (execute) the program. The result will look something
to this:
C++ Syntax
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.
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: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines make the code more
readable.
Line 7: Do not forget to add the closing curly bracket} to actually end the main function.
Omitting Namespace
You might see some C++ programs that run without the standard namespace library. The using
namespace std line can be omitted and replaced with the std keyword, followed by the::
operator for some objects:
Example:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Note: It is up to you if you want to include the standard namespace library or not.
Note: one of the most common syntax errors is indeed to forget to include some semicolon after
a statement.
The cout object, together with the << operator, is used to output values/print text:
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
You can add as many cout objects as you want. However, note that it does not insert a new line
at the end of the output:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
Tip: Two \n characters after each other will create a blank line:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n\n";
cout << "I am learning C++";
return 0;
}
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
Both \n and endl are used to break lines. However, \n is used more often and is the preferred
way.