C++ Structure :the members are public

Objectives

  • Structures : contains aggregated data-types
  • A struct differs from  a class, by having all members are declared as public; whereas the default scopes of the members of a class are private.
  • sturct can be initialized few different ways.
    • (a)  myStruct ms;
      ms.strructmem1 = 1234; // using a dot operator
      ms.structmem2 = 10;
    • (b) mystruct ms2 = { 1234, 10  }; // which should correspond the orders of

      (c) struct constructor
      struct mystruct
      {
      double d1, d2;
      mystruct() : d1(0,0), d2(0,0) {} // default constructor
      mystruct( double d3, double d4): d1(d1value1), d2(d2value1){}
      };
       

  • Unlike an arrray , struct can contain heterogeneous ( different data type) 
  • Code testMyStruct1.txt

Step: 1 Create a project

Step : 2 Create a source file

Step: Edit code Eclipse IDE provides intelligent data pop-up tips to pick and go. The illustration below shows, how to define a structure data-type . The member variables in different structure can have same name and type .

The above illustration shows heterogeneous data-types in structures.

Step: Runtime View