A popular programming language for creating embedded software, games, operating systems, and more is C++, which is strong and performant. Learning the syntax is the first step to comprehending it for newcomers. The fundamentals of its syntax, which are the cornerstone of any program, will be covered in this blog.
Table of Contents
1. Basic Structure of a C++ Program
A program’s main() function and #include directives make up its basic structure. This is a basic “Hello World” program:
#include <iostream> // Includes standard input-output stream
using namespace std; // Allows us to use standard library names directly
int main() {
cout << "Hello, World!"; // Outputs text to the console
return 0; // Indicates successful program termination
}
- #include: This directive instructs the compiler to include a library (for input-output, this is ).
- main(): The main() function serves as the program’s entry point and is required in all C++ programs.
- cout: Text can be printed to the console using this object.
Learn on Youtube:
Syntax of C++
2. Comments
The code is explained in comments. The compiler disregards them.
- Single-line comment: Use
//
for single-line comments.
// This is a single-line comment
- Multi-line comment: Use
/* */
for comments spanning multiple lines.
/* This is a
multi-line comment */
3. Data Types
There are two categories of data types in C++: fundamental and derived types.
Fundamental data types:
int
: Integer values.float
: Floating-point numbers.double
: Double precision floating-point numbers.char
: Single characters.bool
: Boolean values (true or false).
Example:
int age = 25;
float temperature = 36.5;
char grade = 'A';
bool isRaining = false;
4. Variables and Constants
Variables hold values for data and can be initialised as well as declared before usage.
Variable declaration:
int x; // Declaration
x = 10; // Initialization
Variable declaration with initialization:
int y = 20;
Constants: Use the const
keyword to define constants whose values cannot be changed.
const float PI = 3.14159;
5. Operators
C++ supports a wide range of operators to perform operations on variables.
- Arithmetic operators:
+
,-
,*
,/
,%
Example:
int sum = 5 + 3; // sum is 8
- Relational operators:
==
,!=
,>
,<
,>=
,<=
Example:
if (x > y) {
cout << "x is greater than y";
}
- Logical operators:
&&
,||
,!
Example:
if (x > 0 && y > 0) {
cout << "Both are positive numbers";
}
6. Control Flow (Conditionals and Loops)
- If-else statement:
if (age >= 18) {
cout << "You are eligible to vote.";
} else {
cout << "You are not eligible to vote.";
}
- Switch case: Useful for selecting among multiple choices.
int day = 3;
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
default: cout << "Invalid day";
}
- Loops: Loops help execute code multiple times.
- For loop:
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
- While loop:
int i = 0;
while (i < 5) {
cout << i << " ";
i++;
}
- Do-while loop: The loop executes at least once, regardless of the condition.
int i = 0;
do {
cout << i << " ";
i++;
} while (i < 5);
7. Functions
Functions allow code reuse and better organization.
- Function declaration and definition:
int add(int a, int b) { // Declaration and definition
return a + b;
}
- Calling a function:
int result = add(5, 3); // result will be 8
- Function with no return type (void):
void greet() {
cout << "Hello, there!";
}
8. Classes and Objects
C++ is an Object-Oriented Programming (OOP) language. It uses classes to represent blueprints for objects.
- Class declaration:
class Car {
public:
string brand;
string model;
int year;
};
- Creating an object:
Car myCar;
myCar.brand = "Toyota";
myCar.model = "Corolla";
myCar.year = 2020;
cout << myCar.brand << " " << myCar.model << " " << myCar.year;
Conclusion
Gaining proficiency in C++ starts with understanding its syntax. The syntax serves as the foundation for all C++ program operations, from comprehending basic data types and control flow to handling more sophisticated ideas like classes and functions. Gaining more comfort with these ideas requires practice.
Learn More:
Setting Up a C++ Development Environment