DEV Community

Cover image for How to Learn C++: The Complete Guide for Beginners
The Educative Team for Educative

Posted on • Updated on • Originally published at educative.io

How to Learn C++: The Complete Guide for Beginners

Originally posted on Educative.io

C++ notoriously has a steep learning curve, but taking the time to learn this language will do wonders for your career and will set you apart from other developers. You’ll have an easier time picking up new languages, you’ll form real problem-solving skills, and build a solid foundation on the fundamentals of programming.

C++ will help you instill good programming habits (i.e. clear and consistent coding style, comment the code as you write it, and limit the visibility of class internals to the outside world), and because there’s hardly any abstraction, you’re required to define just about every attribute to make your code work; this will force you to really understand what it is you’re doing and how your program will affect the system.

Curious about why we think C++ is a good language to learn? Check out our article on Why C++ is a good first language to learn.

In this post, we will take you through a beginner’s roadmap to learning C++ so you can feel confident as you begin your journey. If you want to skip the reading and get hands-on with C++, you can visit this free course, Learn C++ from Scratch.

Here’s what we’ll cover today:

  1. Brief History of C++
  2. Overview of C++ Tools and Software
  3. Introduction to C++ Language and Syntax
  4. C++ Frequently Asked Questions
  5. Resources for Beginners

Let’s get started!

1. Brief History of C++

Alt Text

A great way to get started with C++ is to learn about its history. C++ is one of the oldest programming languages, so there are many different versions. Having a sense of this history will situate you in the community of C++ programmers and give you a sense of its capabilities. It's always a good idea to familiarize yourself with past versions of a programming language, especially when you get into the professional workplace.

The C++ programming language was invented in 1979 by Bjarne Stroustrup while working on his PhD thesis at Bell Labs. C++ was designed to be an extension of the programming language C, hence its original name, “C with Classes”. Stroustrup’s goal was to add flexibility and OOP (object-oriented programming) to the C language. He included features such as classes, strong type checking, default function arguments, and basic inheritance. The name was changed to C++ in 1983, which derives from the ++ operator.

Alt Text

C++ was released for commercial use in 1985, but it was not yet standardized. In 1990, Borland’s Turbo C++ compiler was released, which added many new features. The first international standard for C++ was published in 1998, known as C++98. This included The Standard Template Library, providing common programming functions and data structures. Based on feedback, the committee revised those standards in 2003, and the updated language was renamed to C++03.

The language saw another revision in 2011 when C++11 was completed. This version includes features such as Regex support, new libraries, new syntax for loops, the auto keyword, and new container classes, amongst other things. Since that time, two more revisions have been released, C++14 and C++17, which featured major revisions. There is currently another update in progress, C++20.

2. Overview of C++ Tools and Software

Alt Text

In order to properly make C++ programs, you’ll need to be familiar with a few tools and softwares: a text editor, a C++ compiler, a linker, and libraries.

Text Editors

In order to write a C++ program, you need a text editor. Think of this like a blank Microsoft Word Document; it is where you will actually write your code. Any text editor will do, and there are even some that come built into your computer, but we recommend using a text editor designed for coding. There are many options out there, but some of the most common text editors for C++ developers are:

  • Notepad++: open-access, lightweight, simple
  • Atom: free, supports many languages, limited plugins
  • Sublime Text: $80, unique features, simple layout
  • Bluefish: lightweight, fast, multi-platform, supports many languages

Notepad++ text editor
Alt Text

Atom text editor
Alt Text

Compilers

A compiler goes through your source code to accomplish two important tasks: first, it checks that your code follows the C++ language rules; second, it translates your code into an object file. Some well-known compilers are GCC, Clang, and the Visual Studio C++ compiler. We don’t recommend Turbo C++, since it’s a bit out of date.

Linker

Once the compiler does its magic, the object file is sent to a linker program which completes three tasks: first, it combines all your object files into a single program; second, it links library files to your program; and third, it exposes any cross-file naming or reference issues.

Libraries

A library is essentially a prepackaged bundle of code that can be reused. The C++ library is called the C++ Standard Library, and this is linked to almost every C++ program. You can also add other libraries to your program if you have needs not met by the Standard Library. Curious about the C++ Standard Library? Learn more about it here.

Integrated Development Environment (IDE)

