C++ Syntax
C++ is a powerful programming language that combines the capabilities of C language and adds object-oriented capabilities.
C++ Syntax
Let's break up the following code to understand it better:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Try it Yourself »
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. 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!".
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 make 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.
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:
It is up to you if you want to include the standard namespace library or not.
Some basic aspects of C++ syntax
Let's review some basic aspects of C++ syntax.
1. Include libraries
In C++ you can use different libraries to work with standard functions. For example, the <iostream>
library is used for data input and output. Here's how to include it:
Example
#include <iostream>
using namespace std;
2. Declaration of variables
In C++ you can declare variables of various types such as int, double, char, etc. Here is an example:
Example
int main() {
int age = 25; // A variable of type int (integer) with the name "age" and value 25
double weight = 7.625; // A variable of type double (double-precision floating point) with the name "weight" and the value 7.625
float height = 1.75; // A variable of type float (single-precision floating-point) with the name "height" and the value 1.75
char initial = 'J'; // A char variable with the name "initial" and the value J
return 0;
}
3. Conditional instructions
Use conditional statements if
, else if
and else
for program flow control:
Example
int main() {
int number = 10;
if (number > 0) {
cout << "Number is positive." << endl;
} else if (number < 0) {
cout << "Number is negative." << endl;
} else {
cout << "Number is zero." << endl;
}
return 0;
}
Example
if (age >= 18) {
// This code will be executed if "age" is greater than or equal to 18
} else {
// This code will be executed if "age" is less than 18
}
4. Loops
Use loops for
, while
and do/while
to iterate:
Example
int main() {
for (int i = 0; i < 5; ++i) {
cout << "Iteration " << i << endl;
}
return 0;
}
5. Operators
Operators are used to perform mathematical and logical operations. Some of the most common operators:
+
Adding-
Subtraction*
Multiplication/
Division==
Equals!=
Not equal to&&
Logical AND||
Logical OR
6. Functions:
Functions are used to break code into smaller, more manageable parts. A function can take parameters and return values.
Example
int sum(int a, int b) {
return a + b;
}
int main() {
int result = sum(10, 20);
// The variable "result" will contain value 30
}
7. Classes:
Classes are used to create custom data types. A class can contain variables, methods (functions) and other members.
Example
class Person {
public:
string name;
int age;
void sayHello() {
cout << "Hello, my name is " << name << endl;
}
};
int main() {
Person person;
person.name = "John Doe";
person.age = 30;
person.sayHello();
}
This is just a brief overview of C++ syntax. You will learn more in the following sections of this tutorial.