C++: Explain Like I’m five – Part 1: Hello World! , Variables and Data types

C++: Explain Like I’m five – Part 1: Hello World! , Variables and Data types

C++ is a very popular and powerful middle-level programming language for performance-critical applications that rely on speed and efficient memory management developed by Bjarne Stroustrup starting in 1979 at Bell Labs. It is used in a wide range of industries such as software and game development, VR, robotics, and scientific computing. It runs on a variety of platforms like Windows, Mac OS, and the various versions of UNIX. Moreover, it supports different ways of programming such as procedural, object-oriented, functional, and so on. This makes it powerful as well as flexible.

Why learn C++?

• C++ is very close to hardware, so you get a chance to work at a low level which gives you a lot of control in terms of memory management, better performance, and finally a robust software development.

• Programming in C++ gives you a better understanding of Object-Oriented Programming (OOP). You can easily understand a low-level implementation of polymorphism when you will implement virtual tables, dynamic type identification or virtual table pointers.

• C++ is an ever-green programming language and is still loved by millions of developers. If you are good at C++, then you will never sit without work.

• It is the most widely used programming language in application and system programming thus enabling it to have a large programming community.

• Moreover, it is fast and flexible while having strong documentation.

Before dwelling deep into some complex concepts, let’s take a look at the main syntax of C++ by saying hello to the world ;)

Hello World in C++

Given below is how we get the output “Hello world!” in C++. We will divide this program in parts to take a closer look at why the components that are the base of the program important.

/*
A
Multiple
Line Comment
*/

#include <iostream>

//A single line comment
using namespace std;

//This is where the execution of the program begins
int main()
{
        cout<<"Hello World!";

        return 0;
}

Output:

Hello World!

Now, let’s discuss every part of the above program.

1. Comments

Two types of comments can be seen in the above program:


// A single line comment

/* 

A Multiple 

line comment

*/

As the name suggests, comments are just a text written by programmer during code development. It doesn’t affect your program logic in any way so you can write whatever you want in comments but it should be related to the code. You should write meaningful comments so that when someone look into your code, they should what you did in the code by just reading your comment. Comments improve the readability of your code. Helpful, aren’t they?

2. #include < iostream >

This statement indicates to the compiler to include the iostream file. This file has pre-defined input/output functions that can be used in our program.

3. using namespace std;

A namespace is basically a region where we have functions, variables, etc. and their space is limited to that particular region. Here std is a namespace name that tells the compiler to look into that particular region for all the variables, functions, etc. I will not discuss this in detail here as we will do it later. Right now, just follow the series in the sequence and you’ll be ready to code in C++.

4. int main()

This is the main function of our program. The execution of the program begins with this function. The “int” here is the return type that tells the compiler that this function will return an integer value. This is the reason why we have a return 0 statement at the end of main function.

5. cout << “Hello World!”;

The “cout” object belongs to the iostream file and the purpose of this object is to display the content between double-quotes. This object can also display the value of variables on-screen (don’t worry, we will discuss everything in detail later).

6. return 0;

This statement returns value 0 from the “main()” function which indicates that the execution of the main function is successful. The value “1” shows that the program has failed to execute.

Now you know the main syntax of a C++ program so I say that we take a look at some easy concepts like Variables and Data types.

Variables

A variable is a name which is associated with a value that can be changed. For example when I write int num=5; here variable name is num which is associated with the value 5 and int is a data type that represents that this variable can hold integer values.

Syntax of declaring a variable in C++

data_type variableA_name = valueA, variableB_name = valueB;

For example:

int numA = 5, numB = 10;

We can also write it like this:

int numA, numB;

numA = 5;

numB = 10;

Rules for naming Variables

There are some rules to follow while naming variables;

• Name your variables so that their name defines their purpose.

• There should be no spacing.

• Do not start your variable names with an underscore.

• Your variable names should not be single letters as it is much better to use single letters for just loop counters (We will discuss them in later parts of the series).

• Do not use C++ keywords or any type of reserved words or constants as your variable names.

Types of Variables

Variables can be categorized on the basis of their data types. For example, in the above example, we have only used integer-type variables. Following are the types of variables available in C++:

int: These type of variables hold integer values.

char: It holds character values like ‘a’, ‘S’, ‘B’, ‘p’, ‘t’ etc.

bool: It holds boolean values: true or false.

double: It holds double-precision floating point values.

float: It holds single-precision floating point value.

We will discuss the data types in detail below.

Types of variables based on their scope

Before getting into the details, let’s discuss what a “scope” is. While discussing the Hello World Program, we saw curly braces in the program like this:

int main()
{
          //Some code
}

Any variable that is declared inside these curly braces have scope limited only within these curly braces. So, if you declare a variable in the main() function and try to use that variable outside the main() function then you will get a compilation error.

Now that we know what a scope is, so let’s take a look on the types of variables based on scope.

1. Global Variable

A variable declared outside of any function (including the main() function as well) is called a global variable. Global variables have their scope throughout the program as they can be accessed anywhere in the program.

Example:

#include<iostream>
using namespace std;
//This is a global variable
char myVar = 'A';
int main()
{
          cout<<"Value of myVar: "<<myVar<<endl;
          myVar = 'Z';
          cout<<"Value of myVar: "<<myVar;
          return 0;

}

Here we have a global variable VarA, that is declared outside of the main() function. We have accessed the variable twice in the main() function without any issues.

Output:

Value of myVar: A

Value of myVar: Z

2. Local variable

Local variables are declared inside any user-defined function, main function, loops, or any control statements (if, if-else etc) and have their scope limited inside them.

Example:

#include<iostream>
using namespace std;

char myFunc() {
//This is a local variable
char myVar = 'A';
}
int main()
{
          cout<<"Value of myVar: "<<myVar<<endl;
          myVar = 'Z';
          cout<<"Value of myVar: "<<myVar;
          return 0;

}

Output:

Here, we will get compile-time error, because we are trying to access the variable myVar outside of its scope. The scope of myVar is limited to the body of function myFuncn().

Since you guys now understand the main syntax and variables, it’s time to take a closer look at Data types in C++.

Data types in C++

Data types define the type of data that any variable can hold. For example, an integer variable can hold integer data, a character type variable can hold character data, etc.

Data types in C++ can be categorized into three groups:

• Built-in

• User-defined

• Derived

Built in data types

• char:

It is used for characters. Size is 1 byte.

Example:

char ch = 'A';

• int:

It is used for integers. Its size is 2 bytes.

Example:

int num = 10;

• float:

It is used for single-precision floating-point. Size is 4 bytes.

Example:

float num = 157.7889;

• double:

It is used for double precision floating point. Size is 8 bytes.

Example:

double num = 10098.98899;

• bool:

It is used for Booleans; true or false.

Example:

bool a = true;

User-defined data types

There are 3 types of user-defined data types in C++:

  1. struct
  2. union
  3. enum

Derived data types

There are 3 types of derived-defined data types in C++:

  1. Array
  2. Function
  3. Pointer

Conclusion

I will cover user-defined data types and derived data types in other parts of the series, so don’t worry. For now, just remember under what category these data types fall into. I hope this tutorial helped you guys understand the basics of C++ programming. In the next part, we will cover operators and some parts of Flow Control (If/Else, switch case, loops, etc.).