C++ Lab Basic
C++ Lab Basic
C++ Lab Basic
PROGRAMMING
Lab class 2
int main() {
cout << "Hello World!";
return 0;
}
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.
//#include <conio.h> includes the console input
output library functions. The getch() function is defined
in conio.h file.
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. 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.
//void main() The main() function is the entry point of
every program in C++ language. The void keyword
specifies that it returns no value.
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:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces.
However, multiple lines makes the code more readable.
Line 6: return 0 ends the main function.
Line 7: Do not forget to add the closing curly bracket } to
actually end the main function.
int main() {
cout << "Hello World!";
return 0;
}
New Lines
To insert a new line, you can use the \n character:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
Another way to insert a new line, is with the endl
manipulator:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
C++ 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
Other Types
A demonstration of other data types:
Example
int myNum = 5; // Integer (whole number
without decimals)
double myFloatNum = 5.99; // Floating point number
(with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
Declare Many Variables
To declare more than one variable of the same type, use a
comma-separated list:
Example
int x = 5, y = 6, z = 50;
cout << x + y + z;
C++ 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:
Example
// Good
int minutesPerHour = 60;