close

Learn C++ Programming

The Definitive Guide

C++ ranks 4th in popularity according to 2016 IEEE spectrum Top Programming Language ranking. Learning C++ is a wise investment for all programmers.

This guide answers all your questions related to C++ on what is it, when is it used, why is it used and how to get yourself started with it.

the C++ conference 2017 We are supporting CppCon 2017 (the C++ conference) happening on September 24-29, 2017 in Bellevue, Washington. See you there.

What is C++ (programming language)?

https://www.programiz.com/

“C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.”

In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Stroustrup in 1979.

Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today.

Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.

History of C++

While Bjarne Stroustrup was working in AT&T Bell Labs in 1979, he faced difficulties in analyzing UNIX kernel for distributed systems. The current languages were either too slow or too low level. So, he set forward to create a new language.

For building this language, he chose C. Why C? Because it is a general purpose language and is very efficient as well as fast in its operations.

He used his knowledge of object-oriented model from SIMULA and began working on class extensions to C. His aim was to create a language with far higher level of abstraction while retaining the efficiency of C.

This new programming language was named C withClasses, but was later renamed to C++ (++ refers to the increment operator in C).

C++98

When C++ was first released in 1985, there were no official standards released. It was only until 1998 that C++ was first standardized which was known as C++98.

C++03

In 2003, a new version of C++ standard was published. C++03 wasn’t really a new standard altogether but a bug fix release identified with C++98 “to ensure greater consistency and portability”.

C++11 (C++0x)

The next major standard for C++ was released in 2011 and it was named C++11. Since, C++ committee was sure this update would be released within 2009, they unofficially named it C++0x. Later, when they didn’t, Stroustrup joked that C++0x went hexadecimal - C++0xB (C++11). Nice save.

C++14 (C++1y)

C++14 is the current iteration of C++ released in 2014. Like C++03, it included mainly bug fixes and simple improvements to C++11.

C++17 (C++1z)

The supposedly next iteration to C++ which is planned to be rolled out in 2017. It is expected to have many new features. Most of the features planned for this version are already completed.

Features of C++

Being a general-purpose language, C++ is undoubtedly feature-rich. Going through all the features will take you some time but, as a beginner, below are the most important features you should know.

  1. C++ is fast

    Since, C++ is an extended version of C, the C part of it is very low level.

    This offers a huge boost in speed that high level languages like Python, Java don’t give you.
     
  2. C++  is statically typed

    C++ is a statically typed programming language.

    In simple terms, C++ doesn’t allow the compiler to make assumptions about the type of data e.g. 10 is different from “10” and you have to let C++ know which one you are talking about.

    This helps the compiler catch errors and bugs before execution of the program.
     
  3. C++ is a multi-paradigm programming language

    C++ supports at least 7 different styles of programming and gives developers the freedom to choose one at their will.

    Unlike Java and Python, you don’t need to use objects to solve every task (if it’s not necessary).

    You can choose the programming style that fits your use case.
     
  4. Object oriented programming with C++

    Object oriented programming helps you solve a complex problem intuitively.

    With its use in C++, you are able to divide these complex problems into smaller sets by creating objects.
     
  5. Power of standard library (Standard template library - STL)

    The power of C++ extends with the use of standard libraries contained in it.

    These libraries contain efficient algorithms that you use extensively while coding.

    This saves ample amount of programming effort, which otherwise would have been wasted reinventing the wheel.

5 Reasons Why you should learn C++?

https://www.programiz.com/

This is one of those questions you need to ask before starting any programming language. It helps you understand the scope of the language, the real world usability and how far you can get with it in terms of support. Here are top 5 reasons you should learn C++.

  1. C++ is irreplaceable

    With the use of C++ in development of modern games, operating systems, browsers, and much more, it is safe to say that C++ is irreplaceable.

    Many major applications like
    • Adobe Products like Photoshop, Illustrator, InDesign
    • Amazon - one of the biggest e-commerce sites
    • Autodesk products for Computer Aided Design
    • Facebook - social networking site are heavy C++ centric products.
    Moreover, the fact that there’s a huge community improving C++ on every iteration means that it is only expected to be used even more in the coming future.
     
  2. You learn the internal architecture of a computer

    Since, C++ is a middle level language, you will write code that interacts directly with the internal hardware of the computer.

    You’ll learn how the computer memory really works, how information is stored in them, how you can retrieve them and so on.

    It is sure to expand your knowledge on the architecture of the computer.
     
  3. Over 600,000 C++ repositories on Github

    Github, the leading open source collaboration platform, has over 600,000 repositories for C++ alone.

    This metric itself proves the worth of C++ in the open source community as well.

    Be it gaming, graphics, windows applications, you can find tons of great open source projects extensively used today. And, you can always create your own.
     
  4. 60% StackOverflow Answer rate and active community

    Likewise, with over 400,000 C++ questions asked on StackOverflow, the number one Q&A platform for developers, more than 60% questions have been answered.

    The number of questions asked and the percentage of them answered shows the interest and active support for C++ today.

    So, you can expect many great developers to help you solve real-life problems using C++.
     
  5. C++ job opportunities and salary

    C++ developers can expect an average of yearly $100,000 salary with over 7,700 jobs advertised every month.

    The requirement of jobs comes mostly from game development, rendering engines and the windows applications.

