fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Basic structure, containing only two elements of type int
  6. // We use the word "struct" to define structure
  7. // After the word struct we write the name of our structure
  8. // By default we should begin names of structures with upper case
  9. struct Point {
  10. int x;
  11. int y;
  12. };
  13.  
  14. int main() {
  15. // After creating the structure we can use it as a new type for our variables
  16. // Now we create variable named "point" of type Point
  17. Point point;
  18.  
  19. // We assign new value to our point variable
  20. // We assign values to the corresponding fields of the Point structure
  21. // 5 is assigned to the first field in the structure - x
  22. // 3 is assigned to the second field in the structure - y
  23. cout << "Creating new point with x = 5 and y = 3" << endl;
  24. point = {5, 3};
  25.  
  26. // To access the field from our variable of type Point we write: variable_name.field_name
  27. // For example to access the field x: point.x
  28. cout << "Point x: " << point.x << endl;
  29. cout << "Point y: " << point.y << endl;
  30.  
  31. // We can also assign new values to fields this way
  32. cout << endl << "Assigning new values to the point variable" << endl;
  33. point.x = 20;
  34. point.y = 13;
  35. cout << "Point x: " << point.x << endl;
  36. cout << "Point y: " << point.y << endl;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5504KB
stdin
Standard input is empty
stdout
Creating new point with x = 5 and y = 3
Point x: 5
Point y: 3

Assigning new values to the point variable
Point x: 20
Point y: 13