Many C++ programmers use an IDE instead of a text editor and compiler. An IDE is a one-stop-shop for C++ programming. It includes a text editor, linker, compiler, and libraries. There is no right or wrong compiler to use. It all comes down to your needs and what layout is best for you. Some popular IDEs are:

  • Code::Blocks: free, in-demand features, plugins by users
  • Visual Studio Code: open source, great features, cross-platform
  • Eclipse: open source, simple, cross-platform, need to install C++ components
  • Xcode: only for MacOS, useful Mac features
  • Netbeans: free, project-based templates, cross-platform

3. Introduction to C++ Language and Syntax

Alt Text

C++ is an object oriented programming language. This means that C++ programs are modeled around objects and classes, which you can control and manipulate by applying functions. OOP languages offer a clear structure to a program and help developers model real-world problems.

The language is designed to provide you with a lot of freedom and power, which is both good and bad. You’re in full control of how your system utilizes resources; there is no automatic memory management like in Java; you have the ability to choose between how memory is allocated (i.e. stack or heap); there is no interpreter in C++ to stop you from writing buggy code.

In order to get started with C++, you need to familiarize yourself with the syntax. This will pave the way for the rest of your C++ journey and help you create optimized programs that are safe and bug-free.

Let's look at some code
Looking at the code below, you may be wondering what all this is and what it means. Welcome to C++ syntax.

What is syntax? Syntax is like the grammar of a programming language. It is the basic foundation for everything you’ll write in C++. These are the rules that define how you write and understand C++ code. Let’s look at an example of some code to familiarize ourselves with the syntax.

#include <iostream> //header file library 
using namespace std; //using standard library

int main() { //main function
  cout << "Hello World \n"; // first object
  cout << "Learn C++ \n\n"; //second object with blank line
  cout << "Educative Team"; //third object 
  return 0; //no other output or return
} //end of code to execute
Enter fullscreen mode Exit fullscreen mode

The syntax explained

#include <iostream> is a header file library. A header file imports features into your program. We’re basically asking that the program copy the content from a file called <iostream>. This stands for input and output stream, and it defines the standards for the objects in our code.

using namespace std means that we are using object and variable names from the standard library (std). This statement is often abbreviated with the keyword std and the operator ::. The int main ( ) is used to specify the main function. It is a very important part of C++ programs and is associated with every C++ program you write. A function essentially defines an action for your code. Anything within the curly brackets { } will be executed.

cout is an object (pronounced see - out). In this example, it defines our outputs: the strings of words. We write a new object using cout on the second line. The character \n makes the text execute on a different line. Including two \n\n creates a blank space. By writing return 0, we are telling the program that nothing will return. We are only outputting strings of text. Note that we use the << operator to name our objects. The semi colon ; functions like a period.

Tip: Leave notes for yourself in the code using //. Any text after these dashes will not be executed. Leaving notes becomes increasingly important as you start to work with teams.

C++ Terms and Vocabulary

Now that we have a sense of what C++ code looks like, let’s define some of the terms we mentioned and introduce you to a few more.

Keywords

Keywords are predetermined names that can be used to identify things in your code. Keywords are identifiers for particular objects, variables, or actions. You can also make your own keywords. Here are a few examples of keywords: goto, float, public, class(1), int. Take a look at a complete list of keywords here.

Variables

