Structs C++ - Reading Assignments

  1. Structs allow us to group variable of different data types together into a single unit.

  2. Individual variables in a struct are called members.

  1. structname.membername
  1. A struct within a struct is a nested struct
1 Like

1 -> To have the possibillity to store multiple (and more important different) variables in one struct in a easy way
2 -> Each individual variable or data type stored in a struct is a member
3 -> For example the struct is called “Employee” and has a member called “Age” so to access the member in the struct is as follows: Employee.Age
4 -> a nested struct is a struct that contains another struct

1 Like

Structs C++ - Reading Assignment.

  1. We need struct because it allows us to group variables of mixed data types into single unit.

  2. Members in a struct are variables that are part of the struct.
    eg.
    an Employee is a declaration and his/her age, wage are referred to as the members.

  3. We can access a member inside a struct by using the member selector operator (which is a period).
    eg.
    john.id

  4. A nested struct is a structure within a structure.
    eg.
    using a member selector operator twice.
    myCompany. MD . wage

2 Likes
  1. Why do we need structs?
    Structs are needed to group different variables of mixed data types together into a single unit.

  2. What are members in a struct?
    Members are variables in a struct.

  3. How do you access a member inside a struct?
    structName.member

  4. What is a nested struct?
    A struct within a struct.

2 Likes

1. Why do we need structs?
because we can create an aggregate data type. it looks kinda like an object (correct me if i’m wrong), but a struct is almost like an object but you can specifie what values and parameters can be stored within the struct.

2. What are members in a struct?
these are variables that are in the structure

3. How do you access a member inside a struct?
structname.membername

4. What is a nested struct?
this is a struct you can put inside of another struct but with other members.

1 Like
  1. We need structs when we need to group variables of mixed data types together into a single unit.
  2. Members are the variables contained within a struct.
  3. struct.member
  4. A struct that contains another struct.
1 Like
  1. Structs are needed to group multiple individual variables of mixed data types together. This then allows us to pass this information out much more easily then if the multiple data points were stored separately.
  2. Members in a struct are the multiple variables contained within the struct.
  3. To access a member inside a struct, one must use a “member selection operator”, which is a simple period. The the struct variable is used to refer to the struct, we then use the period immediately after the struct variable, after which we list the member variable we wish to access; i.e. Car (struct), Car sedan (struct variable is “sedan”), sedan.doors (“doors” is a member variable for sedan).
  4. A nested struct is a struct which contains another struct.
1 Like
  1. Why do we need structs?

A struct (short for structure) allows us to group variables of mixed data types together into a single unit.

  1. What are members in a struct?

These variables that are part of the struct are called members (or fields)

  1. How do you access a member inside a struct?

In order to access the individual members, we use the member selection operator (which is a period).

  1. What is a nested struct?

You can nest one struct into another struct, this is a nested struct

1 Like
  1. Why do we need structs?
    = To create more advanced objects consisting of more than 1 variables.

  2. What are members in a struct?
    = Members are the variables in a struct.

  3. How do you access a member inside a struct?
    = structName.memberName

  4. What is a nested struct?
    = A struct inside a struct.

1 Like
  1. We need structs to group multiple variables of mixed data type together.

  2. Members in a struct are the discreet variables contained within the struct.

  3. To access a member within a struct we use a member selection operator (period).

  4. A nested struct is a struct inside a struct.

1 Like
  1. To group variables of mixed data types together into a single unit.
  2. The variables within a struct.
  3. struct.member
  4. A struct contained within a struct.
1 Like
  1. We need structs because creating individual variables that are all representative of a similar object, yet are all separate form each other in coding, can get pretty unorganized quickly.

  2. …That’s why we are able to group these related variables together inside of a struct, using “member variables.” We can then assign (initialize) values to these using correct coding (ie struct Employee
    {
    short id;
    int age;
    double wage;
    };

Employee will{34, 29, 75000.0}

  1. …and access this information by:
    cout<<"Will’s ID is: "<<Employee.will<<endl;
    cout<<"Will’s age is: "<<Employee.will<<endl;
    cout<<"Will’s wage is: "<<Employee.will<<endl;

returns …
Will’s ID is: 34
Will’s age is: 29
Will’s wage is: 75000.0

  1. A nested struct is when you use a previous struct as a member inside of another struct. A function can also return a struct.
1 Like

1. Why do we need structs?
Structs come in handy when you want to declare more than one variable for a single user. These variables can be called a lot more efficiently than if you declared them one-by-one.

2. What are members in a struct?
These are the variables within a struct.

3. How do you access a member inside a struct?

struct.member

4. What is a nested struct?
This is a struct that has been defined within another struct. An example of this is: Employee being defined within the Company struct.

1 Like

Will is not a member of struct Employee :stuck_out_tongue:

  1. to be able to store variables of mixed data types in one place
  2. members are variables that are part of the struct
  3. we can access a memeber with member selection operator (a period)
  4. nested structs contain other structs inside
1 Like
  1. Why do we need structs?
    It’s useful in the cases where we need more than one variable in order to represent an object.

  2. What are members in a struct?
    Members are the variables that are part of the struct.

  3. How do you access a member inside a struct?
    The struct dot(.) and then the member

  4. What is a nested struct?
    It’s a struct that contain another struct.

1 Like
  1. We need structs in order to define aggregated variables.
  2. Variables or other structs.
  3. StructName.memberName
  4. A struct within a struct
1 Like
  1. Structs allow us to declare our own variable types to then catalogue and reference many different different objects and arrays of those types.
  2. Members in a struct are the variables part of a struct.
  3. You access a member of a struct by writing the name of the struct, then a period, and then the data type of the desired member.
  4. A nested struct is a struct within another struct.
1 Like
  1. structs are useful for grouping individual variables of different types together in one structure
  2. members are the variables that are contained within the struct, can also be called fields
  3. access with a member selection operator which is a period
  4. nested structs can contain other structs
1 Like

1. Why do we need structs?

So that we can aggregate similar properties into a single object and avoid writing extra code. We can pass closely related properties as a single struct object to a function as opposed to passing each variable from the group.

2. What are members in a struct?

The variables/properties defined within a struct are called members.

3. How do you access a member inside a struct?

We use the . operator after the struct name and type the member we would like to access.

Example:

struct Person{
string name,
int age,
string gender
}

int main() {
Person person;
person.name = "John"

...etc...
}

4. What is a nested struct?

A struct that contains another struct as its member.

1 Like