GK SOLUTIONS
AI • IoT • Arduino • Projects & Tutorials
DEFEAT THE FEAR

🛠️ Lesson 2: Setup & Your First C++ Program

⏱️ Estimated time: 20 minutes | Difficulty: Beginner

Getting Ready

To write C++, you need a compiler like GCC or Clang and a code editor like VS Code.

Setting Up C++

C++ uses the same compilers as C (GCC, Clang, MSVC) but with g++ instead of gcc.

# Verify installation
g++ --version

# Compile
g++ hello.cpp -o hello

# Run
./hello       # Linux/Mac
hello.exe     # Windows

Your First C++ Program

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;

    cout << "What's your name? ";
    cin >> name;

    cout << "How old are you? ";
    cin >> age;

    cout << "Hello " << name << "! ";
    cout << "You are " << age << " years old." << endl;

    return 0;
}

Line-by-Line Breakdown:

  • #include <iostream> — Input/output stream library
  • #include <string> — String class
  • using namespace std; — Use standard library names directly
  • string name; — C++ has a proper string type!
  • cin >> name; — Read input from keyboard
  • cout << "text"; — Print output
  • endl — End line (like \n)

C++ vs C: Key Improvements

// C style
char name[50];
printf("Name: %s\n", name);

// C++ style (much cleaner!)
string name;
cout << "Name: " << name << endl;

// C++ has bool type
bool isReady = true;

// C++ allows declaring in for loops
for (int i = 0; i < 5; i++) { ... }

// C++ references (safer than pointers)
int x = 10;
int &ref = x;  // ref IS x

✅ Quick Quiz

❓ What command compiles C++ code?

❓ How do you read input in C++?

❓ What does 'using namespace std;' do?

← Previous Next: Variables & Data Types →