C++ First Part
C++ First Part
C++ First Part
(Object-Oriented
Programming)
C++ Programming
• Introduction
• Syntax
• Output (Print Text)
• Comments
• Variables
C++ Introduction
What is C++?
• C++ is a cross-platform language that can be used to create high-performance
applications. Using Awesome Backgrounds
• C++ gives programmers a high level of control over system resources and memory.
C++ Introduction
C++ Quickstart
o Let's create our first C++ file.
o Open Codeblocks and go to File > New > Empty File.
Write the following C++ code and save the file as myfirstprogram.cpp (File > Save File as):
myfirstprogram.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
C++ Introduction
C++ Syntax
Let's break up the previous code to understand it better:
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.
C++ Introduction
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
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".
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 runs 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:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
C++ Introduction
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
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:
C++ Introduction
New Lines
To insert a new line, you can use the \n character:
Comments
Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Comments can be singled-lined or multi-lined.
Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the compiler (will not be executed).
This example uses a single-line comment before a line of code:
Example
#include <iostream>
using namespace std;
int main() {
// This is a comment
cout << "Hello World!";
return 0;
}
C++ Introduction
Multi-line Comments
• Multi-line comments start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler:
#include <iostream>
using namespace std;
int main() {
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
return 0;
}
Single or multi-line comments?
It is up to you which you want to use. Normally, we use // for short comments, and /* */ for longer.
C++ Introduction
Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for example:
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• double - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
• string - stores text, such as "Hello World". String values are surrounded by double quotes
• bool - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax:- type variableName = value;
C++ Introduction
Where type is one of C++ types (such as int), and variableName is the name of the variable (such as x or myName).
The equal sign is used to assign values to the variable.
To create a variable that should store a number, look at the following example:
Example
Create a variable called myNum of type int and assign it the value 15:
#include <iostream>
using namespace std;
int main() {
int myNum = 15;
cout << myNum;
return 0;
}
C++ Introduction
You can also declare a variable without assigning the value, and assign the value later:
#include <iostream>
using namespace std;
int main() {
int myNum;
myNum = 15;
cout << myNum;
return 0;
}
Note that if you assign a new value to an existing variable, it will overwrite the previous value:
#include <iostream>
using namespace std;
int main() {
int myNum = 15; // Now myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum;
return 0;
}
C++ Introduction
Other Types
A demonstration of other data types:
Display Variables
The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
#include <iostream>
using namespace std;
int main() {
int myAge = 35;
cout << "I am " << myAge << " years old.";
return 0;
}
C++ Introduction
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
return 0;
}
C++ Introduction
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 6, z = 50;
cout << x + y + z;
return 0;
}
C++ Introduction
#include <iostream>
using namespace std;
int main() {
int x, y, z;
x = y = z = 50;
cout << x + y + z;
return 0;
}
C++ Introduction
Identifiers
All C++ variables must be identified with unique names.
These unique names are called identifiers
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and maintainable code:
#include <iostream>
using namespace std;
int main() {
// Good name
int minutesPerHour = 60;
Constants
When you do not want others (or yourself) to override existing variable values, use the const keyword (this
will declare the variable as "constant", which means unchangeable and read-only):
#include <iostream>
using namespace std;
int main() {
const int myNum = 15;
myNum = 10;
cout << myNum;
return 0;
}
C++ Introduction
You should always declare the variable as constant when you have values that are unlikely to change:
#include <iostream>
using namespace std;
int main() {
const int minutesPerHour = 60;
const float PI = 3.14;
cout << minutesPerHour << "\n";
cout << PI;
return 0;
}
C++ Introduction