Reference data type in C++ | C ++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to nourish 80 this is Kishore and today we are going to discuss about reference variables okay today we are going to start reference variables concept now what is called reference variable actually it is a new concept implemented or introduced in C++ actually in C language we are going to use local variables global variables like this okay along with this in C++ they have introduced one new concept called reference variables and here a reference is a new data type okay and this kind of variables are called reference variables here the most important thing is reference is a new data type which is introduced in C++ under it is a derivative data type already we know that generally we are reading point arrays okay functions we are calling what they are deriving data types and we are using this point of arrays functions in C language and in C++ also we are using the same thing but along with this they have introduced one new data type called reference the data type okay and it is a de Rivel real-time and void we need the reference to data type in our programming what is the uses of reference a data type or reference variable in our programming actually we are using pointers in functions okay and based on this functions are divided into two everybody knows we can pass the parameters to the functions in two ways one is what call-by-value another one is called by address okay in C language we are sending the parameter to the function into this one is called by value another one is called address along with this they are going to use call-by-reference technique in C++ with these reference variables or reference data type now why they have been introduced because of in C language when colbert addresses passer we have to declare formal parameter as a pointer and we know that pointer always takes the at the okay pointer always Tosa address of another variable unemployed or the problem is we can initialize any variable address in the future means first one variable address is facet to the pointer later we can change the address of this pointer means we can assign another variable or does to be finder later another variable due to this which variable address it is going to store means now the user is getting confusion which variable Odyssey stored in point that's why to avoid this problem under the main thing is pointers are not secure because of later also we can change the pointer direction means Direction okay here the pointers are not secure why because means pointers stores any variable address means of same type at any time due to this using point we can change the address at any time that's why they are not secure to avoid this problem especially they have introduces a reference data type are reference variables know what is reference direct I am going to do some more things it's actually reference data type our reference variables they're also called link variables which are also known as alias variables you know that alias means what alternative name that means we are providing one name generally in our houses we are having nickname okay generally we are having nicknames means was already name but somebody is calling with another name but both belongs to same person now here also reference workers are the same concept reference variables allows the user to provide alias names for existing variables that's why they also call alias variables okay are linked variables next they are very much useful in copy constructors and operator overloading topic and another important thing their implicit constant okay here reference variables or the implicit pointer constraint okay that means you know that constant means which is never change that's why they also called read-only point her okay next how to declare the difference variables first of all I am giving the syntax just watch it every reference variable is declared like this faster data type next one ampersand and here very next here another available that's all it is how to define a reference variable in C++ first data type later ampersand and here ampersand is not a disk operator generally ampersand is called address operator but in C++ here the meaning is it is a reference variable here ampersand indicates reference a data type that means without ampersand symbol it is a variable okay and it is declared from this data type for example int suppose ye now watch it is generally we are calling a is a normal variable which kind of variable means integer type variable and everybody knows integer is a primitive data type know what this ampersand ee now what happened means now a will become reference data type now a is a reference variable or ampersand indicates a is a reference data line but here the basic concept is integer is the primitive now from primitive we are deriving the difference that's why reference is called derive the data type next here the most important thing in difference variable sees every reference variable should be initialized every difference variable should be initialized with another variable value it is the most important point okay regarding to the reference data that means every normal variable is initialized before or later okay with another value or with another variables but here the point is regarding to the reference data type are reference variables they should be initialized with another variables only that's why here I am going to change the statement now what is the statement made for example int equal to 100 now a is a normal variable next feed is ampersand B now ampersand indicates B is a reference data not as per our discussion every reference variable should have to initialize with another variable here which one is the another variable a that's why now the B should be initialized return a value that's why it is the thing now say this now the B value initialize it with a that's why here B is the alias name for a means a and B indirectly both are same now how can we say both are same suppose later after this expression I have changes a value to 200 now I am going to print the C out e/b other point is actually a value hundred and later a design to be later a value change it to 200 and now watch it what happened now this way gives value to hundred and we also use the value 200 why because means just so before we have discovered it is a reference which is also called aleeah's aureus means what another name here another Raymond nvivo / same that's why when a changer means we also changed the changer means he also changed for example here be equal to 300 now see ot be now what happened a value becomes 300 and B value becomes 300 okay that's why here can I say here and we will power equal okay here the point is okay when you rarely change it to be value also change it to be really change me to a value also change that's why I'm calling reference variables are nothing but linker variables are link variables which are also called a copy or which are also called alias okay you know that generally we know the Harbhajan same okay but in our peppers we are getting but G selected for Test cricket okay for the next text match that means here but these selected means what who is selected Harbhajan Singh is selected that means Harbhajan Singh nickname is budgie no but this electron is Harbhajan Harbhajan means budgie both are same here same thing and B both are linked to the variable that's why any one variable change in winter another variable also change era okay that's why they're called linker available for alias variables next here another important thing most important thing regarding to alias variables are are reference variables is the reference variable should be initialized at the time of declaration only suppose in normal variable int see now it is a normal variable now see if initialized riveter suppose 300 it is possible and this one also possible suppose int see declared in one line c equal to 300 in one mind now it is also allowed that means normal variables are initialized in the same line are in the future also but here the most important thing is reference variable should be initialized at the time of declaration only further initializations are not allowed that's why they are sacred means later the user is not able to change the address are pointing that's why french variables are secured when compared with pointer variables when it is a pointer we can change the address at any time that means using one pointer we can find any address at any time but there does not allows now there is only one time the same feature we are not able to change this address now that is why they are secured when compared with pointer variables that's why reference variables are introduced in three places okay it is reference variable how they are working in our programming now I am going to give you one example for reference variable okay I am going to start the example for reference variables that means how the reference variables are working in our programming I will go with one example okay as usual first of all we have to start with the header file now the major cyclical a set of answer I was team not H and stdio.h common but for here I am going to start hash include I trim that hedge no it is the CEO conceal next hash include Cunha Rotich now the main function is started Lex nope I'm going to declare normal variable for example int a equal 200 okay later I'm going to start like int ampersand B equal to a and int ampersand equal to B now a is a normal variable and B is a reference variable and be referencing a and C also referencing value on C referencing be next right now a value 100 I want to print the world first of all clean clear okay later I am going to bring to the a value first now C out a b c that means i want to print as ABC well okay when the statement is executed now the answer is like this host a value a value 100 next B value B value also 100 because of B is referencing you know now hundred and see also another resonance that's why 100 now available in there be value printed C value also print later watch this this time I am going to change the C value C value equal to 200 now I have changes the C value C equal to 200 now watch it what happens see out yes B C now C is the reference for me no that's what we also changed B is the reference of you know that's what we also changed and a is the original now we also change that's why your answer is looking like this now 200 200 200 next get CH program finished okay now the program is completed according to this example according to this example they also ellos today also means which one reference variables also allows to share the data between the functions generally in C language to share the data in between the functions we are using call by a drastic think varying call by address technique but here we are using call by reference technique for sharing the data between functions on how it is possible I am going to explain with one more example okay just watch the example and here it is the example how the reference variables are working in our programming now I am going to give you what is called by reference okay because of our topic is reference variables now that's what I am going to cover with one reference variable example since here I'm going to use the swap function everybody knows how to swap two variables using a function in general in C language we are going to use this one boiled swept off int starry a common int star V okay now pointer concept means call the address first of all I may explain okay first of all I am going to explain call by address next int temporary for example temporary variable equal to hear story now a value stroll on here story a equal to star V under star V equal to M now the function close and here it is the normal program while main means I am going to call the function from main function it is here int a equal to 10 B equal to 20 now a wily initializer with the pen B value 20 letter I'm going to see a lot is here clean clear ok other screen content also clear now I want to put into the a value B value ok but before going to print of a a and B value I want to swap the values that's why I am going to call this function swap off ampersand ampersand V generally it is called function call when this function is called what happens a Odyssey is possible to this way and we address is positive this B that means here we are sending what actual parameters address they're sending actual parameter address to the formal parameters when it is address the formal parameter should be declared as point that's why I have declared star a star B okay now yeah deceased or in a beatiful sterling me later here you Valerie please leave it to be the early replacing it to a make sure they're under sorry place it means in our this values are replaced that's why a value 20 B value 10 now firstly out a equal to a next /t next be equal to V equal to B and L now okay here angle also now get fahisha directly and program close what happened watch it a equal to a actual after shock now we're printing a value after shop a value becomes 20 and V value becomes 10 the sponsor is 8:20 be 10 and here what we are sending a and B address the fire this concept is called what call by address now impious of call where others I am going to use call by reference means I'm going to use reference variables technique okay and how this one is working say this here address there is no need of address just to remove the address next here there is no need of point just remove the pointers in place of that just use your own personal okay just before you how discusses any variable is torture which ampersand means it is called reference variable any variable which is started with numbers means it is called the first that means what it is the alius for nickname for ye that's why here any change in you affects bc also know and any change in be effect this be also that's why after the function call a value 20 be value and I'll here the point when it is reference variable there is no need of pointer that's why I remove the Stars indirection operator not it now see this it is generally looking to be a call-by-value just to a program just after it it is looking to same to same call by value except the reference type okay with our difference type it is nothing but a call right well if I remember this here they are implicit pointers implicit constraint pointer okay here reference variables are also called implicit pointer constant and we are not able to change the address another time in second time we are not able to change the reference variable address that is why they are secure it is how to use reference variables in swap function or okay according to this example I want to say one more thing here actually value can be reallocated and we are go to local variables which are declared inside the main function now U and V are posted to this variable snow and here point is when a and B values are changing here they are reflecting on main function also that means reference variables LoZ the concept of data sharing between the functions okay all that I said difference variables are used to share the data between the functions now according to this example can be at the local but they are access should outside the function that is why reference variables are also used to share the data between the functions it is that's why instead of pointers are called by address we can use a reference or call by reference okay thank you for watching in next session we are going to continue the remaining [Music]
Info
Channel: Naresh i Technologies
Views: 79,359
Rating: undefined out of 5
Keywords: Reference data type in C++, Reference, data types in C++, C++, programming, c (programming language), C ++ Tutorial, Kishore, Naresh IT, C++ Training
Id: jyAyzvOvL-I
Channel Id: undefined
Length: 23min 0sec (1380 seconds)
Published: Thu Mar 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.