Class 3
Class 3
A reference, as the name itself suggests, is like an Alias. It serves as an alternative name for the object being referred. A reference is like a pointer. But we should not initialize it to the address of an object. Ex: #include<iostream.h> Void main() { int x = 5; int &y = x; // y is a reference of x. cout << x = << x << endl; cout << y = << y << endl; x++; cout << x = << x << endl; cout << y = << y << endl; } Output: x=5 y=5 x=6 y=6
Ex: int &y = x ; y is a reference of x. the reference is indicated by an address operator(&). Following points are remembered in dealing with reference data type. 1. A reference must always be initialized. Ex1: int x; int &y = x; //valid reference : y is reference to x. Ex2: int x =100; int &y ; // error in reference. y = x;
2. Once a reference is defined for a particular object, it cannot be made to refer to any other objec Ex1: int x =100; int &y=x; // reference. int z; y = z // Z value refer to y both are not allowed Or &y = z // illegal; since y is already referred to x; 3. All operations that are performed on the reference are actually applied to the object of that reference. Ex1: int x =100; int &y = x ; y = y + 10; // operators on reference y of x. This actually adds 10 to the value of x. thus both x & y will have value of 110.
4. Each definition of a reference must be prefixed with the address operator. Ex: int a = 10, b = 20; // two objects of type int. int &refa = a, &refb = b; // two references. 5. Like a reference to an int, or a float or a char, a reference to a pointer can be created. char *ptr = Hello; char *&refptr = ptr; // refptr is a reference to the pointer ptr. 6. We can define a multiple references to an object. But changing the value of one of them would change the value in all others. int x = 10; int &y = x, &z = x, &refx = x; // 3 reference. y = y+5; This would cause a change in the value of x & z. so, all the three have the value 15. 7. An array of references is not allowed Ex1: int x,y,z; int &arr[3] = { x,y,z}; // not allowed error.
8. A constant reference can be initialized to an object that belongs to the different data type ( provided there is a conversion from the one type to the other ) as well as to non addressable values, such as literal constants. Ex1: double dval = 3.14159; const int &refval = dval ; // const refer to the double dval;
Similarly, we can also define a const reference to the literal constants. Ex2: const int &ir = 1245;
Ex 3:
double dval = 1024 ; const int &ri=dval; the compiler transforms this in to int temp = dval; const int &ri = temp;
#include<iostream.h> void main() { double dval = 1234.44; const int &ri = dval; cout << dval = << dval << endl; cout << ri = << ri << endl; dval = dval + 5; cout << dval = << dval << endl; cout << ri = << ri << endl; }
output:
dval = 1234.44 ri = 1234 dval = 1239.44 ri = 1234 For ri=ri+5//error :cannot modify a const object.
ARRAY TYPES:
An array is a group of objects that belong to the same datatype. array of int, array of char, array of float All these objects are stored in the consecutive memory locations. Each object is accessed by its position in the arrays. This type of accessing is referred to a indexing or subscripting.
Initialization:
1. An array can be initialized by initial values. That is each array element is assigned a value. const int MAXSIZE = 5; int arr[MAXSIZE] = {10,20,30.40,50};
2. It is not necessary to specify the dimension value when you explicitly initialize the array. float x[ ] = {0.2, 0.8, 1.0, 3.5 }; 3. If the number of initial values is greater than the specific dimensional value then compiler will generate a diagnostic error message. int A[4] = {1,2,3,4,5} ; //error (too many intializers). 4. If the number of initial values is less than the dimension value, all elements which are not explicitly assigned are set to zero. float x[4] = {1.2, 2.3} x[0] = 1.2, x[1] = 2.3, x[2] = 0, x[3] =0 .
5.
Like a numerical arrays, characters arrays can be initialized with i. A list of character constants ii. A string constant Ex: const char chararr[ ] = { A, S , Y }; const char str[ ] = HELLO
6. Initialization of one array to another array is not allowed int a[10], b[10]; a = b ; // is an error. One array can be copied in to another only on element by element basis.
Multidimensional arrays:
If an array has more then one dimension it is called multidimensional array. 1. Two dimensional array 2. Three dimensional array
One to one mapping is preserved i.e. first set of initial values is assigned to first row elements, second set of initial values is assigned to second row elements & so on. If the number of initial values in each initializing set is less than the number of corresponding row elements then all the elements of that row are automatically assigned to zeroes. If the number of initial values in each initializing set exceeds the number of corresponding row elements then there will be a complication error.
1. 2. 3. 4.
The address of the address can be accessed by & a[0] or simply a. Value housed at this address can be a[0] or *a. The address of the second element in a array a &a[1] or (a+1) Value at this address is accessed by a[1] or *(a+1)
Similarly the address of the ith element is obtained by &a[i] or (a+i) The value stored in this address can be accessed by a[i] or *(a+i)
Here x gets a value true & y gets values false. 1. The definition, true has a value 1 when converted to an integer & false has a value 0. 2. The integers can be implicitly converted to bool values: non zero integers convert to true & zero converts to false. 3. bool can be used in arithmetic & logic expressions. If so done they are converted to integers and arithmetic logical operations are performed on these integers. The result of these operations can be converted back to bool; a zero is converted to false & non-zero value is converted to true.
Ex: #include<iostream.h> void main() { bool b = 32; int i = false; cout << endl << b = << b << endl << i = << i << endl; int j = b + b; bool k = b + b; cout << j = << j << endl << k = << k << endl; }
Output :
b=1 i =0 j=2 k= 1
#include<iostream.h> #include<iomanip.h> #include<conio.h> int main() { clrscr(); double a=44.5555; cout.precision(3); cout<<a<<"\n"; return 0; getch(); }