declaration of variable in c++

Declaration of Variables in c++

we know that in c all variables must be declare before they are used in executable statements. this is true with c++ as well , so in c++ also we have to declare variable before use these in c++. there is a significant difference between c and c++ programming languages with regard to the place of their declaration in the program. and c language requires all the variables to be defined at the beginning of a scope. when we read a c program, we usually come across a group of variables declarations at the beginning of each scope level. their actual use appears else where in the scope, sometimes far away from the place of declaration. before the using a variable, we should go back to the beginning of the program to see whether it has been declared , if so , of what type

in c++ language allows the declaration of a variable anywhere in the scope. it means that a variable can be declared right at the place of its first use. This makes the program much easier to write and reduce the errors that may be caused by having to scan back and forth. it also makes the program easier to understand because the variable are declared in the context of their use.the example below

#include <iostream>
int main()
{
float a; // declaration float type
int i; float sum=0;
for(i=1;i<7;i++)
{
std::cin>>a;
sum=sum+a;
}
float average; // declare average type float
average=sum/i;
std::cout << average;
return 0;
}

Dynamic Initialization of variables

in c language, a variable must be initialized using a constant expression, and the c compiler would fix the initialization code at the time of compilation(static binding). c++ however permits or(facility) initialization of the variables at run time. This is referred to as dynamic initialization. in c++ language a variable can be initialized at run time using the expressions at the place of declaration , for example in the below following are valid initialization statements for example:

………………
………………
int n=strlen(string);
……………….
……………….
floaty area=3.1415 * rad * rad;

thus both the declaration and the initialization of the variable can be done simultaneously at the place where the variable is used for the first time. the following two statements in the example of the previous section

float average; // declare where it is need
average=sum/i;

can be combined into a single statement

float average=sum/i; //initialize dynamic at the runtime

dynamic initialization is extensively used in object- oriented programming , we can create exactly the type of object needed using information that is known only at the run time.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise