Introduction to c++

0
822
c ++
introduction to c++

C++ is a cross-plate form programming language which was developed by Bjarne Stroustrup, as an extension to C programming language. This language gives programmers a high level of control over system resources and memory. It was updated 3 major times in 2011, 2014, and 2017 to C++ 11, C++ 14, and C++ 17. It is one of the worlds most popular programming languages. It can be found in today’s operating systems, GUI(Graphical User Interface ), and embedded systems. It is an object oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development cost. It is portable and can be used to develop application that can be adopted to multiple platforms. It is easy to learn and it is close to C# and Java, it makes easy for programmers to switch to C++ or vice versa.

You need text editor and compiler like turbo C to translate C++ code into a language that the computer will understand. There are may text editors and compilers to choose from. Always remember that C++ file name ends with .cpp file format otherwise compiler do not understand and throws errors.

Example of C++ code:

#include <iostream>

Int main(){

cout << “Hello World”;

return 0;

}

Let’s elaborate the code:

-> #include <iostream> is a header file library that lets us work with input and output objects, such as “cout” below. Space between the lines will be ignored.

-> int main is called function which will always appear in C++ program. Any code inside the curly brackets will be executed.

-> cout pronounced as “see-out” is an object used together with insertion operator (<<) to output/print result. Same as printf on C programming language. In our example it will print “Hello World” text. Every C++ statement will end with “;” semicolon.

-> return 0 will end the function.

Note: Every function wrapped by curly brackets and every statement end with semicolon “;”.

New Lines:

You can use cout as many time as you want to  print lines in the progra. But it will print on the same line. To print every statement in another line you can use \n or endln in the end of statement.

For example:

cout << “Hello World ! \n”;

cout << “From Nepal”;

And for endl:

cout << “Hello world !” << endl;

Cout << “From Nepal”;

You can use \n multiple time as you need. Each \n will show one single line space. If you want two space between lines use \n\n and so on. \n is the preferred way to break the lines.

Comments:

To make code readable we can use comments. It will help programmers to identify what actually that line of code will do. Comments can be single-lined and multi-lined.

For example :

//This is an example of comments in C++ language

/* This is an example of multi-lined comments

 In C++ language */

In program these comments will be ignored by compiler and run program. It is up to you which you want to use.

Variables in C++

There are different types of variable in C++ language which is defined with different keywords.

For Example:

Int : stores integers ( whole numbers ), without decimals such as 432 and -644

Double: store floating point numbers with decimals such as 43.43 and -64.5

char: stores single character such as ‘q’,’r’. Char variables are surrounded by single quotes.

string:  stores text such as “Hello world”, String variables are surrounded by double quotes.

bool: stores values with two state: true or false.

Declaring Variables ( Creating Variables ):

To declare variable you must have to specify variable type and assign it to value.

Syntax to declare variable:

Variable_type variable_name = value;

Where variable type can be int, double, char or string. And variable name is the name of the value and the equal sign is used to assign value to the variable. Value always assigned left to right. For example:

// Creating variable type of int which store value 453:

int myVariable = 453;

cout << myVariable;

In this example int type of variable named myVariable which stores 453 integer value. And print value.

Other types of value declaration:

double myDouble = 54.65; // Floating point number

char myChar = ‘U’; // Character

string myString =”‘Hows u doing”; //String

bool myBool = true; //Boolean true or false

Display Variable:

cout object is used together with << operator to display variable.

The combine of both text and variable, separate them with the << operator.

int amount = 500;

cout << “This item costs ” << amount << “ Rs. each”;

Declare Multiple variable at once:

int a =4, b = 54, c = 22, d = 64;

cout << a+b+c+d;

Note: you can only declare same type of variable at once like previous example.

C++ identifiers:

Variables must be identified with unique name in C++. It can be short like a or b and can be more descriptive like username, userpassword etc. It is recommended to use descriptive names in order to create understandable and maintainable code.

//Good

string songName = “Don’t give up”;

//Ok but don’t have any meaning

string m = “Music is love”;

The general rules for constructing variable names for variable (Unique identifier) are :

  1. Names can contains letters, digits and underscore.
  2. Name must begin with a letter or underscore (_).
  3. Names are case sensitive ( myVar and myvar are two different variable )
  4. Names can not contain white spaces or special characters like !,#,%,& etc.
  5. Reserved words (like C++ keywords, such as int ) cannot be used as name.

Constants:

When you have do not wants others or yourself to override existing variable value, use the const keyword. This will declare the variable as “constant”, which means unchangeable and read-only.

For example:

const int myNum = 53; // myNum will always be 53

myNum = 32; //error: assignment of read-only variable ‘myName’

You should always declare the variable as constant when you have values that are unlikely to change.

For example:

const int secondsInMin = 60;

const float PI = 3.14;

C++ User Input:

We already discussed about cout this is used to print result and to get user input we will use cin object pronounced “see-in”. cin is predefined variable that reads data from the keyboard with the extraction operator (>>).

For example:

cout << “Enter value :\n”; // Print instruction

cin >> x; //get User input value

cout <<”Your number is: ”<<x; //Print value entered by user

In the given example first line be printed and user can enter value from keyboard then program will print text with user value.

Example:

Create simple calculator :

int x, y, sum;

cout << “Enter first number :\n”;

cin >> x;

cout << “Enter second number:\n”;

cin << y;

sum = x+y;

cout << “Sum of ”<<x<<” and ”<<y<<” is ”<<sum;   

These are the basic introduction to c++ programming language. Practice and continuous research will make you more understanding about the programming language.

LEAVE A REPLY

Please enter your comment!
Please enter your name here