4 Things to Know Before you Code in C++

https://www.programiz.com/

Now that you know what C++ is and how vast its scope ranges to, it’s time to get started with it.

But, before you start, there are a couple of important things you should know.

Below are the 4 most important things you need to know.

  1. C++ cannot be learnt in a day

    Learning any language takes time and that holds even more truth for C++.

    If you are here to learn C++ in a day, then you’re going to end up facing failure.

    To be honest, there’s no definite time to complete learning C++ and someone who says they can, are simply lying.

    You only start learning with regular practice and dedication. So, I suggest you to invest valuable time learning C++.
     
  2. Learning C++ can be hard.

    Since it’s not a high level language, learning C++ can get overwhelming when you start and you’d need to be prepared to put thoughtful hours to learn the basics.

    But, there’s no need to panic.

    We offer plenty of resources and easy C++ tutorials available on Programiz to get started for beginners.

    Also, there are numerous support communities that will help you when you are stuck.
     
  3. No, you don’t need to learn C before C++

    People have different theories whether one should learn C before C++ or not. If you ask me, it isn’t a must. You can easily start with C++ and that’s what I did myself.

    If you already know C, you will have a head start in learning C++ as they have similar attributes like syntax and semantics.
     
  4. Don’t wait for the next C++ release

    Since, a new iteration of C++ is due late 2017, a lot of people ask whether it would be better it they wait until the next release before learning C++ or not.

    The answer is NO.

    Though there are a lot of additions and improvements planned for the next releases, the core principles are the same. So, it would be wise to invest your time now.

Compile and run C++ programming on your OS

https://www.programiz.com/

C++ is completely free and readily available on all platforms.

Follow the tutorial below for running C++ on your computer.

There are multiple compilers and text editors you can use to run C++ programming. These may differ from system to system.

If you want a quick start, you can also run C++ program online.

Run C++ program Online

The most effortless way to program C++ has to be online and there are several online compilers to choose from.

There are several sites that allows you to run C++ programming online. The one I prefer is ideone.com.

To run C++ programming in Ideone.com, select C++ language at the bottom of the editor, write your code and click Run.

I don’t recommend using online compilers other than testing as it is very limited in use (you can’t work with files or take inputs from user).

To unlock the full potential of C++, it’s recommended to install compiler and run C programs from your system.

Here’s is the easiest way to run C++ programming in your system (Mac OSX, Windows and Linux) that suit a beginner’s purpose.

Run C++ Programming in Mac OS X

The easiest way to run C++ Programming in Mac OS X is by downloading XCode.

  1. Go to download page of apple developer site.
    Download XCode to run C++ in Mac OSX
  2. Click the download Xcode link. It’s recommended to download the latest version even if it’s in beta.
  3. When download is completed, open Xcode and follow the wizard to install it. You might want to put the Xcode in Applications for future use.
  4. Then, open Xcode and go to File > New > Project
    Navigate to project in Xcode
  5. Under OS X section, choose Application, choose Command Line Tool and hit next.
    Command line tool in XCode
  6. Provide the Product Name, for example: Hello. And, choose C++ under Language section. Then, click next.
    Choose C++ language in XCode
  7. Choose the location where you want to save the project in your Mac. You can uncheck Create Git repository button and click create.
  8. Navigate to main.cpp file in the screen where you can see default code provided by XCode.
    Run C++ Program in XCode
  9. Change the code as you wish. And finally to run the code, go to Product > Run or simply hit Cmd + R.
  10. By default, you will see the output at the bottom of your screen.

Run C++ programming on Linux

