Explain reference oddities, C/C++ Programming

Assignment Help:

Reference Oddities

A reference variable can demote to any integer variable, be it in an array or a member variable from structure or class. Reference variables can demote to constants as well. For instance, take a look at the program given below.

                void func(int &intref)

                 {

                                ++intref;

                 }

                void main()

                 {

                  long lint = 20;

                  int sint = 10;

                                cout << "Before incrementing " << endl;

                                cout << "Small int = " << sint << endl;

                                cout << "Long  int = " << lint << endl;

                                func(sint);                                                           // Integer Variable

                                func(lint);                                                            // Long    Variable

                                cout << "After incrementing " << endl;

                                cout << "Small int = " << sint << endl;

                                cout << "Long  int = " << lint << endl;

 

                                func(25);                                                                              // Integer Constant

                                func(sint + 10);                                                  // Expression

                }

                Output :

                                                Before incrementing

                                                Small Int = 10

                                                Long  Int = 20

                                After incrementing

                                                Small Int = 11

                                Long  Int = 20

The program calls func() , which receives a reference to an integer, four times. First with an integer variable  , which gets duly increased, as can be seen from the output. It again calls the func() with long variable.  The compiler does not give a type mismatch error but instead , it makes a temporary variable of the correct type and assigns the value of the variable lint to this variable. The func() increments the temporary variable and thus as can be seen from the output , lint still has the same value. Same, the last two calls do not result in errors but temporary variables are created for them just like in the earlier case.

Though, since a mismatch between the data types of the actual argument and the formal argument is shielded by these temporary reference variables, programmers prefer to use pointers, if a function has to change the value of an argument. Besides having the pointer notation also makes it obvious that the function is intended to make changes to the arguments. Though, references are still used with structures , classes and objects extensively.

 


Related Discussions:- Explain reference oddities

Define a structure of student class, Define a structure of student class:  ...

Define a structure of student class:  Write a program to define a structure of student record in C. class student { char name[20]; int roll_no; int marks[2];

Coding, I want MODI method''s coding in c++ using all concepts of c++..plea...

I want MODI method''s coding in c++ using all concepts of c++..please help me its my project work...

Explain function templates, Function Templates Function templates give ...

Function Templates Function templates give you with the capability to write a one function that is a skeleton, or template, for a family of similar functions. In function ov

C, how to learn programming skills

how to learn programming skills

Amie, what happens when the following command is used? chmod u=rwx,go=r-x f...

what happens when the following command is used? chmod u=rwx,go=r-x foo

UltimateCarRadio Project, In this assignment, you will be modifying your As...

In this assignment, you will be modifying your Assign-05 code to use more inheritance. As well, in this assignment, you will be asked to use newand delete, throw and catch except

Padovan string, write a program that counts the number of occurrences of th...

write a program that counts the number of occurrences of the string in the n-th Padovan string P(n)   program in java // aakash , suraj , prem sasi kumar kamaraj college

C program to demonstrate pointer to variable, C program to demonstrate Poin...

C program to demonstrate Pointer to variable: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 //poin

Ascii string related, A string S is said to be "Super ASCII", if it contain...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd