DEV Community

suraj kumar
suraj kumar

Posted on

C++ Tutorial: From Basics to Advanced Programming

C++ is one of the most powerful and widely used programming languages. Known for its efficiency, speed, and flexibility, C++ is the foundation of many modern software systems, including operating systems, browsers, databases, and high-performance applications. In this C++ Tutorial, we’ll cover the basics of C++ and gradually move toward advanced concepts with examples.


Introduction to C++

C++ was developed by Bjarne Stroustrup as an extension of the C language. It introduced the concept of Object-Oriented Programming (OOP) while retaining the power of low-level memory manipulation. This combination makes C++ suitable for both system-level and application-level programming.

Key features of C++:

  • Fast and efficient execution.
  • Object-Oriented features like classes and inheritance.
  • Rich Standard Template Library (STL).
  • Platform independence.

Setting Up C++

To run C++ programs, you need a compiler like GCC (g++), Turbo C++, or an IDE such as Code::Blocks, Dev C++, or Visual Studio Code.

A simple program:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, C++!" << endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Basic Concepts in C++

Variables and Data Types

Variables are containers for storing data. C++ supports data types like:

  • int → integer
  • float → decimal
  • char → single character
  • string → sequence of characters
  • bool → true/false
int age = 20;
float price = 99.5;
char grade = 'A';
bool isAvailable = true;
Enter fullscreen mode Exit fullscreen mode

Operators

C++ includes arithmetic, comparison, and logical operators.

int x = 10, y = 5;
cout << x + y;  // 15
cout << (x > y); // 1 (true)
Enter fullscreen mode Exit fullscreen mode

Control Structures

Control structures guide program flow.

If-Else

int num = 10;
if (num % 2 == 0) {
    cout << "Even";
} else {
    cout << "Odd";
}
Enter fullscreen mode Exit fullscreen mode

Loops

for(int i = 1; i <= 5; i++) {
    cout << i << " ";
}
Enter fullscreen mode Exit fullscreen mode

Functions

Functions help in code reusability.

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << add(5, 10);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Object-Oriented Programming (OOP) in C++

One of C++’s strongest features is OOP. It organizes code into objects that contain both data and methods.

Classes and Objects

class Car {
public:
    string brand;
    int year;

    void display() {
        cout << brand << " - " << year;
    }
};

int main() {
    Car c1;
    c1.brand = "Toyota";
    c1.year = 2020;
    c1.display();
}
Enter fullscreen mode Exit fullscreen mode

Inheritance

Inheritance allows classes to use properties of other classes.

class Animal {
public:
    void eat() { cout << "Eating..."; }
};

class Dog : public Animal {
public:
    void bark() { cout << "Barking..."; }
};

int main() {
    Dog d;
    d.eat();
    d.bark();
}
Enter fullscreen mode Exit fullscreen mode

Advanced Concepts in C++

Pointers

Pointers store memory addresses.

int x = 10;
int* ptr = &x;
cout << *ptr;  // Output: 10
Enter fullscreen mode Exit fullscreen mode

Templates

Templates allow writing generic functions.

template <typename T>
T add(T a, T b) {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Standard Template Library (STL)

STL provides pre-built classes and functions for data structures and algorithms.

Example with vectors:

#include <vector>
vector<int> v = {1, 2, 3};
v.push_back(4);
Enter fullscreen mode Exit fullscreen mode

Why Learn C++?

  • Foundation for other languages like Java and C#.
  • Used in competitive programming due to speed.
  • Essential for system programming and embedded systems.
  • Backbone of many applications (games, databases, OS).

Tips for Learning C++

  1. Start with simple programs and gradually move to OOP concepts.
  2. Practice problem-solving on platforms like HackerRank or LeetCode.
  3. Explore STL for efficient coding.
  4. Work on real projects (mini calculators, games, or file-handling apps).

Conclusion

C++ Tutorial is a versatile programming language that blends procedural and object-oriented paradigms. By learning its basics, control structures, functions, and OOP features, you build a strong foundation for advanced programming. As you progress, explore pointers, templates, and STL to unlock the full potential of C++.

Top comments (0)