Master C++ programming with our comprehensive tutorial. Learn through examples and hands-on practice.
C++ is a powerful general-purpose programming language that extends C with object-oriented features.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}A simple Hello World program in C++
Learn the fundamental syntax and structure of C++ programs including variables, data types, and operators.
#include <iostream>
#include <string>
using namespace std;
int main() {
int number = 42;
double pi = 3.14159;
char grade = 'A';
bool isValid = true;
string name = "John";
cout << "Number: " << number << endl;
cout << "Pi: " << pi << endl;
cout << "Grade: " << grade << endl;
cout << "Is Valid: " << isValid << endl;
cout << "Name: " << name << endl;
return 0;
}Common data types and variable declarations with output
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
// Arithmetic operators
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
cout << "Sum: " << sum << endl;
cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;
cout << "Quotient: " << quotient << endl;
cout << "Remainder: " << remainder << endl;
return 0;
}Basic arithmetic operators in C++ with output
Control structures allow you to control the flow of program execution.
#include <iostream>
using namespace std;
int main() {
// If-else statement
int score = 85;
cout << "Score: " << score << " - ";
if (score >= 90) {
cout << "Grade A" << endl;
} else if (score >= 80) {
cout << "Grade B" << endl;
} else {
cout << "Grade C" << endl;
}
// For loop
cout << "For loop output: ";
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
cout << endl;
// While loop
cout << "While loop output: ";
int count = 0;
while (count < 5) {
cout << count << " ";
count++;
}
cout << endl;
return 0;
}Common control structures in C++ with output