C++ Tutorial For Beginners




Looking for a C++ tutorial? 
    
Maybe you're new to the programming world and you were told to learn C++ first by either a friend or teacher.

Whatever your reason for being here, you've come to the right place. 

This tutorial covers the basics of this programming language known as "C++" and it's designed for beginners and anyone looking to refresh their memory of the language.

So let's head straight in.

First of all, 

What is C++?

       C++ (short for “C-Plus-Plus” or Cpp) is a middle-level programming language used mostly in game development but can be used pretty much anywhere for various projects.

       It is usually recommended by professionals as the first programming language to learn because it has all the fundamental programming concepts embedded into it.

NOTE: C++; though related to, is NOT C or C#. The difference between them would be discussed later.

Next, 

How/where do I run a C++ program?

C++ is a compiled language and hence needs a program/software known as a compiler to first convert our C++ codes into a language (usually binary) the computer can understand for execution.

There are various C++ compilers out there. Some of them are:

·      Apple C++. Xcode

·      Bloodshed Dev-C++

·      Clang C++

·      Cygwin

·      Mentor Graphics

·      MINGW

·      IBM

·      GNU CC Source, etc.

Then, 

How do I write C++ codes?

C++ codes are written inside C++ files. 

These are basically text files with the “.cpp” file extension, which means that you can use any text/code editor such as Notepad, Notepad++, Sublime Text, Visual Studio Code, Brackets, Bare Bones, Text Wrangler and Gedit or an IDE (Integrated Development Environment) such as Code Blocks to create your C++ codes in a matter of minutes. 

However, as mentioned earlier; a compiler is required to execute the codes written so make sure you download a compiler (specifically a C++ compiler) alongside your code editor or IDE.

c++ file extension
C++ file extension



C++ Setup

       C++ is platform-independent, which means it can run across a number of platforms such as Windows, Mac and Linux.

The various setups for Windows and Mac would be discussed next. 

Mac and Linux have a similar setup structure and hence you can use the Mac setup instructions on your Linux system.

Note that the setup is based on the Code Blocks IDE, however if you have a favorite text/code editor already, you can stick with that. Just make sure to download a C++ compiler before you get started.


Windows Installation

1.   Head on to www.codeblocks.org in your browser.

2.   Click on “Downloads”.

3.   Click on “Download the binary release”.

4.  Click on the first link. It should say “Windows XP/10/7/8” etc.

5.   Click on the fourth link. It should say “codeblocks-[version] mingw-setup.exe”. This setup installs Code Blocks AND that C++ compiler we’ve been talking about.

6.  Open up where you saved the application and install it using the default settings.

7.   Start using Code Blocks.

Mac Installation

1.   Head on to www.codeblocks.org in your browser.

2.   Click on “Downloads”.

3.   Click on “Download the binary release”.

4.   Click on the fourth link. It should say “Mac OS X”. This should open up into another page.

5.   Scroll to the bottom. You should see a zip file and next to that, there would be a “Download from” table. Click on the link there.

 6.   Once Code Blocks is downloaded unto your computer, navigate to your Downloads folder. Double-click it.

7.   Extract Code Blocks application from the zip file into your Downloads folder if it isn’t done automatically.

8.  Drag and drop the application onto your “Applications” folder.

9.   Start using Code Blocks.

The next step is to get a C++ compiler. 

Mac computers usually come with a default C++ compiler and you can check to see if you have that compiler by following the follow steps:

1)  Click on the search bar on your desktop. It should be at the top-right corner.

2) Type in “Terminal” and open up the terminal window.

3)  Type in “gcc -v” in the Terminal and hit enter. If you have the compiler installed already, you should see “Configured with: ” and some other information. 

If you don’t see this, follow the next steps to install it.

a.   You can clear up the terminal window by typing in “clear” and hitting “Enter” on the keyboard or leave it as it is, it’s up to you.

b.  Type “xcode-select --install” and hit enter. If an error occurs, check to see if you have an internet connection and try again.

c.    Check if the compiler is properly installed by following the steps above.

And you’ve successfully installed your code editor and a C++ compiler. Now you’re ready to start programming in C++.


C++ Basics

       In this section, we’ll deal with all the basic concepts in C++. First of all, we’ll discuss the syntax of the language.

Syntax is basically the “grammar” of a computer language. 

For example, think of a human language (such as English or Spanish). We have various grammatical rules for making sentences in English. These rules, if followed, lead to easy communication between individuals. The same goes for computers.

       Computers cannot understand human languages and although these languages are written in English, they are translated into the binary representation of that language by either the computer itself or with the aid of something called a “compiler”.

A compiler is a special software that translates human-based codes of certain programming languages into a language the computer can understand for execution. 

