Introduction to C++

Learning a new programming language like C++ is an adventure in itself. It is similar to learning an instrument like piano, violin, guitar, etc. or gaining an athletic skill like playing baseball, volleyball, football, running, etc. or doing research in academics. Likewise, learning a new programming language like C++ requires time and practice. It takes more amount of effort in gaining expertise in the same.

Keys to learning programming

The key to learn a programming language is by solving the challenging problems. There appears to be no real shortcut. But, people are in a big rush to learn programming. It provides them with the basic knowledge of the subject.

But advancement takes time. If you learn some of the basics and apply this little chunk of knowledge on a big project, there is a high probability that the program may not run (good for you) or can cause some serious trouble. There are many books available which can teach programming in 24 hours (possible if you are GOD).

In 24 hours you might be able to learn some keywords, some syntax, might write some good programs, but you won’t be able to get the deep understanding of the subject. So, instead you should take time and enjoy the journey of learning which will take more than 24 hours.

Learning C++

Learning C++ is a long, exciting, a bit dramatic and slow process. It includes many programming principles, including procedural programming, generic programming and object-oriented programming. Each of these principles will be explained later.

If you have learned C, learning C++ can become a bit easier. But there is a huge difference in both the programming languages. They differ in their syntax. Many new concepts have been added to C++, which make it more robust. Many features have been added to C++ over C, including:

  • Classes
  • Polymorphism
  • Virtual Function
  • Function Overloading
  • Inheritance
  • Generic Programming
  • Namespaces
  • Exception Handling Mechanism

All of these will be explained in detail in further articles.

Coding our first problem in C++

Let’s code our first C++ program. All you need is a textpad and a compiler. It is sometimes tedious to set up compiler into your computer. There are many IDEs that you can use to compile and run a C++ program.

An IDE is an integrated development environment that provides facilities for software development. Many IDEs like, Code::Blocks, NetBeans, CodeLite, Geany, Dev C++, Eclipse, Microsoft Visual Studio, etc. help in developing programs faster. But for now we will use a textpad(notepad for instance) and a compiler(see this article). Open a textpad and write the following:

#include<iostream>
using namespace std;
int main(){
    cout<<"Hello World."<<endl;
    return 0;
}
//The same is shown in the figure below
helloWorld.cpp program written in textpad. introduction to c++
helloWorld.cpp program written in textpad

Save the file as helloWorld.cpp in the respective directory. Now we need to compile the program and get the executable file. For my program, I am using Linux environment with g++ compiler to compile my c++ program. Following command is run in the terminal-

g++ helloWorld.cpp -o helloWorld

Make sure you are in the same directory as the file helloWorld.cpp. When you run this command you get an executable file named helloWorld. In the same terminal type

./helloWorld

If everything is right, you will receive the text in the terminal as

Hello World.
helloWorld.cpp program output in the terminal. introduction to c++
helloWorld.cpp program output in the terminal

You can change the program, compile and run the program any number of times. Try copying the fourth line and writing it on the next. It prints the text (Hello World.) twice. You can change the text to your name, your favorite color, author etc. You always have the option of modifying the text file for changing the output to your requirement.

Let me describe in brief to you about the command that we ran in the terminal –

g++ helloWorld.cpp -o helloWorld
  • g++ command is GNU c++ compiler invocation command. It uses source code to generate the executable file.
  • helloWorld.cpp is the name of the text file, that we need to compile.
  • -o is used as an option to name the output file.
  • .helloWorld is the name of the executable output file.

You can also run the command as

g++ helloWorld.cpp

What happens now, is that it creates a file named a.out and not helloWorld. It is not a good practise to not name an executable file. Because every time a new file is compiled in the same directory, it replaces the old executable file a.out with a new executable file a.out and your old executable file is lost. So you should always use  -o filename while executing a command.

Examples

Now, let’s make another program.

#include<iostream>
using namespace std;
int main(){
    cout <<"Learn C++"<<endl;
    cout<<"You wont regret it."<<endl;
}

Save the above text file as prgm1.cpp. Now run the commands in the terminal:

c++ program output. introduction to c++
program for prgm1.cpp
g++ prgm1.cpp -o prgm1
./prgm1

It displays the output as 

Learn C++
You won't regret it.
Output shown for the program prgm1.cpp. introduction to c++
Output shown for the program prgm1.cpp

As described earlier, you can add as many lines as possible and the program will display it, provided it has correct syntax. Let’s learn a bit about our helloWorld.cpp program.

It has the following elements

  • a preprocessor #include directive
  • a using namespace directive
  • function header : main()
  • function body:
    cout<<"Hello World."<<endl;
    return 0;

Whenever you write a C++ program, it will contain all of the above specified elements. We will look at all of these elements in a great detail in other articles.

Before you start using C++, you should know the advantages of the same. Please click the link Advantages of C++, to know about the areas which are beneficial with the usage of C++.

Else you can hop on to the programming C++ starting from here(Your first C++ program).

To sum up, this article described the adventure of programming language comparing with the other genres. With many topics to master in C++, we now know that it is going to be an interesting journey ahead. We now know various IDEs you can use to make a C++ program and run the same. You learned a g++ command for compiling the C++ program and making the executable file.

We wrote two C++ programs and now you can write the programs that can display some value or string. Before you start programming, you should know the advantages of the same. I would recommend you to go through this article.

Happy Coding. 

Leave a Reply