You are on page 1of 8

RAGHAVENDRA TUTORIALS

C++ NOTES-1
_______________________________________________________________________________________________

What is C++?
 C++ is a cross-platform language that can be used to create high-
performance applications.
 C++ was developed by Bjarne Stroustrup in 1980, as an extension
to the C language.
 C++ gives programmers a high level of control over system
resources and memory.
 The language was updated 3 major times in 2011, 2014, and 2017
to C++11, C++14, and C++17.

Why Use C++?


 C++ is one of the world's most popular programming languages.
 C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.
 C++ is an object-oriented programming language which gives a
clear structure to programs and allows code to be reused, lowering
development costs.
 C++ is portable and can be used to develop applications that can be
adapted to multiple platforms.
 C++ is fun and easy to learn!
 As C++ is close to C# and Java, it makes it easy for programmers
to switch to C++ or vice versa

Get Started
This tutorial will teach you the basics of C++.

It is not necessary to have any prior programming experience.

C++ Get Started


To start using C++, you need two things:

 A text editor, like Notepad, to write C++ code

pg. 1
 A compiler, like GCC, to translate the C++ code into a language that
the computer will understand

There are many text editors and compilers to choose from. In this
tutorial, we will use an IDE (see below).

C++ Install IDE


An IDE (Integrated Development Environment) is used to edit AND
compile the code.

Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are
all free, and they can be used to both edit and debug C++ code.

Note: Web-based IDE's can work as well, but functionality is limited.

C++ Quickstart
 Let's create our first C++ file.
 Open your IDE and go to File > New > Empty File.
 Write the following C++ code and save the file
as myfirstprogram.cpp (File > Save File as):

myfirstprogram.cpp
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

Don't worry if you don't understand the code above - we will discuss it in
detail in later chapters. For now, focus on how to run the code.

Main Function

pg. 2
The main function is where the bulk of the code will go. For a program to run, it must
have a main function. Visual Studio starts a program with code that looks like this:

All the code that the main function runs goes between an opening curly bracket { and a
close ng curly bracket }

Building your solution tells the IDE to test for errors and compile the
code. Compiling converts your code from C++ to complex machine code that the
computer can run.

Variable names
Understanding variables and how they work is critical to C++. A variable is like a chest
or a container that can be filled with different values. The containers are names so they
can be found later.

When a type of variable is specified, like a string, doing so tells the program that this
chest can only be filled with words, phrases, and multiple characters. It won't be able to
hold numbers, true/false values, and so on. 

Variable types
In C++, there are different types of variables (defined with different
keywords), for example:

 int - stores integers (whole numbers), without decimals, such as


123 or -123

pg. 3
 double - stores floating point numbers, with decimals, such as 19.99
or -19.99
 char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
 string - stores text, such as "Hello World". String values are
surrounded by double quotes
 bool - stores values with two states: true or false

Declaring (Creating) Variables


To create a variable, specify the type and assign it a value:

Syntax
type variableName = value;

Where type is one of C++ types (such as int), and variableName is the


name of the variable (such as x or myName). The equal sign is used to
assign values to the variable.

To create a variable that should store a number, look at the following


example:

Example

Create a variable called myNum of type int and assign it the value 15:

int myNum = 15;
cout << myNum;

Types of Variables discussed in detail….


1)String
A string represents text; words and phrases. Said differently, it’s a sequence of
characters enclosed in double quotation marks:

string jimmysName = "Jimmy"; 


string twoWords = "Hi there!"; 
string nonsense = "sdfg sdf ksksjs"; 
pg. 4
In terms of assigning values to variables, a value is the information the variable holds.
So, with the sting, it’s a word. As mentioned above, assigning values to variables is like
filling a chest with something. When the variable myName is used , the computer will
return the value inside it.

To change a variable, simply write the variable name and a string value. Whatever value
was written before will now be overwritten by the new value. 

And when naming variables, it’s suggested to do so descriptively so coders can


remember what is placed in each. 

Other variable types include:  

2)Char
A char is a single letter or symbol you can type, enclosed in single quotation marks.

char a = 'a'; 
char exclamation = '!'; 

3)Int
Int is short for integer, which is a whole number.

int myAge = 94; 


int myCats = 300000000; 

4)Double
Doubles and floats are numbers with a decimal. A double is more precise than a float; it
can hold more numbers before and after the decimal.

double decimalNumber = 4.4567; 


double pi = 3.14159265; 

5)Bool
Bool is short for boolean. It represents a value that's true or false.

bool iAmAnAlien = true; 


bool aHungryAlien = false; 

pg. 5
Arithmetic operators
Arithmetic operators are crucial to games. For example, when players endure damage or
earn experience points, totals need to be calculated in some fashion. 

C++ arithmetic operators read from left to right and follow the normal mathematical
order of operations. Some basic operators and their order of precedence are listed in the
table below.

Arithmetic operators can be performed on both variables and literals (data typed in
directly).

Example Games
There are a wealth of options in terms of games that can be coded with C++. Here are
just a few examples to get coders thinking about what they might want to create!

RPG Fighting Tournament


This type of game includes creating, saving, and loading characters, tracking a
character's wins in their save file, and individual character stats. In these games, turn-
based combat is in a loop that determines the winner and moves the player through
various levels or objectives. If that sounds familiar, it should to most gamers—many big-
name video games follow this format!

Pong
Pong: a true gaming icon and one of the earliest arcade games. Using a couple UI
elements in SFML, a simple single-player Pong game can be created.

pg. 6
Tetris
Tetris is a classic puzzle game where you try to position falling blocks to create lines
without building over the top of the board. Tetris has some interesting programming
challenges involved in creating it.

Checkers
Creating a new game from scratch can seem like a daunting task. However, with good
planning, you can break a project into smaller pieces. (Stay tuned for a tutorial on how to
create your own checkers game with C++! 

C++ Comments

pg. 7
Comments can be used to explain C++ code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code. Comments can be singled-lined or multi-lined.

Single-line Comments
Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler
(will not be executed).

This example uses a single-line comment before a line of code:

Example
// This is a comment
cout << "Hello World!";

This example uses a single-line comment at the end of a line of code:

Example
cout << "Hello World!"; // This is a comment

C++ Multi-line Comments


Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by the compiler:

Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";

Single or multi-line comments?

It is up to you which you want to use. Normally, we use // for short


comments, and /* */ for longer.

pg. 8

You might also like