Classes and Structures in C++ are same except class defaults to private and structures to public. They can have data members, member function, this pointer, static member functions.
Union is also similar to classes and structures except:
1. unions cannot be used as base or derived class in inheritance
2. unions cannot have static members
3. defaults to public
Sample program showcasing unions in C++ similarities with classes in C++:
Union is also similar to classes and structures except:
1. unions cannot be used as base or derived class in inheritance
2. unions cannot have static members
3. defaults to public
Sample program showcasing unions in C++ similarities with classes in C++:
union personalInfo
{
int id;
char name[20];
void print()
{
cout<<this->id;
}
};
int main()
{
union personalInfo me;
me.id=10;
me.print();
return 0;
}
Login to comment.
Why create separate log in id? Log in with your
Leave your comment:
