C++ Programming Basics
C++ is a powerful general-purpose programming language. It is widely used for developing
operating systems, browsers, games, and more. C++ supports both procedural and object-oriented
programming, making it flexible and efficient.
1. Setting Up C++
To start coding in C++, install:
- A C++ compiler (e.g., GCC, MinGW)
- An IDE like Code::Blocks or Visual Studio Code
To verify installation, type `g++ --version` in your terminal or command prompt.
2. Your First C++ Program
Here is a simple C++ program that prints "Hello, World!":
```cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
```
To run:
1. Save as hello.cpp
2. Compile: `g++ hello.cpp -o hello`
3. Run: `./hello`
3. Variables and Data Types
C++ supports various data types:
- int: Integer numbers
- float: Floating point numbers
- char: Single characters
- string: Text (requires <string> header)
- bool: true/false
Example:
```cpp
int age = 25;
float price = 19.99;
char grade = 'A';
string name = "John";
bool isCppFun = true;
```
4. Control Flow
C++ supports if-else, switch, for, while, and do-while loops.
Example (if-else):
```cpp
int number = 10;
if(number > 0) {
cout << "Positive number" << endl;
} else {
cout << "Negative number" << endl;
```