Introduction to Unions in C++ | CPP Programming Video Tutorial

preview_player
Показать описание
In this c++ / cpp programming video tutorials / lecture for beginners video series, you will learn about the introduction to unions with example.

You are gonna learn what is a union, how to declare or define it, how values are stored in them, how to access them in detail with examples.

watch the video on difference between structure and unions at

Learn Programming in HINDI at our youtube channel

Catch us on SocialMedia
Рекомендации по теме
Комментарии
Автор

I was the subscriber to this channel when it had subscribers less than 50k ♥ and this channel is booming around.
Thanks for the help Anil sir 🔥.
#BestTeacher

abhijeetraj
Автор

Sir pls solve..

1. #include<iostream>

using namespace std;

union emp{
int id;
double salary;
};

int main()
{
emp worker;

worker.id = 44;


cout<< worker.salary <<endl;

return 0;
}

output = 7.70858e=+260. Y?? cos u said tht the value = 44 will be available to both the variables..then y oesn't it appears when I access it with salary?

2. #include<iostream>

using namespace std;

union emp{
int id;
double salary;
};

int main()
{
emp worker;

worker.id = 44;
worker.salary = 65;
worker.id = 32;



cout<< worker.salary <<endl;

return 0;
}

output is still 65. Y?
when i hv initialised the value of id agn with 32 then the previous value of salary has to be deleted bcos they both are sharing the same memory spaces then how come the same memory can store two values at a time?

nikhilarora
Автор

What is the practical use case of Unions?

wwewwf
Автор

Does the memory size of each data member must be the same?

okfine
Автор

Useful video but what if i have a method that returns a variable from the union structure, how would i declare that method type? for example i want the method to conditionally return either a DWORD or float.

REAL-kopuzz
Автор

Thanks for the help. How do I cite your work.

MultiGeesh
Автор

so union is just like reference variables???

ishaqkhan
Автор

Are "sting" variables allowed as union members? Can't get mine to compile even with #include <string> headed..

rodwynnejones
Автор

Why do you type anil and ras down there??

KansasFashion
Автор

what is the use of anil and ras over there

prudhvibikumalla
Автор

Hello, i got 1 question, what can i use this for? it's really useless for me, thanks nice tutorial.

Martin-wzrm
Автор

why inside union we can't have string member?
its showing me error

adityasaxena
Автор

hi anil i have a doubt when i used this union operator and i tried to change the value of age then again i accessed it using a different value then it is showing me some garbage values here is the program which i  have done                                          #include <iostream>

using namespace std;
union student
{
    int age;
    float salary;
};

int main()
{
    student manish;
    manish.age=24;

    cout << "the age of manish is ="<<manish.age<< endl;
    manish.salary=8000;
    cout<<"the salary of manish is="<<manish.salary<<endl;
    cout<<"the age of manish is ="<<manish.age<<endl;
    return 0;
}
 
whats the fault in that
                                                                                                                                                      

bendhimanish
Автор

As a noob, I do not understand why we would use a union; why do I want to assign only 1 value to all variabes? 

CindyK
Автор

#include <iostream>

using namespace std;

struct emp{
int id;
float sal;
};

int main()
{
emp worker;

worker.id=65;
worker.sal=12000;

cout << worker.id << endl;
cout << worker.sal<< endl;
return 0;
}

output gives values of both id and salary.... how?? only one value is store at a time then how both values are shown....
and which one is more prior ?? int or float ??

And isnt union useless?? like we can only acces one member at a time... how does it help??

diptishramteke
Автор

THEN WHAT IS THE USE OF UNION?? WHY UNION WAS CREATED ON THE FIRST PLACE??

harshpalsingh
Автор

why do Akshay and Anjali stay aside from anil and ras? Are they angry with those ones?

ДимаКимсыщик
Автор

Sir ...
Im getting output as ...anjali.id
Not 25 🙁

sudhathonta
Автор

Learn to pronounce "FIVE".
What is that IDE?

TheUltimateLizard
welcome to shbcf.ru