logo codesdope

Introduction


C++ is one of the most popular programming languages. It is based on C but is an object-oriented programming language. It is widely used in graphics libraries, games, network devices, etc.

Why should we learn C++ ?


Features of C++

  • C++ is fast
  • Easy to learn
  • A powerful programming language
  • Has many built-in functions and operators which can make complex programs easy
  • Can be compiled on a number of computer platforms
  • C++ is an object-oriented programming language.
  • Use of pointer makes it a much more powerful language.
  • Also largely used in many large-scale projects

How to start ?

In this whole C++ tutorial, you will be learning about writing C++ programs. But what after that?

After writing any program, we need to compile and run it to see the output of the program.

So, what is meant by compiling ?

When we write our program in C++ language, it needs to be converted to machine language (which is binary language consisting of 0s and 1s) so that computer can understand and execute it. This conversion is known as compiling a program. We compile a code with the help of a compiler.

Integrated Development Environment (IDE)

To write and compile our C++ program, we have been provided with many IDEs.

An IDE consists of a Text Editor as well as a Compiler. We type our program in the text editor which is then compiled by the compiler.

Text Editor


We write our program in a text editor. The file containing our program is called source file and is saved with a .cpp extension.

C++ Compiler


After saving our program in a file with the .cpp extension, we need to compile it to convert it into machine language that computer can understand.

Now let's see how to compile and execute a C++ program on different operating systems.

Writing and compiling C++ program on Windows


Though there are many IDEs available for editing and compiling C++ programs for Windows, here we are talking about editing and compiling program using DevC++ which allows us to edit, compile and run our program. We can write and save our program using DevC++ itself and then compile it using the same.

Before learning to write a program in DevC++, first, make sure it is installed in your computer. If not, then download and install it.

To write and compile in DevC++, follow the steps given below.

1. On opening DevC++, you will get a window. Click on File->New->Source File option.

opening file in devc++

2. Write your C++ program as shown below and save it ( ctrl+s ).

#include <iostream>
int main()
{
   std::cout << "Hello World" <<std::endl;
   return 0;
}
writing on file in devc++
saving file in devc++

3. Once you have written the program, click on compile and run.

compiling C++ file in devc++

4. An output window will appear showing the result that is, Hello World printed.

Run C++ file in devc++

Now, you are ready to go for the next chapter. We will explain this code in the next chapter.

Writing and compiling C++ program on Linux


For Linux, you can write your C++ program in various text editors like vim, gedit or Emacs. The following steps show how to edit and compile your C++ program, assuming that the text editor is gedit.

1. Open Terminal ( ctrl+alt+T ).

2. Open a new file with .cpp extension ( in our case name of the file is hello.cpp ) in your favourite editor (in our case gedit). The command is :

gedit hello.cpp

open file with .cpp in gedit

3. The text editor window will be opened. Type the C++ program given below ( we will explain this code in the next chapter ) and save it.

#include <iostream>
int main()
{
   std::cout << "Hello World" <<std::endl;
   return 0;
}

4. To compile the program, type g++ hello.cpp -o hello ( g++ filename.cpp -o hello ) and press Enter. This will create an executable file with name 'hello' ( you can give any other name also but you will have to execute the file with that name only ).

compile c++ file in terminal

5. To run your program, type ./hello ( 'hello' is the name of the executable file we created in step 4 ) and press Enter. This will show the output of your program.

run c++ file in terminal

You can see Hello World printed on your screen. Now, you know how to write and compile C++ codes. So, you can go to the next chapter to understand this code.

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.