To run C++ programming on Linux, you need:

  • a compiler. We’ll install GNU GCC compiler which is good for beginners.
  • development tools and libraries.
  • a text editor (gEdit works just fine for our purpose). Or, you can download text editor of your choice.

Here’s a step by step guide to run C++ in Linux (Ubuntu, Debian, CentOS, Fedora, Redhat and Scientific Linux):

  1. Open the terminal and issue the following command.

    For Ubuntu and Debian distribution:
    $ sudo apt-get update
    $ sudo apt-get install build-essential manpages-dev
    For CentOS, Fedora, Redhat and Scientific Linux:
    # yum groupinstall 'Development Tools'

    This installs GNU GCC compiler and related tools on your system.
  2. To verify if gcc compiler is installed, issue the command
    $ gcc --version
    If you get output similar like this, gcc is correctly installed on your system.
  3. Open the text editor of your choice and save a file with .cpp extension. I made hello.cpp file using gEdit.
    If you are a linux wizard, feel free to use vim or emacs. Any editor is fine but, don’t forget to use .cpp extension; it’s important.
  4. Switch to the directory where the file is located. And, issue the following command.
    $ g++ program-source-code.cpp-o name-of-your-choice
    Here, program-source.code.cpp is the filename you chose before. And, name-of-your-choice can be any name you prefer. In my case, I issued the following command.
    $ g++ hello.cpp -o hello
  5. If there is no error, an executable file named is created; hello is my case.

Finally, you can see the output using following command.

$ ./hello

As you might have imagined, you need to use the name of executable file you chose before. Also, you need to use path to the execute file if you are in a different directory.

Run C++ in Windows (XP, 7, 8 and 10)

To run C++ Programming in Windows, you’d need to download Code::Blocks.

There are others available as well but Code::Blocks makes installation a piece of cake.

It’s easy, simple and developer friendly.

To make this procedure even easier, follow this step by step guide.

  1. Go to the binary release download page of Code:Blocks official site.
    Download Code::Blocks in Windows
  2. Under Windows XP / Vista / 7 / 8.x / 10 section, click the link with mingw-setup highlighted row either from Sourceforge.net or FossHub.
  3. Open the Code::Blocks Setup file and follow the instructions (Next > I agree > Next > Install); you don’t need to change anything. This installs the Code::Blocks with gnu gcc compiler, which is the best compiler to start with for beginners.
  4. Now, open Code::Blocks and go to File > New > Empty file (Shortcut: Ctrl+Shift+N)
    Create new empty file in CodeBlocks
  5. Write the C++ code and save the file with .cpp extension. To save the file, go to File > Save (Shortcut: Ctrl+S). Important: The filename should end with .cpp extension, like: hello.cpp, your-program-name.cpp
    Create .cpp file to run C++
  6. To run the program, go to Build > Build and Run (Shortcut: F9). This will build the executable file and run it.

If your program doesn’t run and if you see error message "can't find compiler executable in your search path(GNU GCC compiler)",  go to Settings > Compiler > Toolchain executables  and click Auto-detect. This should solve the issue in most cases.

Your first C++ program

https://www.programiz.com/

Now you have installed the compiler based on your OS, it’s time to write your first C++ program.

“Hello World!”

Your first C++ program will be a “Hello World!” program.

You might have noticed “Hello World!” being the first program while starting out with any programming language. This is because:

  • It is a standard check to see whether everything is working fine or not.
  • There will be very less code to start with.
  • The less code makes it intuitive for the beginners to get acquainted with the language.
  • The code is enough to learn the basic syntax and semantics of the language.

So, let’s get coding.

#include <iostream>
using namespace std;
int main()
{
    cout<<"Hello World!";
    return 0;
}

The program prints Hello World! in the output screen.

How the program works?

