Chapter: Object Oriented Programming and Data Structure : Data Abstraction & Overloading

Variables Initialization

A variable can be considered as a box that can hold a single value. However, initially the content of a variable (or a box) is empty.

Variables Initialization

 

A variable can be considered as a box that can hold a single value. However, initially the content of a variable (or a box) is empty. Therefore, before one can use a variable, it must receive a value. Do not assume the compiler or computer will put some value, say 0, into a variable.

 

There are at least three ways to put a value into a variable: · initializing it when the program is run

· using an assignment statement

· reading a value from keyboard or other device with a READ statement.

 

The way of initializing a variable is very similar to the use of PARAMETER attribute. More precisely, do the following to initial a variable with the value of an expression:

 

· add an equal sign (=) to the right of a variable name

 

· to the right of the equal sign, write an expression. It is important to note that all names in the expression must constants or names of constants.

 

Initializing a variable is only done exactly once when the computer loads your program into memory for execution. That is, all initializations are done before the program starts its execution.

 

The use of un-initialized variables may cause unexpected result. Examples:

 

The following example initializes variables Offset to 0.1, Length to 10.0, and tolerance to 1.E-7.

 

· REAL :: Offset = 0.1, Length = 10.0, tolerance = 1.E-7

 

The following example initializes variables State1 to "MI", State2 to "MN", and State3 to "MD".

 

· CHARACTER(LEN=2) :: State1 = "MI", State2 = "MN", State3 = "MD" The following example first defines three named integer constants

 

with PARAMETER and uses these values to initialize two integer variables. Thus, variables Pay and Received are initialized to have values 4350 (=10*435) and 8 (3+5), respectively.

 

· INTEGER, PARAMETER :: Quantity = 10, Amount = 435, Period = 3 · INTEGER :: Pay = Quantity*Amount, Received = Period+5

 

The following example contains a mistake. While the compiler is processing the initialization value for variable Received, the value of Period is unknown, although it will be defined on the next line.

 

· INTEGER, PARAMETER :: Quantity = 10, Amount = 435 · INTEGER :: Pay = Quantity*Amount, Received = Period+5 INTEGER, PARAMETER :: Period = 3

 

Reference variables

Basic Syntax

 

Declaring a variable as a reference rather than a normal variable simply entails appending an ampersand to the type name, such as this "reference to an int"

int& foo = ....;

 

Did you notice the "...."? (Probably, right? After all, it's 25% of the example.) When a reference is created, you must tell it which variable it will become an alias for. After you create the reference, whenever you use the variable, you can just treat it as though it were a regular integer variable. But when you create it, you must initialize it with another variable, whose address it will keep around behind the scenes to allow you to use it to modify that variable.

 

In a way, this is similar to having a pointer that always points to the same thing. One key difference is that references do not require dereferencing in the same way that pointers do; you just treat them as normal variables. A second difference is that when you create a reference to a variable, you need not do anything special to get the memory address. The compiler figures this out for you:

 

int x;

int& foo = x;

 

// foo is now a reference to x so this sets x to 56

foo = 56;

std::cout<< x <<std::endl;

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Data Abstraction & Overloading : Variables Initialization |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.