Variables are like containers that store values. To declare a variable, you must give it a value and a type using the correct keyword. All variables in C++ need a name, or identifier. There are some basic syntax rules to follow when making identifiers.

  • Names are case sensitive
  • Names can contain letters, numbers, and underscores
  • Names must begin with a letter or an underscore
  • Names cannot contain whitespaces or special characters (!, #, @, etc.)
  • Names cannot use reserved keywords

There are six different types of variables:

Alt Text

Data Types

Data types are the classifications for different kinds of data you can use in a program. Data types tell our variables what data they can store. There are three data types in C++:

  • Primitive data types: these are the built-in data that you can use to declare variables. They include integer, character, boolean, floating point, double floating point, void, and wide character.

  • Derived data types: these are derived from the primitive data types. They include function, reference, array, and pointer.

  • User-Defined data types: these are defined by you, the programmer.

Strings

Strings are objects in C++. They are a set of characters within ” “ quotes, like our ”Hello World” string. Since they are objects, we can perform functions to them, like the len ( ) function, which determines the length of a string.

Operators

Operators are symbols that manipulate our data and perform operations. In C++, we can overload operators to make them work for programmer-defined classes. Overloading an operator basically means that an operator can have more than one function at a time. There are four kinds of operators in the C++ language:

Arithmetic Operators are used for mathematical operations. These work just like algebraic symbols.

cout << x + y // this adds x to y
Enter fullscreen mode Exit fullscreen mode

Assignment Operators are for assigning values to our variables

int x = 10 // this defines x as 10
Enter fullscreen mode Exit fullscreen mode

Comparison Operators compare two values

x <= y // determines x is greater than or equal to y
Enter fullscreen mode Exit fullscreen mode

Logical Operators determine the logic between values

x < 4 && x < 9 // will return true if both statements are true about x
Enter fullscreen mode Exit fullscreen mode

Objects

An object is a collection of data that we can act upon. An object in C++ has an attribute (its traits) and method (its abilities). You construct objects using a class. Think of this like a blueprint for an object.

You create a class using the class keyword. You must define an access specifier, such as public, private, or protected. The public keyword states that class is accessible from outside that class. Once you define your class, you can define your attributes and objects. Take a look below at an example of a class and object.

#include <iostream>
using namespace std;
class Dog //this is the name of our class
{
public: 
    string name = "rover"; //this is an attribute 
    string gender  = "male"; 
    int age = 5;

};

int main() {
  Dog dogObj; //here we are making an object of Dog class
  cout << "Dog name is: "<<dogObj.name<<endl; //by using . operator we can access the member of class
  cout << "Dog gender is: "<<dogObj.gender<<endl; //accessing the public members of class Dog in main()
  cout << "Dog age is: "<<dogObj.age<<endl;
}
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are blocks of code that run when they are invoked. They are the workhorse for your program and are used to perform operations and manipulations on your code. They are extremely important for code reusability and help to better modularize your code. Think of these like actions that you initiate. In C++, there are predetermined functions, like the main ( ) of our initial example.

To create a function, you have to give it a name (called the declaration) and parentheses ( ). You can then invoke this function at any point by using that name ( ).

There are a lot of ways to use functions. You can also attach return values to your functions, which determine if a function should output any information. The void keyword states that there will be no return. The return keyword, on the other hand, will call for a data type output.

Conditional Statements

These allow you to perform checks on whether a block of code should be executed or not. There are four conditional statements in C++:

  • if: a certain action will be performed if a certain condition is met

  • else: a certain action will be performed instead if that condition is not met

  • else if: a new condition will be tested if the first is not met

  • switch: tests a variable against a list of values

Loops

Loops are similar to conditional statements. They execute blocks of code as long as a certain condition is reached. There are two types of loops in C++:

  • while loops: this loop will continue to iterate through your code while a condition returns true.

  • for loops: this is used when you know the exact number of times you want to loop in your code

Now that you have a basic understanding of C++ syntax, let’s go over some FAQ and resources to get you started on your C++ journey.

4. C++ FAQ

Alt Text

How long does it take to learn C++?

Well it really depends on what is meant by “learn”. If you’re serious about this language, then your learning is never done. Developers can devote their entire career to C++ and still feel as though they have more to learn.

With that said, if you put in the work, you can learn enough C++ in 1-2 years and still be a great developer.

In short, there is no one right answer to this question, and it largely depends on your learning style, goals, educational plan, and prerequisite knowledge.

What is C++ used for?

C++ is focused on large system performance, so it is used in a wide variety of programs and problems where performance is important. This includes, but is not limited to, operating systems, game development, 3D animation, web browsers (it is used in Firefox and Chrome), software for offices, medical software, and more. C++ is used in all Blizzard games, most console games, Adobe Photoshop, Mozilla Thunderbird, PDF technologies, and MRI scanners.

What is the difference between C and C++?

The main difference is that C++ is an object-oriented language while C is a procedural programming language. C does not allow for functions to be defined within structures, while C++ does. C and C++ also have some different functions, keywords, and memory allocation procedures.

What is the difference between C++ and C#?

C# is a much newer language (created by Microsoft in 2000), and is built off of C++, so they share similar syntaxes. One major difference between the two is their flexibility. C# shows you compiler warnings as you write code to help reduce errors, while C++ does not. C# only runs on Windows OS, while C++ can be run on any platform (MacOS, Linux, Windows, etc.). C# is great for mobile and web applications, while C++ is known for performance and programs that work directly with hardware. They also handle memory management a bit differently.

Is C++ similar to other programming languages?

C++ is the foundation for many other object-oriented programming languages like Java, JavaScript, Python, PHP, Rust, C#, and more. Learning the syntax of C++ will make it easier to learn other programming languages.

What’s the best programming language to learn?

There’s really no one answer to this question, and every developer will tell you something different. It depends on what kinds of jobs interest you, your prerequisite knowledge, and your career goals. The truth is, every programming language is challenging to learn, but you are capable of learning any of them. A few benefits to starting with C++ are: the syntax is widespread, you’re forced to think about memory management, and it introduces you to multiple programming paradigms, which is a great way to expand your thinking and search for new approaches to problems.

Is C++ in demand? Does C++ pay well?

Yes, and yes. If you put in the time, you will be rewarded. C++ developers already have high-paying salaries, and it’s expected that the salary will grow in the coming years. C++ is experiencing a resurgence of popularity since it is great for robust applications like self-driving cars and VR. Since C++ has a steeper learning curve than most languages, the skills you obtain will set you apart when you’re applying to jobs. Some developers argue that C++ will open doors that other programming languages just can’t.

Alt Text

5. Resources for C++ Beginners

Alt Text

Since C++ is such a mature language, there are a lot of resources available to new learners. With so many free resources out there, you can take charge of your learning. Here’s a list of the most useful, free resources for a C++ beginner.

Tutorials/Online Courses

Educative’s Free Learn C++ From Scratch Course:
Working with experts in the field, Educative has put together a free, one-stop-shop C++ tutorial for beginners. This text-based, highly-interactive course begins with an introduction to the fundamental concepts and then proceeds to cover more complex ideas such as multidimensional arrays, loops, inheritance, and more.

Every chapter is loaded with hands-on practice, visualizations, and examples to make your learning easier. This course is also part one of our carefully designed learning track. Once you complete our from Scratch course, you’ll know what to learn next with the click of a button!

Community and Forums

  • C++ Meetups: This is a free platform that connects groups of people who are interested in the same topics and hobbies. Search for local C++ groups and communities in your area.

  • Dev.to C++ Community: This is an open access forum and resource for developers of all skill levels to get help, ask questions, and solve problems. The C++ community on Dev.to is very active, so you can turn here for articles, FAQ, and inspiring ideas.

  • GitHub C++ Community: GitHub is one of the most popular online communities and forums for developers around the globe. Here you can learn from other C++ developers, ask questions, and contribute to projects.

  • StackOverflow C++ Community: StackOverflow is an open-access forum where you can access questions and discussions on anything related to C++. StackOverflow is one of the best online communities for developers, and it is loaded with information to help you at any stage.

Online Training/Practice

  • Edabit: Edabit is like Duolingo for developers. You can search for online challenges in C++ and train in their embedded coding environments. The exercises range in difficulty and are searchable by tag.

  • Google Code Jam: Learn by competing with others on C++ projects. Code Jam is the longest running coding competition where you can solve puzzles and challenges using your new C++ skills.

  • Project Euler: This is a great place to train your C++ skills by solving mathematical problems. The problems range in difficulty so you can keep using this platform as you grow in confidence and skill.

Style Guides and Libraries

  • Bjarne Stroustrup’s Website: The creator of C++ maintains a website where he posts lots of useful information about the programming language, including free access to his book and a complete glossary of terms. This site also documents the history of the language, research papers relating to computer programming, interviews, and more.

  • C++ Lecture Slides: This site offers access to a variety of lecture slides relating to C++ programming. This site also offers information about libraries, the STL, and software tools.

  • The Free Country C++ Libraries: The Free Country offers free C++ libraries and source code. This will be more useful once you start building your own programs, or explore the libraries to familiarize yourself with C++ syntax and usage.

  • Google C++ Style Guide: Style guides are written by experienced programmers, and they outline how to maintain certain programs. A style guide is a great reference if you want to learn formatting rules and syntax.


Get started today learning this exciting, popular programming language! Your journey to becoming a C++ developer begins today.

Happy learning!

Top comments (3)

Collapse
 
monoblaine profile image
Serhan Apaydın

C# only runs on Windows OS

This is incorrect since 2016.

Collapse
 
saint4eva profile image
saint4eva

C# and .NET are cross platform - they run on everything.

Collapse
 
saint4eva profile image
saint4eva

Dear author you need to update your article. And you made so many incorrect statements.