c++

Getting Started

Introduction to Computer Programming

We all know that computer is an electronic device that takes data as input, processes it and gives meaningful result as output. A computer processes data on the basis of instructions. These instructions direct computer hardware (i.e. CPU) to perform various operations, like arithmetic operations, logical operations, memory allocation etc.

The set of instruction given to a computer is called a program or computer program. These computer programs are written in programming languages which are also known as High Level Languages (HLL). High Level Languages are near to human language and can be converted into languages understandable by computer hardware ( machine code and binary numbers). Thus, a programming language acts as a interpreter between humans and computers.

We have different programming languages to communicate with computers like, C, C++, java, python etc.

In this course, we are going to learn C++.


About C++

C++ is one of the most popular and powerful general purpose language came into existence in 1979, and was developed by Bjarne Stroustrup at AT&T bell labs. Although, its first edition was launched in 1985 . It was initially developed as an extension C language, but later became much popular.

C++ can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.


Getting Started with C++ IDE and Compiler

Basically, for programming a programmer needs two things a text editor to write programs and an interface to convert them in machine code. This interface is called compiler. A compiler itself is a program that converts high level language into machine understandable language.

An Integrated Development Environment (IDE) is a software that comes with both, the text editor and compiler and also with other necessary tools for programming, software development and testing.

There various IDE based on different compilers. But in this course we will use Code::Blocks IDE based on GNU Compiler Collection (GCC).


How to download

To download Code::Block go to the following link,

http://www.codeblocks.org/downloads/26

it is available for linux and windows

Mac OS users have to download Xcode IDE based on Clang compiler

To download you may go to Apple website or Apple Appstore .


Setting up the IDE

For windows users
  • 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.
For Mac OS users
  • 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 choose Application option which is under the OS X section on the left side bar. Now choose command-line tools from available options and hit Next button.
  • On the next window provide all the necessary details like ‘name of organisation’, ‘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 Product menu and choose the Run option from the dropdown.

‘Hello world!’ in C++

‘Hello world!’ is the first program one writes while learning a programming language. We will do the same in C++ language.

//my first program
#include<iostream>
using namespace std;
int main(){
    cout<<"Hello world !";
    return 0;
}

Output:

Hello world!

Let’s see how this works.

  1. //my first program
    in C++, a line starting with // is called comment. Comments are intended for a person who is reading the code, for better understanding. It is completely ignored by compiler.

  2. #include
    The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file.

  3. int main(){…}
    A C++ program must have main() function followed by curly braces {} in which program is written. The execution of program starts from main() and it is called by the operating system.

  4. cout<<”Hello world !”;
    Cout followed by << prints the line inside the quotations on the console. ; is used to indicate end of a statement.

  5. return 0;
    It is the exit statement from a function or program.