Structures


What is a structure

If you start programming code in C++ you'll sooner or later be facing a situation in which you'll need to store coherent data in a structured way. This is where structures come in place...

A structure is basically a user-defined datatype which consists of other datatypes like int, char, etc. ...

Defining a structure

//This is how you'll usually define a structure    
using namespace std;
struct MyOwnStructure { // keyword "struct" followed by a name, followed by braces containing the datatypes you like, followed by a semicolon
    int property_one;
    int property_two;
    char property_three;
    bool property_four;
    //...
};

Using a structure

int main (void) {
    MyOwnStructure demo1; //Declare demo1 of type MyOwnStructure
    MyOwnStructure demo2; //Declare demo2 of type MyOwnStructure

    //set demo1's with values
    demo1.property_one = 1;
    demo1.property_two = 2;
    demo1.property_three = 'a';
    demo1.property_four = false;

    //set demo2's values
    demo2.property_one = 3;
    demo2.property_two = 4;
    demo2.property_three = 'b';
    demo2.property_four = true;

    cout << "Demo1: " << demo1.property_one << demo1.property_two << demo1.property_three << demo1.property_four << endl;
    cout << "Demo2: " << demo2.property_one << demo2.property_two << demo2.property_three << demo2.property_four << endl;
    /*
    Will output
    Demo1: 12a0
    Demo2: 34b1
    */
    return 0;
}

Exercise

  • Create a structure named "person" with the following fields
  • name (should be string)
  • age (should be int)
  • do_programming (should be bool)
  • declare two objects "p1" and "p2" of your structure's datatype
  • set the values for "p1" as follows:
    • name: alice
    • age: 20
    • do_programming: true
  • set the values for "p2" as follows:
    • name: bob
    • age: 18
    • do_programming: false
  • "cout" their informations in the scheme "name (age)" => e.g. tim (23) by getting the values from your structure-objects

Copyright © learn-cpp.org. Read our Terms of Use and Privacy Policy

If you start programming code in C++ you'll sooner or later be facing a situation in which you'll need to store coherent data in a structured way.\nThis is where structures come in place...\n

A structure is basically a user-defined datatype which consists of other datatypes like int, char, etc. ...\n

Defining a structure\n
//This is how you'll usually define a structure    \nusing namespace std;\nstruct MyOwnStructure { // keyword \"struct\" followed by a name, followed by braces containing the datatypes you like, followed by a semicolon\n    int property_one;\n    int property_two;\n    char property_three;\n    bool property_four;\n    //...\n};\n\n

Using a structure\n
int main (void) {\n    MyOwnStructure demo1; //Declare demo1 of type MyOwnStructure\n    MyOwnStructure demo2; //Declare demo2 of type MyOwnStructure\n\n    //set demo1's with values\n    demo1.property_one = 1;\n    demo1.property_two = 2;\n    demo1.property_three = 'a';\n    demo1.property_four = false;\n\n    //set demo2's values\n    demo2.property_one = 3;\n    demo2.property_two = 4;\n    demo2.property_three = 'b';\n    demo2.property_four = true;\n\n    cout << \"Demo1: \" << demo1.property_one << demo1.property_two << demo1.property_three << demo1.property_four << endl;\n    cout << \"Demo2: \" << demo2.property_one << demo2.property_two << demo2.property_three << demo2.property_four << endl;\n    /*\n    Will output\n    Demo1: 12a0\n    Demo2: 34b1\n    */\n    return 0;\n}\n\n

Exercise\n
    \n
  • Create a structure named \"person\" with the following fields\n
  • name (should be string)\n
  • age (should be int)\n
  • do_programming (should be bool)\n
  • declare two objects \"p1\" and \"p2\" of your structure's datatype\n
  • set the values for \"p1\" as follows:
      \n
    • name: alice\n
    • age: 20\n
    • do_programming: true\n\n\n
    • set the values for \"p2\" as follows:
        \n
      • name: bob\n
      • age: 18\n
      • do_programming: false\n\n\n
      • \"cout\" their informations in the scheme \"name (age)\" => e.g. tim (23) by getting the values from your structure-objects\n", "code": "#include \nusing namespace std;\n\n//Your structure here...\n\nint main (void){\n //Declaring person's\n //Your code here...\n\n //Setting person 1's values\n //Your code here...\n\n //Setting person 2's values\n //Your code here...\n\n //Printing out\n //Your code here...\n //cout << name << \" (\" << age << \")\" << endl;\n \n\n return 0;\n}", "back_chapter": "Welcome", "previous_chapter": "Pointers", "next_chapter": "Function_arguments_by_reference", "links": [], "output": "\nalice (20)\nbob (18)\n", "solution": "#include \nusing namespace std;\n\nstruct person { \n string name;\n int age;\n bool do_programming;\n};\n\nint main (void){\n //Declaring person's\n person p1;\n person p2;\n\n //Setting person 1's values\n p1.name = \"alice\";\n p1.age = 20;\n p1.do_programming = true;\n\n //Setting person 2's values\n p2.name = \"bob\";\n p2.age = 18;\n p2.do_programming = false;\n\n //Printing out\n cout << p1.name << \" (\" << p1.age << \")\" << endl;\n cout << p2.name << \" (\" << p2.age << \")\" << endl;\n\n return 0;\n}", "is_tutorial": true};