C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Structures, Unions


Tutorials Cpp

Topic

How are Structures, Unions used in C++?



Explanation
Structure:

Structures in C++ is a collection of variables. Structures in C++ can be declared even without the keyword "struct". By default all the members of a structure are "public", even "private" members can also be declared in a function.

Syntax:
   struct struct-type-name{
       type name1: length;
       type name2: length;
       .
       .
       type nameN : length;
       }variable_list;
Example:

   #include <iostream.h>
   struct Emp
     {
       int empno;
       int empsal;
     };
   void main( )
     {
       Emp emp1= { 23, 12000};
       cout << "Employee Number::" << 
                         emp1.empno << '\n';
       cout << "Employee Salary:: "<< emp1.empsal;
     }

Result:
    Employee Number:: 23
    Employee Salary:: 12000

In the above example, the structure "Emp" is used initialize the integers, that are referenced in the "main()" function.

Unions:

Unions in C++ is a user defined data type that uses the same memory as other objects from a list of objects. At an instance it contains only a single object.

Syntax:
   union union-type-name{
       type member-name;
       type member-name;
   
     }union-variables;
Example:

#include <iostream.h>
union Emp
  {                      
    int  num;       
    double sal;     
  };
int main()
  {
     Emp value;
     value.num = 2;
     cout << "Employee Number::" << value.num 
           << "\nSalary is:: " << value.sal << endl;
     value.sal = 2000.0;
     cout  << "Employee Number::" << value.num 
        << "\nSalary is:: " << value.sal << endl;
     return 0;
  }

Result:
    Employee number is::2
    Salary is::2.122e-314
    Employee number is::0
    Salary is::2000

In the above example, only "value.num" is assigned, but still the "val.sal" gets a value automatically, since the memory locations are same.








Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting