Skills101

C++ Programming · Lesson 1 of 9 · Updated ·

Introduction to C++ — From C to C++, cin/cout, Namespaces and Strings

C++ is C plus a lot more: classes and objects, references, templates and a huge standard library (the STL). It's used for games, browsers, databases, trading systems — and it's the favourite language of competitive programmers. Many B.Tech and BCA syllabi teach it right after C.

Comparison of C and C++: C is procedural; C++ adds classes, function overloading, references, the STL and cin/cout

1. Your first program

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, Skills101!" << endl;
    cout << "2 + 3 = " << 2 + 3 << "\n";
    return 0;
}
Hello, Skills101!
2 + 3 = 5
  • #include <iostream> gives cout (output) and cin (input).
  • << sends values to cout, one after another.
  • endl and "\n" both start a new line ("\n" is faster).
  • Compile with g++ -std=c++17 main.cpp -o main.

2. Namespaces

Library names live in the std namespace: std::cout, std::string. using namespace std; lets you skip std::. That's fine in small programs and contest code; in big projects, write std:: to avoid name clashes.

3. Input with cin

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

int main() {
    // Input: Asha 21
    // Input: Jaipur Rajasthan
    string name;
    int age;
    cin >> name >> age;           // reads words separated by spaces
    cin.ignore();                 // skip the rest of the line
    string city;
    getline(cin, city);           // reads a whole line, spaces included
    cout << name << " (" << age + 1 << " next year) from " << city << "\n";
    return 0;
}
Asha (22 next year) from Jaipur Rajasthan

No & and no format specifiers — cin knows the types.

4. The string class

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

int main() {
    string s = "Skills";
    s += "101";                                  // join
    cout << s << " " << s.size() << "\n";
    cout << s.substr(0, 6) << " " << s.find("101") << "\n";
    cout << (s == "Skills101") << " " << s[0] << "\n";
    string num = to_string(42) + "%";
    int back = stoi("250") + 1;
    cout << num << " " << back << "\n";
    return 0;
}
Skills101 9
Skills 6
1 S
42% 251

Unlike C strings, std::string grows by itself, compares with ==, and you don't manage memory.

5. auto and the range-based for loop

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

int main() {
    vector<int> marks = {78, 92, 65};
    int total = 0;
    for (int m : marks) total += m;       // range-based for
    auto avg = total / (double)marks.size();   // auto: compiler picks double
    cout << total << " " << avg << "\n";
    for (auto& m : marks) m += 5;         // & lets us change each element
    for (auto m : marks) cout << m << " ";
    cout << "\n";
    return 0;
}
235 78.3333
83 97 70

6. bool and other C++ basics

  • bool with true / false is built in.
  • const for constants; nullptr instead of NULL.
  • Declare variables where you need them (even inside for).

Common mistakes

  • Mixing cin >> x and getline without cin.ignore() — getline reads an empty line.
  • Forgetting #include <string> / <vector> (some compilers include them indirectly, others don't).
  • using namespace std; in header files of big projects.

Quick recap

  • C++ = C + OOP + templates + STL.
  • cout << to print, cin >> to read, getline for whole lines.
  • std::string and std::vector replace most manual arrays and char strings.
  • auto and for (x : container) make code shorter.

Practice what you learned

📝 Quiz: C++ Introduction5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →