your image

Learn C++ from Scratch: The Complete Guide for Beginners

Amanda Fawcett
www.educative.
Related Topic
:- C++ language programming languages

Learn C++ from Scratch: The Complete Guide for Beginners

Oct 29, 2019 - 16 min read

 

Amanda Fawcett

 

 

 

C++ from Scratch Series

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.

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.

Here’s what we’ll cover today:

Let’s get started!


 

Learn C++ for free with hands-on practice

Get a handle on one of the most popular programming languages in the world.

Learn C++ from Scratch

 

Brief History of C++

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.

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.

 

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 update 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.

 

Overview of C++ Tools

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++ Image Source: https://notepad-plus-plus.org

 Atom Source Image: https://atom.io/packages/goto-definition

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 C++ Standard Library.

 

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

 

Introduction to C++ Language and Syntax

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.

Enjoying the article? Scroll down to sign up for our free, bi-monthly newsletter.

 

Let’s look at some C++ 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.

1

2

3

4

5

6

7

8

9

#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 exectute

 

 

 

 

Run

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. 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.


 

Keep the learning going.

Start with a simple hello world program and proceed to cover core concepts such as conditional statements, loops, and functions in C++, before moving on to more advanced topics like inheritance, classes, and templates, along with much more.

Learn C++ from Scratch

 

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

 

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:

int myNum = 5;               // Stores integers (whole numbers)float myFloatNum = 5.99;     // Stores decimals loating point numberdouble myDoubleNum = 9.98;   // Floating point numberchar myLetter = 'D';         // Stores single charactersbool myBoolean = true;       // Stores Boolean, values with a true or false statestring myText = "Hello";     // Stores strings of 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 length ( ) 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.

  • Assignment Operators are for assigning values to our variables

  • Comparison Operators compare two values.

  • Logical Operators determine the logic between values

cout << x + y // This adds x to y
int x = 10 // This defines x as 10
x <= y // Determines x is greater than or equal to y
x < 4 && x <9 // Will return true if both statements are true about x

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#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;

}

 

 

 

 

Run

 

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.

 

C++ FAQ

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.

 

 

Next steps for learning C++

Congrats! You’ve learned the basics of C++! You’re well on your way to becoming a hire-able C++ programmer.

Educative’s free C++ tutorial is the ideal place to start for beginners. Educative’s Free Learn C++ From Scratch is a text-based, highly-interactive course that begins with an introduction to the fundamental concepts and proceeds to cover more complex ideas such as multidimensional arrays, loops, inheritance, and more.

Once you complete our from Scratch course, you’ll know what to learn next with the click of a button! Your journey to becoming a C++ developer begins today.

 

Continue reading about C++

 

WRITTEN BYAmanda Fawcett

Comments