0% found this document useful (0 votes)
38 views

Class 3

1. A reference is an alias for an object that allows access to the underlying object. It is initialized when declared and cannot be made to refer to another object. 2. Any operations on a reference are applied to the referenced object. Multiple references can refer to the same object, so changing one changes the others. 3. Arrays allow storing multiple objects of the same type in contiguous memory locations that can be accessed via an index. Multidimensional arrays generalize this to multiple indices. References and pointers are closely related for arrays since array names store address of the first element.

Uploaded by

dhanya_h_1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Class 3

1. A reference is an alias for an object that allows access to the underlying object. It is initialized when declared and cannot be made to refer to another object. 2. Any operations on a reference are applied to the referenced object. Multiple references can refer to the same object, so changing one changes the others. 3. Arrays allow storing multiple objects of the same type in contiguous memory locations that can be accessed via an index. Multidimensional arrays generalize this to multiple indices. References and pointers are closely related for arrays since array names store address of the first element.

Uploaded by

dhanya_h_1
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Reference Type:

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.

Declaration odf an array:


Data type specifies = data type. Identifies name of the array Dimension indicates the no of elements in the array & enclosed with a pair of brackets. Ex: int arr[10]; arr is an array of 10 elements of the type integer. Arr[0] to arr[9];

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

Initialization of 2D- array:


const int MAX = 10; const int MIN = 10; int AMAT[MAX][MIN]; or int AMAT[2][3] = {{1,2,3,} , {4,5,6}}; {1,2,3} 1st set containing first row initial values {4,5,6} 2nd set containing second row initial values

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.

Relationship of Array & Pointer types :


The name of an array itself designates some memory location and has its corresponding address. This address is the address of first element of an array. Since array name holds the address of the first array element, by definition, it is a pointer. int a[3] = { 10, 20, 30};

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)

The Bool type : Bool


The bool object can be assigned the literal values true or false. It is most commonly used to hold the results of comparisions. Ex: bool x,y; int a = 10, b=20, c=30; x = a<b; or y = b>=c;

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(); }

You might also like