Structs C++ - Reading Assignments

  • Why do we need structs?

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

  • What are members in a struct?

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

  • 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)

Employee joe; // create an Employee struct for Joe
joe.id = 14; // assign a value to member id within struct joe
joe.age = 32; // assign a value to member age within struct joe
joe.wage = 24.15; // assign a value to member wage within struct joe
  • What is a nested struct?

A struct containing other structs

1 Like
  1. It’s more convenient, because it allows us to group variables of mixed data types together into a single unit.
  2. Members are the variables which belongs to struct.
  3. StructName.memberName.
  4. Nested struct is a struct which contains another struct inside itself.
1 Like
  1. Structs allow us to group multiple variables of mixed data types together in one unit, for easier organization and accessibility.

  2. Members (or fields) of a struct are its variables. For example, if you wanted to have a person as a struct, then the struct’s member might be weight, height or age.

  3. You can access members in a struct using the member selection operator: a period ( . ).

e.g. Person.age = 25

  1. A nested struct is a struct within a struct. For example, a company might have different departments, and each departments will have its own employees.
1 Like

Its the other way around. Its a struct within a struct :slight_smile:

1 Like
  1. Why do we need structs?
    We need structs to group variables of different data types together.

  2. What are members in a struct?
    The variables inside the struct.

  3. How do you access a member inside a struct?
    To acces a member we can use the member selection operator (the period).

  4. What is a nested struct?
    A nested struct is a struct used inside another struct.

1 Like
  1. We need to structs in order to be able to represent an object with more than one variable.
  2. The variables in a struct are called members.
  3. To access a member of a struct we use the member selection operator. struct.member
  4. A nested struct is a struct which is a member inside another struct.
1 Like
  1. So we could store several variables in one place
  2. The contents, variables
  3. . (Frank.wage)
  4. A structs that contain other structs
1 Like

Actually its a struct within a struct :stuck_out_tongue:

1 Like
  1. Why do we need structs?
  • If we need to save a value, we declare a variable. If we need to save several values of one type, we can use arrays. If, however, the values are of different types, we need structs.
  1. What are members in a struct?
  • In a struct, we declare each variable with the type of value it will hold. These variables are called members. The members of Fruit are diameter, weight and color:
    struct Fruit {
    int diameter;
    double weight;
    string color;
    };

Fruit, however, is not the actual struct we will use, it is rather a structure of structs of a type Fruit. To make use of it, we do the following:
Fruit apple;
Fruit orange;

thus declaring structs apple and orange.

  1. How do you access a member inside a struct?
  • To edit the values separately:
    apple.weight = 0.125;
  • To define all values in one statement:
    Fruit apple {98, 0.125, “red”};

If we don’t define a value, it will be a default value of the type (0 for int, 0.0 for double…)
We can define our own default values when declaring the struct:
struct Fruit {
int diameter { 100 };

  1. What is a nested struct?
  • It is a struct inside a struct:
    struct Garden {
    Fruit apple { … }
    Fruit orange { … }
    };
  • To then access the members, we use selection operator twice:
    garden1.apple.weight = 0.125;
  • Or for all at once, double { { } }:
    Garden garden1 {{ 98, 0.125, “red” }};
2 Likes
  1. We need structs to organize different type of data in one structure.
  2. Those are different data types in one group.
  3. First you declare a variable name for the specific struct Exp: structName variableName;. Second you can call the inner members like this: variableName.anyMember;
  4. It is a struct inside a struct.
1 Like
  1. Structs allows us to group variables into a single unit
  2. The variables in the struct
  3. Struct.member
  4. Nested structs are structs contained in other structs eg. Struct1{….}Struct2{Struct1,…}
1 Like

1. Why do we need structs?

“Struct” is short for structure and we need them because they allow us to group variables of mixed data types together into a single unit.

2. What are members in a struct?

Variables that are part of the struct are called members. In the example below, the employee’s ID, age and wage are all members of the struct:

struct Employee

{

short id;

int age;

double wage;

};

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

When we define a variable such as Employee joe, joe refers to the entire struct (which contains the member variables). In order to access the individual members, we use the member selection operator, which is a period.

Example:

Employee joe;

joe.age = 27;

joe.wage = 24.15;

4. What is a nested struct?

This is where structs contain other structs.

Its actually the other way around, its a struct contained in a struct :slight_smile:

2 Likes
  1. because sometimes we need to group variables of mixed data types together into a single unit.
  2. variables that are part of the struct
  3. structVariable.member
  4. a struct in a struct
1 Like

1)To gather a group of variables of mixed data type into a single unit.
2)Variables
3)By using a member selection operator(period). Example: Variable.name;
4) A nested struct is when a struct contains another struct.

1 Like

Actually the other way around. Its a struct within a struct :slight_smile:

1 Like

Why do we need structs?
we need structs to group variables of mixed data types together into a single unit.

What are members in a struct?
The variables that are part of the struct are called members.

How do you access a member inside a struct?
we use the member selection operator (which is a period)

What is a nested struct?
Structs can contain other structs

1 Like

Okay noted :+1: Thanks very much for correcting my mistake :slight_smile:

1 Like
  1. We need a struct to group variables of mixed data types together into a single unit to be able to pass this mixed data to a function.

  2. Members of a struct are variables.

  3. To access a member inside a struct we use the member selection operator - a period. struct.member

  4. A nested struct is a struct located within 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.
  2. What are members in a struct?
    Variables that are part of the struct.
  3. 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).
  4. What is a nested struct?
    A struct that contains another struct or structs.
1 Like