C++ is a powerful, general-purpose programming language developed by Bjarne Stroustrup. It is widely used for system software, games, applications, and competitive programming.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
#include <iostream> – allows input/output
main() – starting point of program
cout – prints output
Variables store data values.
int age = 18;
float height = 5.9;
char grade = 'A';
bool isStudent = true;
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
cout << "You entered: " << number;
return 0;
}
C++ uses conditions to make decisions.
int age = 20;
if (age >= 18) {
cout << "Eligible to vote";
} else {
cout << "Not eligible to vote";
}
Loops repeat a block of code.
// for loop
for(int i = 1; i <= 5; i++) {
cout << i << " ";
}
// while loop
int i = 1;
while(i <= 5) {
cout << i << " ";
i++;
}
Functions are reusable blocks of code.
int add(int a, int b) {
return a + b;
}
int marks[5] = {80, 85, 90, 75, 88};
for(int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
C++ supports OOP concepts:
class Student {
public:
string name;
int age;
void display() {
cout << name << " " << age;
}
};