C++ Getting started

To explore the real crux of programming and diving into the world of compilers, memory, and pointers, a good choice would be to start with C++. So we wonder what C++ is? When was it developed? How does it work? And what is the language capable of?

To get started, C++ is a versatile programming language that was developed by Bjarne Stroustrup in 1998. It supports many different programming paradigms like procedural, functional, and object oriented.

The above lines give a brief introduction to C++, but what is the language capable of ? C++ offers limitless variety of development from small programs to full stack commercial applications.

Interesting C++ Applications

From games to databases to operating systems, C++ has really dominated the tech world with amazing applications like:

  • Google file system and Google Chromium browser
  • MySql server
  • Microsoft Windows OS
  • Mozilla firefox
  • GUI based adobe systems
  • Banking Applications like Infosys

Write Your First Program

The best way to learn a language is through an example. To discover a C++ program’s anatomy, the first thing you would need is a working compiler.

Get Your Compiler Started

The two things you will need are :

  • A text editor
  • A compiler like GCC. A compiler translates your high-level code to computer understandable form.

However, the easiest option would be to use an IDE(Integrated Development Environment). IDE combines all the tools necessary for a developer in a single GUI, i.e., we can find both the compiler and the text editor in one place.

Installing C++ IDE in Windows

An IDE can be installed in Windows. Some good IDEs to compile C++ programs could be code blocks, dev c++, and visual studio. I would share examples compiled in dev c++. To download dev c++ use the below link:

After the installation is complete, click ‘run’ to start the application. Then press CTRL + N to open a new window, and you are good to go!

To compile and run the program just press F11 key and a console window will pop up.

Installing C++ IDE in Linux

To run a C++ program in Linux, the first and the most important thing is installing a g++ or GCC compiler. The following command is shown below that installs all the necessary C++ libraries on Ubuntu.

sudo apt-get install g++

The Linux terminal is used and a file can be opened as shown in the snippet.

Gedit firstprogram.cpp

You can paste any c++ code like the one shown below and close the file.

// A simple C++ program
 #include <iostream> // preprocessor directive
 using namespace std;

 int main()
 {
 cout << "Programming is fun";
 return 0;
 }

Then to check the output of the program first run the command shown below to compile the program.

sudo g++ -o firstprogram firstprogram.cpp

And then type the following command in the terminal.

./firstprogram

The output will be displayed on the terminal.

A Deeper Dive into C++

Now the real action begins. Let’s dive deeper and look into the individual portions of a C++ program.

First, the line:

#include<iostream>

This is a preprocessor directive and is always included on top of a program. The #include part includes the iostream header file that is necessary to print the output to the terminal and to take user input from the keyboard. Without this header file, a C++ program will not work.

Next is the namespace. It is a block or a portion that contains variables, functions, and classes.

So a question comes to mind as to why this namespace is so important? Considering how vast a programming language is, and many libraries are used in our programs, we likely create functions or variables with the same names as present in the libraries; thus, we define a namespace to avoid such conflicts. To use the iostream header file entities, the program must have access to the std namespace.

After this is a function that is the lifeline of the whole C++ program. The main function:

Int main() 
{
}

This function returns an integer value to the operating system after it completes execution. A C++ program can consist of many functions; however, every C++ program must have the main function because it marks the starting point of a program and other functions are likely to be called in the main function.

Then comes the real deal, the syntax , the programming style and the actual code:

cout << "Programming is fun"; 

This line outputs the following text on the terminal. The above-explained components are important for your program structure, but only this line results in a screen output. Each line in a C++ program ends with a semicolon. Like a full stop marks the end of a sentence, the semicolon marks the end of a statement in C++.

The last line:

Return 0; 

This line returns the value zero after the complete execution of the program. The value 0 indicates that the program has been successfully executed.

Last but not least, any line starting with // are comments. The meaning of comments is that the sentence holds no meaning for the compiler. You can write anything you want, and the compiler will give no error. However, the true purpose of comments is to make the code easily understandable and provide assistance to developers to remember what they have done when they re-visit their code.

IMPORTANT NOTE: C++ is a case-sensitive language, so INT MAIN() and int main() hold many differences. Only int main () written in the small case will work.

Concluding Note

We peeked into the world of C++. The C++ programming language offers a wide range of programming knowledge including manual memory management, dealing with pointers, metaprogramming, the difference between compile-time and run-time, and much more. Even though the learning curve is steep, but the road is worth it. Take it!

Similar Posts

One Comment

  1. Very well written, as a university student starting c++ this semester, this article is a good read to get acquainted with the language.

Leave a Reply to Hatif Cancel reply

Your email address will not be published. Required fields are marked *