C++ Basic Introduction
C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc.
C++ is a general-purpose object-oriented programming (OOP) language, developed by Bjarne Stroustrup, and is an extension of the C language. It is, therefore, possible to code C++ in a “C style” or “object-oriented style.” In certain scenarios, it can be coded in either way and is thus an effective example of a hybrid language.
C++ is considered to be an intermediate-level language, as it encapsulates both high- and low-level language features. Initially, the language was called “C with classes” as it had all the properties of the C language with an additional concept of “classes.” However, it was renamed C++ in 1983.
It is pronounced “see-plus-plus.”
C++ is one of the most popular languages primarily utilized with system/application software, drivers, client-server applications and embedded firmware.
The main highlight of C++ is a collection of predefined classes, which are data types that can be instantiated multiple times. The language also facilitates the declaration of user-defined classes. Classes can further accommodate member functions to implement specific functionality.
Multiple objects of a particular class can be defined to implement the functions within the class. Objects can be defined as instances created at run time. These classes can also be inherited by other new classes which take in the public and protected functionalities by default.
C++ includes several operators such as comparison, arithmetic, bit manipulation, and logical operators. One of the most attractive features of C++ is that it enables the overloading of certain operators such as addition.
A few of the essential concepts within the C++ programming language include polymorphism, virtual and friend functions, templates, namespaces, and pointers.
C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.
C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully. If you do not want to set up a local environment you can also use online IDEs for compiling your program.
Using the online IDE:
IDE stands for the integrated development environment. IDE is a software application that provides facilities to a computer programmer for developing software. There are many online IDEs available which you can use to compile and run your programs easily without setting up a local development environment.
You can click on the Run on IDE button to run the program.
Setting up local environment
For setting up your own personal development environment on your local machine you need to install two important software:
Text Editors are a type of programs used to edit or write texts. We will use text-editors to type our C++ programs. The normal extension of a text file is (.txt) but a text file containing C++ program should be saved with ‘.CPP’ or ‘.C’ extension. Files ending with the extension ‘.CPP’ and ‘.C’ are called source code files and they are supposed to contain source code written in C++ programming language. These extension helps the compiler to identify that the file contains a C++ program.
Before beginning programming with C++, one must have a text-editor installed to write programs.
Once you have installed text-editor and typed and save your program in a file with ‘.CPP’ extension, you will need a C++ compiler to compile this file. A compiler is a computer program which converts high-level language into machine-understandable low-level language. In other words, we can say that it converts the source code written in a programming language into another computer language which the computer understands. For compiling a C++ program we will need a C++ compiler which will convert the source code written in C++ into machine codes. Below are the details about setting up compiler on different platforms.
We will install the GNU GCC compiler on Linux. To install and work with the GCC compiler on your Linux machine, proceed according to below steps:
- You have to first run the below two commands from your Linux terminal window:
sudo apt-get update sudo apt-get install GCC
This command will install the GCC compiler on your system. You may also run the below command:
sudo apt-get install build-essential
This command will install all the libraries which are required to compile and run a C++ program.
- You have to first run the below two commands from your Linux terminal window:
- After completing the above step, you should check whether the GCC compiler is installed in your system correctly or not.
- To do this you have to run the below-given command from Linux terminal:
- If you have completed the above two steps without any errors, then your Linux environment is set up and ready to be used to compile C++ programs. In further steps, we will learn how to compile and run a C++ program on Linux using GCC compiler.
- Write your program in a text file and save it with any file name and.CPP extension. We have written a program to display “Hello World” and saved it in a file with the filename “helloworld.cpp” on desktop.
- Now you have to open the Linux terminal and move to the directory where you have saved your file. Then you have to run the below command to compile your file:
g++ filename.cpp -o any-name
filename.cpp is the name of your source code file. In our case, the name is “helloworld.cpp” and any-name can be any name of your choice. This name will be assigned to the executable file which is created by the compiler after compilation. In our case, we choose any-name to be “hello”.
We will run the above command as:
g++ helloworld.cpp -o hello
- After executing the above command, you will see a new file is created automatically in the same directory where you have saved the source file and the name of this file is the name you chose as any-name.
Now to run your program you have to run the below command:
This command will run your program in the terminal window.
There are lots of IDE available for windows operating system which you can use to work easily with the C++ programming language. One of the popular IDE is Code:: Blocks. Once you have downloaded the setup file of Code:: Blocks from the given link open it and follow the instruction to install.
- After successfully installing Code:: Blocks, go to File menu -> Select New and create an Empty file.
- Now write your C++ program in this empty file and save the file with a ‘.cpp’ extension.
- After saving the file with ‘.cpp’ extension, go to Build menu and choose the Build and Run option.
Mac OS X Installation:
If you are a Mac user, you have to download Xcode. To download Xcode you have to visit the apple website or you can search it on the apple app store. You will find all the necessary installation instructions there.
- After successfully installing Xcode, open the Xcode application.
- To create a new project. Go to File menu -> select New -> select Project. This will create a new project for you.
- Now in the next window, you have to choose a template for your project. To choose a C++ template to choose Application option which is under the OS X section on the left sidebar. Now choose command-line tools from available options and hit the Next button.
- On the next window provide all the necessary details like ‘name of organization’, ‘Product Name’ etc. But make sure to choose the language as C++. After filling the details hit the next button to proceed to further steps.
- Choose the location where you want to save your project. After this choose the main.cpp file from the directory list on the left side-bar.
- Now after opening the main.cpp file, you will see a pre-written c++ program or template is provided. You may change this program as per your requirement. To run your C++ program you have to go to the Product menu and choose the Run option from the dropdown.