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

Why php is also known as scripting language, Why PHP is also known as scrip...

Why PHP is also known as scripting language? PHP is fundamentally a general purpose language, which is used to write scripts. Scripts are normal computer files which comprises

Looping, Write a programme to display the patern.. A A B A C B A B C...

Write a programme to display the patern.. A A B A C B A B C A B A A

Board coloring, programing code on board coloring in clanguage

programing code on board coloring in clanguage

Thermodynamics, the program that solve the efficiency of otto cycle

the program that solve the efficiency of otto cycle

Subtraction of numbers, Two numbers to be subtracted using bitwise operatio...

Two numbers to be subtracted using bitwise operations

Explain the standard input/output iostream library, Question 1 Implemen...

Question 1 Implement a class stack which simulates the operations of the stack allowing LIFO operations. Also implement push and pop operations for the stack 2 Explain the c

Define multi file program, Define Multi File Program? While writing lar...

Define Multi File Program? While writing large programs we must divide programs up into modules. These would be separate source files and the main() function would be in one fi

Abcd, #question.gsdjfhjhjfkfk.

#question.gsdjfhjhjfkfk.

C++ programming, I need help programing this... In this exercise, you will ...

I need help programing this... In this exercise, you will create a program for the sales manager at Computer Haven, a small business that offers motivational seminars to local comp

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