What is Struct

struct ExampleStruct {
 Type value;
 Type value;
 Type value;
 // No functions!
 };
  • struct is a class where everything is public
  • Use struct as a simple data container
  • if it needs a function it should be a class instead

Always initialize structs using braced initialization.

#include <iostream>
#include <string>
using namespace std;

// Define a structure.
struct NamedInt {
  int num;
  string name;
};

void PrintStruct (const NamedInt& s) {
  cout << s.name << " " << s.num << endl;
}

int main(int argc , char const* argv []) {
  NamedInt var = {1, "hello"};
  PrintStruct (var);
  PrintStruct ({10 , "world"});
  return 0;
}