Structs in C++

The C programming language had the concept of a structure, grouping data together to form a complex data type (for example: complex numbers, Person record). C++ extends this concept by allowing methods to be associated with this data, so for example in C we could:

  
  struct A{
    char* someString;
    int someint;
  };

  // use like
  A testA;
  A *ptrtoA = new A;

  testA.someString = "something";
  ptrtoA->someint = 4;
  

In C++ we can add methods:


  struct Account{
      Account( ... );
      virtual void makeLodgement( ... ); //etc..
    private:
      int theAccountNumber;
      float theBalance;
  };

Structs and Classes are very similar in C++; however, members in a struct are public by default and members in a class are private by default. The 'C' language only allows public member data within structs (there is no concept of private or protected in C).

We can construct any class from a struct and vice-versa. It is a question of style, however a class is more commonly used for a data structure that has associated methods.