Now, let’s dissect the above code. The code is divided into six major parts:

  • #include <iostream>
  • using namespace std
  • ;
  • int main() { }
  • cout << “Hello World!”;
  • return 0;
  1. What is #include <iostream>?

    If you’ve already written code in C language before, you might seen this line of code before. If you haven’t, don’t worry we’ll cover it now.

    This statement includes the header file into the application so that you are able to use the operations included in them. Also, you can create your own header files and include them in your program using the #include.

    What is iostream?

    iostream is what you call the header file. It is a standard C++ input/output library file.
    It comes packaged with the compiler/IDE and contain mechanisms to get the information from the user and print same or added information to a file, screen or any other media.

    What is #include?

    The #include iostream file, into the program. This ensures that now you’re able to use the operations, iostream operations (like: taking input from user, displaying output on the screen), in the program.

  2. What is using namespace std;”?

    The statement is intuitive in itself, you are “using” the “namespace” “std” in your file.
    We use the namespace std to make it easier to reference operations included in that namespace.
    If we hadn’t used the namespace, we’d have written std::cout instead of cout. This tells the compiler that every cout is actually std::cout.

    What’s a namespace?

    It’s a region where your code resides. It limits or expands the scope of your code to one or more files.

    Why do you use namespace?

    Like two persons can have the same name, variables and functions in C++ can have same names as well. The use of namespace is to avoid the confusion of which variables/functions you are referencing to.

    What is std?

    std is a standard namespace used in C++.
  3. Semicolon ”;”

    Ask any C++ programmer and they will tell you at least one horror story related to the semicolon ; .

    The semicolon is a terminal. It terminates a statement. When missed or incorrectly used, it will cause a lot of issues.
  4. int main() { }

    As the name suggests, it is the main function of the program. The code inside { } is called the body and is executed first when you run your C++ program.

    It is one code that is mandatory in a C++ program. If you just have this line of code alone, your program will be valid.
  5. cout << “Hello World!”;

    This statement prints “Hello World!” onto the output screen.

    The cout is an object of standard output stream. What this means is, it outputs/prints the data after << , i.e. Hello World! into a stream (in this case, the output screen).

    What is a stream?

    Stream is basically a sequence of objects, usually bytes. It can describe files, input/output terminal, sockets, etc.
    What is <<?

    << is the insertion operator used to write formatted data into the stream.
  6. What is return 0;?

    This statement returns 0 ‘zero’.

    This is called a return statement. It isn’t mandatory to return anything from the main() function but is rather a convention. If not return, the compiler returns a status automatically.

    Why zero in return statement?

    It denotes Exit status of the application that basically the tells system “The program worked fine.”

How can you learn C++?

https://www.programiz.com/

Now that you’ve grasped the basics of starting out on C++, you should aim to expand on it with good tutorials and dedication from your side.

Learn C++ from Programiz

We, at Programiz, offer you easy-to-follow tutorials and examples to help you learn C++ programming from scratch.

The tutorials are designed for beginners absolute who do not have any prior knowledge of C++ programming (or, any other programming languages).

Our tutorials will provide you the perfect platform to enhance your programming skills and apply it to real life.

It is a must for you to have a good book in hand if you want to better yourself in programming.

It won’t just familiarize you with the programming language itself but also teach you to write good code, follow standard principles and understand core programming concepts.

Here are the top 2 books we feel is best for you.

 

5 Tips to better yourself in C++ Programming

https://www.programiz.com/

As you consume more content on C++, you’ll gradually understand there is no one way to get something done.

  1. Learning by doing

    Whether you follow our tutorials or from any other media, practice what you’ve just gone through. Only practice will make you a better programmer.

    Don’t just copy code and run it. Take some time to think what the code actually does. Replicate it on your system and see what errors occur and most importantly, learn from them.

    Only then will you better yourself as a developer.
  2. Follow C++ standards

    It is really important to follow a good C++ standard when you start programming. This provides a set of easy rules to follow for a particular purpose in a particular setting. For beginners, it gives you one less freedom when you start out.

    Take a look at IsoCPP’s coding standard FAQ and also follow the C++ Core Guidelines.
  3. Read others C++ code

    Join Github’s open source projects and read others code. This can be overwhelming at first when you see all the code in the project. You can use Code Whittling to start small and only focus on one thing at a moment.

    You’ll not only learn other’s style of coding but you’ll also understand how they think.
  4. Break things

    Don’t be afraid to break things the way it is. You’ll be amazed to find how much you can learn from the broken code filled with errors.

    Errors are developers best friend. Understand what the error is about. Follow the error trail that takes you to the root of the issue, fix them and learn from them.
  5. Join C++ communities

    Get help from others. There are tons of great C++ communities that will help you solve real life problems and most importantly, become a better developer.

    Some of them are:
    • StackOverflow - Most Popular programming Q&A site on the web
    • Codechef - Practice questions, challenges and a large community of programmers
    • CodeProject - For those who code, with in-depth articles and huge community of coders

Final Words

C++ is a great language to start your programming journey with. The experience will take you a long way in becoming a great developer. So why wait. Get started by visiting any of the tutorials below.