Loading....
Coupon Accepted Successfully!

 

More Programs

4.35 Write a program to show the use of scope access operator.

 

Explanation: In the above program, variable j is declared in two scopes, that is, local and global. The variable j declared before main() is a global variable and the variable j is declared inside the main() function is local. Both global and local variables are initialized with 11 and 12, respectively. The local variable j can be accessed directly. The local variable hides the existence of global variable. The global variable can be accessed using (::) the scope access operator. The scope access operator prefixed with variable name displays the value of global scope and similarly we can also change the value. The output shows the values of local and global variables.

 

 

Tip: Whenever there is a tie between local and global variables, the local variable gets the preference.

 

4.36 Write a program to define three variables in different scopes and access them with and without a scope access operator.

 

Explanation: In the above program, the variable j is declared in three different scopes. First is defined in before main(), second is inside the main(), and the third one is also inside the main() but within another block. The first cout statement displays the values of variable j of local and global scope. The second cout statement that is inside the block displays the value of variable j local to the same block and the global variable declared before main(). The third statement produces the same output as the first.


4.37 Write a program to demonstrate the use of reference variable.

 


Explanation: In the above program integer variable x is initialized to 15 and y is declared reference to variable x. The variable x and y refers to the same memory location and change in one variable can affects other. In the above program values of variable x and y are modified and values and address are displayed. From the output we notice that value and address of the variable x and y are displayed same.


4.38 Write a program to pass structure variable into function using c and c++ style.

 

Explanation: In the above program, the structure boy is declared before main() with two data members name and age of char and int data type, respectively. Two user-defined functions are defined as cstyle() and cppstyle(). In function main(), b1 and b2 are two variables of the struct boy type and they are initialized. The address of b1 is passed to the cstyle() function whereas cppstyle() needs only variable name. The cstyle() function receives address and stores it in the pointer. The cppstyle() function accept reference of the actual variable and stores it in the format variable. The code inside these functions directly replaces the old values with new ones. The result is displayed as per the output.


4.39 Write a program to return an object by reference.

 

Explanation: In the above program, structure boys is declared. The objects b1 and b2 are declared of structure boys type. Both the structure variables are initialized. The function show() is declared and defined. The statement show()=b2; passes contents of b2 into function show(). The contents of variable b1 are displayed that is similar to b2. The function returns object b1 and again content of b1 is displayed.


4.40 Write a program to declare constant variable and calculate area of a circle.

 

Explanation: In the above program, variable r and area are of float type. The variable pi is constant and initialized to 3.14. The value of r is entered through the keyboard. Area of circle is calculated using the formula. The pow() function calculates the square of the variable r. The cout statement displays the area of the circle. The variable pi is constant and cannot be changed during the program execution.


4.41 Write a program to declare reference variable to const variable.

 

Explanation: In the above program, integer x is a constant variable and initialized with 15. The variable y is a reference variable to variable x. We cannot change the value of x as it is a constant variable. But we can change the value of its reference variable. The change in reference variable does not change the value of constant variable. The output shows the values of both the variables.


4.43 Write a program to declare a constant variable.

 

Explanation: The above program is the same as previous one. In this program the reference is declared as constant. We cannot change the value of the reference variable. A change made in actual variable changes the value of the reference variable.


4.44 Write a program to declare reference variable to character pointer and display the strings.

 

Explanation: In the above program, txt1 is a character pointer and initialized with the string “Eye”. The pointer txt2 is a reference variable to the character pointer txt1. The statement *txt1=‘B’; replaces the first character of the string with character ‘B’ and contents of both the pointers are displayed. The statement *txt2=‘d’; replaces the first character of the string with variable ‘d’ through the reference variable.


4.45 Write a program to return a constant reference.

 

Explanation: In the above program, the function show() and pointer s are declared as constants. It is not possible to assign any data to pointer s because it is a constant. The function show() is invoked and it returns string. The returned string is assigned to pointer s. The content displayed is “Hello”.


4.46 Write a program to use global and local variable and their pointer of same name and calculate their sum.

 

Explanation: In the above program, variable s and pointer sp are declared in two scopes, that is, global and local. The address of global variables is assigned to both the local and global pointers sp using the following statements:

  1. int *sp=&s.;
  2. int *sp=&::s;

In statement (a), the address of global variable s is assigned to global pointer sp. In statement (b), the scope access operator is used to access the global variable and ampersand pre-fixed assigns address of global variable to local pointer sp. Thus, both the local and global pointers point to the same variable. The output indicates contents of individual variables and pointers and sum of local and global variables. The last sum result is 30 since both pointers point to the same variable, that is global variable.


4.47 Write a program to perform division of local and global variable of same name.

 

Explanation: In the above program, G is a macro initialized with scope access operator. We can use G in place of (::) scope access operator. The variable loop is declared and initialized in two scopes (local and global). Using for loop repetitive division of global and local loop variable is performed and result is displayed.


4.48 Write a program to create variable with reference to pointer variable. Initialize and display the values.

 


Explanation: In the above program, integer a is declared and initialized with value 175. The pointer variable x is declared and initialized with address of variable a. The reference variable j is declared and value pointer x is assigned to it. The cout statement displays the values of a, j, and x. The value printed is 175. The integer variable q is declared and initialized with 147. Its address is assigned to pointer x and again q is initialized with 155. This time the values of a, j, and x displayed are 175,175, and 155, respectively. The value of pointer is altered but values of actual and reference variables are unaffected.


4.49 Write a program to use type casting and convert values from one to another data type.

 

Explanation: In the above program, x is an integer variable and y is a float variable. The variable y is initialized with value 2.1. The values 15.2 and 85.13 are displayed by converting them to integer using type casting syntaxes.

The value of float y is assigned to integer variable x using type casting. The values of x and y are displayed on the screen. Again variable y is assigned with x + 1.5. The result will be 3.5 and not 3.6 because by applying float type cast we cannot convert an integer value to float.





Test Your Skills Now!
Take a Quiz now
Reviewer Name