Structs C++ - Reading Assignments

While you are correct about what structs are, we are talking about C++ here :slight_smile: Javascript doesn’t have structs in the same regard as C++

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 are called members (or fields).

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

  4. What is a nested struct?
    Structs that contain other structs.

1 Like
  • Why do we need structs?

Structs are needed to cluster variable together into a single super variable (Structured aggregate data: struct).

  • What are members in a struct?

I’m trying to relate this to something I know. Let’s say the Struct: ‘Sugar’. The members of this struct would be name, carbon, hydrogen, and oxygen. They are mostly sweet, but they are passed different values. SUGAR.glucose has member values C=6, H=12 and O=6. SUGAR.fructose has member values C=6, H =12 and O=6. Have too close a relationship with SUGAR.fructose with make you fat, giver you diabetes and rot your teeth!

  • How do you access a member inside a struct?

the members of the struct can be accessed by invoking the struct member as an expression with the period operator.

  • What is a nested struct?

This is when a struct elevates itself to ‘super’ struct form my taking on other structs as members (these structs would be members relative to the caller ‘super’ struct).

[/quote]

1 Like
  1. Why do we need structs?
    Structs, or structures, are a way for a programmer to create a unique variable type that contains multiple values of different types (which is similar to an array - except that an array only allows multiple values of single variable type).

  2. What are members in a struct?
    The members in a struct are the different variables defined within the struct.
    For example:

struct Person
{
   string name;
   int age;
   double height;
   double weight;
};

In the above example, the variables name, age and height (which are all different types of values) are members that are defined within ‘struct Person’.

  1. How do you access a member inside a struct?
    You can access a member by using the member selection operator, which is a period. Once you have defined the struct and then defined a variable of the struct type, for example:
Person eggbert{"Eggbert", 122, 160.5, 96.8};

You can then access members as follows:

cout<<eggbert.name<<" is a very old man, having been alive for "<<eggbert.age<<endl;
cout<<eggbert.name<<" is also a rather short and heavyset individual, coming in at "<<eggbert.height<<" centimetres tall and "<<eggbert.weight<<" kilograms"<<endl;
  1. What is a nested struct?
    A nested struct is exactly like a struct as defined in question 1, however another struct can use it as one of its members and therefore make it a nested struct. That is, a struct can have all kind of variable types (such as int, double, string, etc), one of which includes a struct (as a struct is just a user defined variable type).
    So, using our struct from before, we could have the following struct:
struct BookClub
{
   Person bookClubMember;
   int months;
   string favBook;
}

This example shows a struct called BookClub that includes:
bookClubMember - which is of the struct variable type (and can therefore access any values, from that struct, that have been initialised),
months - which lists the number of months the bookClubMember has been a part of the group and is of the int variable type, and
favBook - which lists the favourite book of the bookClubMember and is of the string variable type.

Hopefully I have this right, if not - I think my overly detailed explanations will show exactly where I have gone wrong, I think I am understanding structs correctly though and they seem like a slightly complex but very handy tool in a programmers toolbox.

1 Like

You got it right! :smiley: keep up the good work!

2 Likes
  1. Why do we need structs?
    Structs allow to store multiple variables accessible through one identifier.

  2. What are members in a struct?
    The members are the individual variables belonging to the struct.

  3. How do you access a member inside a struct?
    By using a period: struct.members

  4. What is a nested struct?
    A nested struct is a struct which is defined within another struct. Consequently the inner struct is a member of the outer struct.

1 Like
  1. Why do we need structs?

We need structs when more than one variable is needed to represent an object. A struct is an object or an aggregate data type that has properties or members. For example, a person is the object and name, address, and phone number are the properties of that object.*

  1. What are members in a struct? The member are the attributes or properties of the struct. An address struct can have street number, street name, city and zip as its members.
  2. How do you access a member inside a struct? *Use the member selector operator which is a (.). For example if Mike is an instance of the student struct then Mike.studentid is used to access the member value, studentid.
  3. What is a nested struct? A nested struct is a struct that contains another struct.
1 Like

A nested struct is a struct within a struct :slight_smile:

1 Like
  1. Why do we need structs?
    A struct allows the programmer to aggregate(group together) multiple individual variables of mixed data types together into one unit.

  2. What are members in a struct?
    The multiple individual variables in a struct are know as members.

  3. How do you access a member inside a struct?
    After the strut variable is defined, to access individual members use member selection operator. This operator uses the strut name with a period symbol to indicate that it’s a operator.

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

1 Like
  1. A struct allows us to group variables of mixed data types together into a single unit.
  2. All the variables that are part of it.
  3. we use the member selection operator which is a period.
  4. This is when a struct has another struct like member
1 Like

1. Why do we need structs?
To have more than one variable in order to represent an object
2. What are members in a struct?
The variables inside a struct
3. How do you access a member inside a struct?
Use the member selection operator which is a period
4. What is a nested struct?
A struct within a struct

1 Like
  1. Why do we need structs?
    Structs make it possible to tie together several different variables regardless of type and saves a lot of time. It is a starting point for object-oriented programming.

  2. What are members in a struct?
    These are variables declared within a struct which can be accessed with the struct identifier.

  3. How do you access a member inside a struct?
    You use the struct identifier and the member selector “.”

  4. What is a nested struct?
    This is when you use a struct as variable inside another struct.

1 Like
  1. Structs allow to store mixed data types together into a single unit custom type
  2. Members ore fields are the variables that form a structure
  3. First declare a structure, for example Employee. Then to access a member inside, let’s say “name”, just type Employee.name , assuming “name” exists
  4. A nested struct are structs that contain other structs
1 Like

Its actually the other way around. It is a struct contained inside another struct :slight_smile:

3 Likes
  1. Why do we need structs?
    Structs allows us to create, store and access an organized group of different variables.

  2. What are members in a struct?
    Members are the variables declared and held within a struct.

  3. How do you access a member inside a struct?
    By using the member operator.
    example:
    Struct.member = x;

  4. What is a nested struct?
    A nested struct is a struct within a struct. The example given in the reading was that of a struct Company that contains as a member a struct for employees.

1 Like
  1. Why do we need structs?

In order to represent multiple individual variables together.

  1. What are members in a struct?

Members are the variables that form the struct.

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

Using the member selection operator, which is a period. structIdentifier.member

  1. What is a nested struct?

These are structs that contain other structs.

2 Likes

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

3 Likes
  1. Why do we need structs? In order to group multiple individual data variables together.
  2. What are members in a struct? Individual variables within the struct.
  3. How do you access a member inside a struct? Once a variable is defined that refers to the entire struct, individual members are accessed by the member selection operator, which is a period. i.e. joe.id or joe.age, etc.
  4. What is a nested struct? A struct contained inside another struct.
2 Likes
  • To represent different variables we can store inside one struct. Different from array since structs behave like objects.
  • Variables
  • Struct.member
  • Struct inside another struct
1 Like

We need structs to be able to group and access different types of variables from an object. By using structs, we can get a function to return multiple variables.

The variables inside the struct are called members. They can be any types od variables.

By using dot notation.
eg.:
struct Wehicles
{
short numWheels;
short numDoors;
int capacity;
}
Wehicles car {4, 5, 5};

To access a member (numDoors), we use car.numDoors

A struct can hold other structs in them too, these are called nested structs. To access a value of a variable that is in a nested struct, we have to use dot notation twice.

1 Like