C++ Tutorials: What Newcomers Need To Know

C++ Tutorials: What Newcomers Need To Know

After all these years, C++ still commands significant popularity in the industry for a good reason. This is a powerful language that can give you amazing tools to build highly optimized programs. Give our C++ tutorials a try, and you are planning to dive into it.

C++ Introduction

The C++ programming language (pronounced C plus plus) was created by the Danish computer scientist Bjarne Stroustrup in 1979 when he was working at AT&T Bell Labs.

The original intent was to create an extension of C. During its development, C++ has become a general-purpose, object-oriented language that has both high-level and low-level features. For the most part, C++ is a superset of C (even though many modern C features aren’t available in C++).

The International Organization for Standardization (ISO) working group began its effort to standardize C++ in 1998. The result was a standard ISO/IEC 14882:1998, or as it is informally known, C++98. Since then, there have been 5 revisions of this standard, the latest C++20.

Future revisions are set to be released every 3 years. These revisions add features, improvements, and bug fixes to the official C++ specification. Support for these standards varies between compilers.

Features Of C++

Object-Oriented

Even though you can write C++ programs in other styles, object-oriented programming (OOP) is well-supported by this language. This is how C++ has been adopted widely since its introduction, and OOP is also one of the most notable missing features in C.

High-Performance

C++ is a popular choice when high loads or low-level support are top priorities. This language sits closer to the hardware and native code than higher-level solutions like Python. It is more optimized to produce high-performance programs, allowing programmers to fine-tune their code.

Platform-Dependent

C++ programs are machine-independent, meaning you can execute them (with no or little changes) in different machines rather than just the one they are compiled on. However, this advantage doesn’t apply beyond machines of the same platforms.

Unlike languages like Java, C++ isn’t platform-independent. You can’t just develop a C++ program for Windows and expect it to run flawlessly in Linux or macOS. The compiled machine code is different for each underlying operating system.

Portable

C++ can run pretty much on any platform and device. From mainframes to Raspberry Pi boards, there are always tools for developing C++ in those environments.

Rich Ecosystem

C++ has never stopped being a popular language. Decades of development and adoption in the industry have created a vast number of libraries and toolchains. You are spoilt for choice when picking tools to start learning C++.

The C++ community is also one of the most active in the developer world. This gives you excellent access to guides, tutorials, and support.

Where You Can Use C++

You can find popular programs written in C++ in most application domains, from low-level software to user-facing applications:

  • Device drivers
  • Operating systems
  • Browser
  • Office suites
  • Games
  • Multimedia software
  • Science research
  • Web servers
  • Embedded systems

C++ has a similar design philosophy to C, which tends to trust the programmer. They assume the programmer knows what they are doing and give them more freedom.

This can be both amazing and dangerous. More often than not, programmers write code that doesn’t make sense. That is why you should be aware of the common pitfalls and risky practices of C++ to write it efficiently and safely.

Getting Started With C++

Install A Text Editor / IDE

You will need a text editor or an IDE that supports C++. Popular choices include:

Editors

  • Visual Studio Code
  • Sublime Text
  • Notepad++
  • Vim
  • Emacs

IDEs

  • Visual Studio
  • CLion
  • Eclipse
  • Xcode
  • Code::Blocks

In this tutorial, we will use Visual Studio Code (VS Code). It is a popular free, cross-platform editor developed by Microsoft.

Go to the download page and select the installer for your operating system. Run the installer and follow its instructions to install VS Code.

Open VS Code, and press the shortcut Ctrl+P. Paste this command and press Enter:

ext install ms-vscode.cpptools

This is the official C/C++ extension from Microsoft, which offers editing and debugging features.

Install A Compiler

C++ is a compiled language, meaning you must compile your code into native code before it can be run. There is no official compiler for C++, but our recommendations are GCC on Linux, Clang/LLVM on macOS, and Mingw-w64 on Windows.

Linux

Most Linux distributions have official packages and meta-packages that contain GCC.

Ubuntu/Debian

sudo apt-get install build-essential

Fedora

sudo dnf install make automake gcc gcc-c++

openSUSE

sudo zypper install -t pattern devel_basis

After installing GCC, verify the installation of the C++ compiler with this command:

g++ --version

It should print the version of the compiler provided by GCC.

g++ (GCC) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.

macOS

Clang comes with Xcode, Apple’s official IDE. You can also install LLVM with Homebrew to get this compiler:

brew install llvm

If Homebrew isn’t available, you can get the installer directly from LLVM’s download page.

Windows

Go to the download page to get the installer and run it to install Mingw-w64.

Your First Program

This is a simple program in C++.

#include <iostream>

int main() {
   std::cout << "Welcome to LearnShareIT";
}

You have to compile the code with GCC or Clang:

g++ welcome.cpp -o welcome
clang++ welcome.cpp -o welcome

The compile binary will produce this output:

Welcome to LearnShareIT

We are going to explain the example line by line:

  • #include <iostream>: this is a directive whose job is to tell the preprocessor to include specific files. In this case, it includes the header file for basic input/output operations in C++.
  • int main(): this is how you declare a function in C++. It specifies the returned type (integers) and the name of the function (main). Everything between the brackets {} belongs to that function. The main function is the designated starting function of a C++ program.
  • std::cout: a predefined stream object in the iostream header file used to write standard output streams.

C++ Tutorials – Summary

C++ is still a great choice when you need to develop heavy-duty solutions or when low-level hardware support is needed. It supports OOP out of the box with great built-in tools for developing high-performance programs. Check out our C++ tutorials to learn more about other features of this amazing programming language.

Leave a Reply

Your email address will not be published. Required fields are marked *