Getting started with C++
As a newbie, it can be very confusing finding your place and rhythm in the world of programming. In this article, we will learn how to start with programming and with C++ specifically. This article could not have been possible without Shambhavi Shandilya, who was the co-organizer for the C++ hIQ, huge shoutout to her!
For becoming a programmer, one must know how to talk to computers. For talking to anybody in general we need to have a common language or translator who can translate between two persons who understand different languages. After we have a communication set up, we can start solving each other’s problems. Similarly, we will learn how to talk to computers using C++ as our language. After we know how to talk to computers, we can solve some problems.
Before we jump into programming and write actual codes, let’s first learn what a program is. A program is a set of instructions that we give to our computer. Computers only understand binary(1s and 0s). We write code in high-level languages like C++, Python, JAVA, etc. There is something called a compiler in our computers that converts these instructions into something that our computers can understand(low-level language). You can think of a compiler as a translator for now.
You can try writing your code here :
The above link takes you to an ‘IDE’ or an ‘integrated development environment’. You can use the IDE to write, compile and execute your code.
Another option would be to use your own IDE on your local system and compile the programs using a compiler like g++(https://www.geeksforgeeks.org/compiling-with-g-plus-plus/).
For simplicity, you can use the Coding Blocks IDE.
Simple output programs to start with :
While learning a new programming language we usually start with the HELLO WORLD program. In this, we ask the computer to print “Hello World!” on the screen. In C++ we can write it like this:
#include<iostream>using namespace std;int main(){cout<<”Hello World”;return 0;}
For now, let’s focus on the ‘cout<<’ instruction alone. Anything that we write after this instruction in quotes will be printed on the screen. Try this:-
#include<iostream>using namespace std;int main(){cout<<”My name is xyz”;cout<<”My age is 21";}
How many lines of output did you get? We gave two different instructions but we got one line of output. To add a new line we use endl or \n. Try this:-
#include<iostream>using namespace std;int main(){cout<<”My name is xyz”<<endl;cout<<”My age is 21 \n”;cout<<”I am learning c++”;}
\n(newline — an escape sequence) and endl(endline) add a new line to the output.
You can learn more about these escape sequences here:
https://www.geeksforgeeks.org/escape-sequences-c/
Now let’s break the code and try to understand what the other lines in our program mean.
#include<iostream> is a preprocessor.
using namespace std; imports the standard files to run a program. You can learn more about pre-processors and different types of preprocessors here:
https://www.geeksforgeeks.org/cc-preprocessors/
But for now, just think of them as essential components that must be present at the start of your code if we want to get input or give an output.
main() is a function. Every C++ program starts with the execution of the main function i.e. whenever a C++ program is executed, the instructions inside the main function are carried out first. The brackets () indicate that main is a function. We will learn about functions later in the article.
Did you notice the ‘;’ at the end of every instruction? Try giving instructions without the semicolon(;). The compiler throws an error. So we must use semicolons in the right places.
What are those right places?
- Semicolons terminate statements, declarations. (Like a fullstop in the English language)
- It is used at the end of class and structure definitions.
- To separate the initializer, test, and increment portions of a for loop.
Till now we have printed some constant lines.
Let’s learn about storing data. To store data we use variables.
Variables:
Think of variables as containers in which we can store some data.
Different types of variables:-
Think of a scenario when you are in the kitchen and want to make some tea/coffee. First, you will take out all the utensils and tell your mother that you’re going to make chai(tea). Then you fill the chai/coffee in the appropriate utensil, say teacup or coffee mug.
Similarly in C++, we first declare some variables(taking out the utensils) and tell the compiler that we are going to use these variables(telling your mother that I am going to make chai).
Now, you can’t keep chai in a rice bag, right? We need a cup for that. Similarly different types of data need different types of variables to store the data. Let’s say you have an integer value, we use data type ‘int’, for a character we have ‘char’ and many more.
You can learn about different data types in C/C++ here:
https://www.geeksforgeeks.org/c-data-types/
For declaring a variable in C++ we can have two different approaches. First, we may take out all the utensils. Then make chai and pour it into the teacup. Or we can directly take the cup and pour the chai.
In C++ we can declare the variable by telling it’s data-type and then giving a name to the variable. Let’s say we want a variable that stores an integer value and we want to name it ‘number’.
int number;
Simple as that. Now let’s pour some chai or in other words give the variable some value.
number = 12;
Now the number 12 is stored inside the variable ‘number’.
For the second approach we can combine the two statements like this
int number = 12;
Let’s apply this in a program.
#include<iostream>using namespace std;int main(){int age;string name;age = 18;name = “xyz”;cout<< “My name is “ << name << “ and age is ” << age;return 0;}
We store the values 18 and xyz in variables named ‘age’ and ‘name’ respectively. Then we access the values using the names of the variables.
Now that we know how to print constant data and variable data, let’s perform some actions using operators.
Operators are used to perform operations on variables and values. An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
You can learn about the different types of operators here:
https://www.geeksforgeeks.org/operators-c-c/
https://www.w3schools.com/cpp/cpp_operators.asp
Taking input:
Till now we were dependent on hardcoded data. Now we will learn about taking inputs from the users. For taking inputs we need to have a place to store the data taken as input. So we declare a variable and store the input in the variable. To take inputs we use ‘cin>>’ which is kind of opposite to ‘cout<<’. Try the following program
#include<iostream>using namespace std;int main(){int age;cout<<”what is your age?”<<endl;cin>>age;cout<<”Your age is”<< age;return 0;}
In this program, we declared a variable named age and took input from the user using cin and stored the value in the variable age. Then we used cout to print the value of the variable age.
Let’s try another program
#include<iostream>using namespace std;int main(){int a;int b;int c;cout<<”What is your first number?/n”;cin>>a;cout<<”What is second number?/n”;cin>>b;c= a+b;cout<< “The sum is “ << c;return 0;}
In this program, we take two inputs of two numbers a and b. Then we add the values of variables a and b using + operator and store the sum in a different variable c. Then we print the value of the c.
Till now we have learned how to use basic output and input methods in C++ or in other words we know how to greet our computer using C++ language. To have communication with our computer we need to learn a lot more about the languages. You have become a programmer now. Next, you need to enhance your knowledge and skills to actually solve real-life problems or to develop something on your own.
The following resources will help you learn and explore the world of C++. Once you have learned these you can start solving actual problems.
if-else statements:
https://www.geeksforgeeks.org/decision-making-c-c-else-nested-else/
switch:
https://www.geeksforgeeks.org/switch-statement-cc/
Increment Decrement operators:
Loops:
https://www.geeksforgeeks.org/loops-in-c-and-cpp/
https://www.w3schools.com/cpp/cpp_for_loop.asp
https://www.w3schools.com/cpp/cpp_while_loop.asp
https://www.w3schools.com/cpp/cpp_do_while_loop.asp
Functions:
https://www.w3schools.com/cpp/cpp_functions.asp
https://www.geeksforgeeks.org/functions-in-c/
Arrays:
https://www.w3schools.com/cpp/cpp_arrays.asp
https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
https://www.geeksforgeeks.org/arrays-in-c-cpp/
Multi-Dimensional Arrays:
https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/
https://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm
Pointers:
https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm
https://www.w3schools.com/cpp/cpp_pointers.asp
https://www.geeksforgeeks.org/pointers-c-examples/
Call by value vs call by reference
https://www.geeksforgeeks.org/difference-between-call-by-value-and-call-by-reference/
STL:
https://www.geeksforgeeks.org/the-c-standard-template-library-stl/
Structures: