Wednesday, February 7, 2018

C++ Variables

C++ Variables


  1. A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
  2. It is a way to represent memory location through symbol so that it can be easily identified.
  3. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory;
  4. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive
Variable Syntax:
datatype varname;
ex: int x;
ex: int x, y;
ex: in x=5; y=10; etc...
  • In C++, there are three ways to initialize variables.
  • The first one, known as c-like initialization (because it is inherited from the C language), consists of appending an equal sign followed by the value to which the variable is initialized:
datatype varname= initial_value;
ex: int x=10;
  • A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses ():
datatype varname (initial_value);
ex: int x(10);
  • Finally, a third method, known as uniform initialization, similar to the above, but using curly braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):
datatype varname {initial_value};
ex: int x{10};

Types of variables:

There are mainly two types of variables those are:
  • Local Variables
  • Global Variables

Local Variables:

  • Variables defined within a function or block are said to be local to those functions.
  • Anything between ‘{‘ and ‘}’ is said to inside a block.
  • Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.

Global Variables:

  • As the name suggests, Global Variables can be accessed from any part of the program.
  • They are available through out the life time of a program.
  • They are declared at the top of the program outside all of the functions or blocks.

Example1:
#include<iostream>
using namespace std;
int y=15; // global variable (created outside the block)

int main(){
    int x=10; // local variable (created inside the block)
    cout << x;
    cout<< y;
    return 0;
}

Example2:
#include<iostream>
using namespace std;
int y(15); // global variable (created outside the block initialized with constructor)

int main(){
    int x{10}; // local variable (created inside the block initialized with uniform)
    cout << x;
    cout<< y;
    return 0;
}

Example3:
#include<iostream>
using namespace std;
int x=15; // global variable (created outside the block)

int main(){
    int x=10; // local variable (created inside the block)
    cout << x;  
    return 0;
}

// here both local and global variable names are same at this time by default the output will be the local variable value because it is local to this function.

Example4:
#include<iostream>
using namespace std;
int x=15; // global variable (created outside the block)

int main(){
    int x=10; // local variable (created inside the block)
    cout << ::x;  
    return 0;
}
// here both local and global variable names are same at this time if you want to print global variable value use scope resolution operator (::) before the variable name.

You can also use extern keyword to access a global variable from one program to another program.
You cannot apply extern keyword to local variables.
ex: extern int x=15; // must and should it is a global variable which means it is declared outside the block only

C++ Basic Syntax

C++ Basic Syntax


  1. includes the standard input output library functions. It provides cin and cout methods for reading from input and writing to output respectively.
  2. The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++.
  3. The line int main() is the main function where program execution begins.
  4. The next line cout << "Welcome to Programming Tutorials by Abhishek"; causes the message "Welcome to Programming Tutorials by Abhishek" to be displayed on the screen.
  5. Each statement must be end with semi colon (;).
  6. The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process.

#include <iostream>
using namespace std;
int main() {
   cout << "Welcome to Programming Tutorials by Abhishek";
   return 0;
} 

  • To execute the above code you have to install GNU C/C++ Compiler. Otherwise you can use IDE like Codeblocks.
  • Simply copy the above code and paste it in codeblocks and click on build and run then it will display the output in the command prompt.

Standard end line (endl)

  1. The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the stream.
  2. By default the output will be displayed side by side only which means in a single line.
  3. But if you want to display output from a new line you have to use endl .

C++ Introduction

C++ Introduction


  1. C++ is an object-oriented programming language. It is an extension to C programming.
  2. C++ is a general purpose, case-sensitive, free-form programming language
  3. C++ supports the object-oriented programming, the four major pillar of object oriented programming used in C++ are:
      • Inheritance
      • Polymorphism
      • Encapsulation
      • Abstraction
  4. By the help of C++ programming language, we can develop different types of secured and robust applications:
      • Window application
      • Client-Server application
      • Device drivers
      • Embedded firmware etc