Introduction to C++
Basic Syntax and Structure
C++ is a powerful general-purpose programming language. It is used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, and functional programming. In this tutorial, we will cover the basic syntax and structure of C++ programs.
1. Hello World Program
Every C++ program starts with the main() function. Let's look at a simple example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Explanation:
#include <iostream>: This is a preprocessor directive that includes the standard input-output stream library.using namespace std;: This line tells the compiler to use the standard namespace.int main(): This is the main function where the execution of the program begins.cout << "Hello, World!" << endl;: This line prints "Hello, World!" to the console.return 0;: This line terminates the main function and returns 0 to the operating system.
2. Variables and Data Types
Variables are used to store data in a program. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory. Common data types include:
int: Integer type.float: Floating point type.double: Double precision floating point type.char: Character type.bool: Boolean type (true or false).
Example:
#include <iostream>
using namespace std;
int main() {
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Double floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
cout << "Integer: " << myNum << endl;
cout << "Float: " << myFloatNum << endl;
cout << "Double: " << myDoubleNum << endl;
cout << "Character: " << myLetter << endl;
cout << "Boolean: " << myBoolean << endl;
return 0;
}
3. Operators
Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C++ has a rich set of operators including:
- Arithmetic Operators: +, -, *, /, %
- Comparison Operators: ==, !=, >, <, >=, <=
- Logical Operators: &&, ||, !
- Assignment Operators: =, +=, -=, *=, /=, %=
Example:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
cout << "a + b = " << (a + b) << endl; // Arithmetic
cout << "a == b: " << (a == b) << endl; // Comparison
cout << "a > b: " << (a > b) << endl; // Comparison
cout << "a && b: " << (a && b) << endl; // Logical
a += b; // Assignment
cout << "After a += b, a = " << a << endl;
return 0;
}
4. Control Structures
Control structures allow you to control the flow of execution of the program. Common control structures include:
- Conditional Statements: if, else, else if
- Loops: for, while, do-while
Example:
#include <iostream>
using namespace std;
int main() {
// if-else statement
int x = 10;
if (x > 5) {
cout << "x is greater than 5" << endl;
} else {
cout << "x is not greater than 5" << endl;
}
// for loop
cout << "For Loop: ";
for (int i = 0; i < 5; ++i) {
cout << i << " ";
}
cout << endl;
// while loop
cout << "While Loop: ";
int j = 0;
while (j < 5) {
cout << j << " ";
++j;
}
cout << endl;
return 0;
}
5. Functions
Functions are blocks of code that perform a specific task. They help in breaking down the program into smaller, manageable parts. A function in C++ is defined as:
#include <iostream>
using namespace std;
// Function declaration
void sayHello();
int main() {
// Function call
sayHello();
return 0;
}
// Function definition
void sayHello() {
cout << "Hello from the function!" << endl;
}
Explanation:
void sayHello();: Function declaration.sayHello();: Function call.void sayHello() { ... }: Function definition.