C++ needs this special software so the computer can understand the C++ commands as C++ is a compiled language as we mentioned earlier.

So when we talk about syntax in C++, we’re referring to the already-set rules in C++ with which a computer would use to identify the language and carry out the tasks assigned.

c++ syntax
C++ Syntax

C++’s syntax is very straight-forward and easy to grasp. It can be divided into three smaller parts for easier assimilation when working with variables.

These are the:

1.   Type Statement: This part tells C++ what kind of variable we’re working with. This is necessary because C++ in addition to being a compiled language is also a static language, which means that the type of a variable CANNOT be changed at runtime (when the code is being executed). This basically means that if we set the type of a variable to be string (text), we cannot put a number or any other value that’s not a text.

2.  Variable Naming: A variable can be likened to a box. It’s where we store some value in the computer’s memory for future use. The variable name is like a label we put on that box so we can access the box later.

Imagine you’re moving from your current house to a new one. 

You start packing your things into boxes right? Now imagine you don’t put labels on the boxes. You’d have to open up EVERY box just to find where you stored maybe your favorite comics. 

This is why we use labels (or variable names) so we can easily know that the box labeled “Comics” is where we stored all our comics.

3.  Value assignment: Since a variable is just a box where we store information, we need to put in some actual stuff in there. This is known as the value of the variable. We can either assign a value to a variable on the same line we declared it in by using an equal to sign (=) or we can assign a value later when we need to make use of the variable.

Value/Data Types In C++

   So now you know that we can store different values in variables but you might be wandering, “what are the types of these values or data that we store?”

   There are lots of value types we can store in variables. Some include:

·      Characters: We can use the “char” keyword before our variable name to tell C++ that we want to store a single character in this variable. This is done like so:

     storing a character in c++

·      Strings: Strings are just a collection of different characters in a sequence. It can be likened to plain text and we set the data type of this variable by using the “string” keyword like so:
      
     storing a string in c++

NOTE: When working with single characters, we use single-quotes but when working with a sequence of characters (a string); you can use either single or double-quotes.

·      Numbers: Most times in your programs, there’s going to arise a situation where we don’t want to  store text but a number; either the age of a user or the rating the user gave us, etc. We can do this by typing in the type of number we’re storing.

In C++, there are two basic number types. These are:

a)  Integers: Popularly known as “ints”, these are number types that are of whole numbers, e.g. the age of a person. These numbers do not contain decimal figures.

b) Floating Point Numbers: Popularly referred to as “floats” are numbers which contain decimal figures, e.g. the rating of the user. Floats also have two distinct types which are “floats” and “doubles”. The only difference between floats and doubles is that a double can hold more decimal points than a float.

Declaring value types of numbers can hence be done thus:
                   

integer declaration
Integer Declaration  
float declaration
Float Declaration 
  


double declaration
Double Declaration

NOTE:

-     When working with numbers, you don’t need to surround them with quotation marks. Quotation marks are only necessary when working with strings and single characters.

-     You can also store negative numbers in these data types, like “-2”, etc.

-     20 and 20.0 are regarded as different data types in C++. 20 is a whole number while 20.0 is a floating point number.


c) Booleans: There’s also another type of data value in C++ and in other programming languages which is known as a “boolean”. A boolean is simply just a “true or false” data type. These are usually used when we want to make certain decisions only when a particular condition is either true or false.

To set a Boolean type for a variable, the “bool” keyword is used like this:

boolean declaration


The first condition asks if a user is a beginner in C++ and this condition is set to true, which means the user is a beginner. 

The second condition asks if a user is not advanced in C++ (which if set to true would mean that they’re beginners) and this is set to false, which means they have some knowledge about C++.

These true or false conditions can be helpful when writing loops in C++.

Note the way the variables are named. The first letter of the first word is in lowercase while the first letter of every other word is in uppercase. This is known as “camelCaseNaming”.

A boolean can only have one of two conditions, true or false.


d) Arrays: Arrays are a collection of multiple items, like names of students in a class, etc. Arrays can have multiple values (or elements) and these elements can be accessed using square brackets and typing in the index of the element we want like this: (cout << array[0] <<endl;).

Remember, you don’t always have to store information inside a variable if you’re not going to reuse it. 

For example instead of storing ‘A’ inside a variable and then printing it out, we can just print “A” out directly like so:

printing something on the screen

Don’t worry too much about how this line came about. How to write C++ codes would be discussed soon.

e) Void: In addition to the above-stated types, there’s another value type in C++ and this is the “void” value which essentially means nothing, or empty.


Download the full tutorial PDF here.

Comments

Sponsored Links

Popular posts from this blog

How To Make A Simple Calculator Using HTML, CSS and JavaScript

Coding vs. Programming - What Are The Differences?

Contact Us

Name

Email *

Message *