Structs C++ - Reading Assignments

1. Why do we need structs?

There are many instances in programming where we need more than one variable in order to represent an object. For example, to represent yourself, you might want to store your name, your birthday, your height, your weight, or any other number of characteristics about yourself.

2. What are members in a struct?

Variables within the struct.

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

After declaring a variable with the type of an existing struct, reference members within the struct using the name of the declared variable followed by a period followed by the struct member name.

4. What is a nested struct?

A struct referenced within the definition of another struct.

1 Like
  1. Why do we need structs? To group individual but associated data together into an easier to work with form of object, or structure.

  2. What are members in a struct? Members are the individual instances of the struct type. ie. A ‘cake’ structure may have the member ‘flourGrams’ declared as an int.

  3. How do you access a member inside a struct? The above for example can be referenced like this: cake.flourGrams = 125.

  4. What is a nested struct? When structure is placed as a member within a second structure.

QUIZ: Advertising:

struct Advertising
    {
        int adsTotal;
        int clickedPercentage;
        double earnedFromEach;
    };

    Advertising user{}; // initialise instance with 0 values.

    void displayAndCalc(Advertising userData){
    double total = 0.00;

    cout << "Total Ads: " << userData.adsTotal << endl;
    cout << "Percentage clicked: " << userData.clickedPercentage << endl;
    cout << "Each earned: " << char(156) << userData.earnedFromEach << endl;
    total = ((userData.adsTotal / 100) * userData.clickedPercentage) * userData.earnedFromEach;
    cout << "\n" << "Your total earnings are: " << char(156) << total << endl;
    }

int main()
{
    cout << "Please enter how many adverts were shown: ";
    cin >> user.adsTotal;
    cout << "Please enter the clicked percentage: ";
    cin >> user.clickedPercentage;
    cout << "Please enter amount earned from each clicked advert: " << char(156);
    cin >> user.earnedFromEach;
    cout << endl;


    displayAndCalc(user);
}

And MULTIPLY FRACTIONS:

struct Fraction
    {
        int numerator;
        int denominator;
    };

    Fraction numerator{}, denominator{}; // initialise instance with 0 values.

    int getNumerator{};
    int getDenominator{};

    void multiply(Fraction fractionA, Fraction fractionB){
        cout << "\nfraction A (" << fractionA.numerator << "/" << fractionA.denominator << ") X ";
        cout << "fraction B (" << fractionB.numerator << "/" << fractionA.denominator << ") = ";
        // (double) conversion needed before the member int's, to allow division.
        double tempA = (double)fractionA.numerator / (double)fractionA.denominator;
        double tempB = (double)fractionB.numerator / (double)fractionB.denominator;

        double total = (tempA * tempB);
        cout << total << endl;
    }

int main()
{
    cout << "Please enter the first numerator: ";
    cin >> getNumerator;
    cout << "Please enter the first nominator: ";
    cin >> getDenominator;
    Fraction fractOne{getNumerator, getDenominator};

    cout << "\n" << "Please enter the second numerator: ";
    cin >> getNumerator;
    cout << "Please enter the second nominator: ";
    cin >> getDenominator;
    Fraction fractTwo{getNumerator, getDenominator};

    multiply(fractOne,fractTwo);

    return 0;
}
1 Like
  1. To group multiple variables of mixed data types together in a single unit.
  2. Variables defined inside a struct.
  3. By using the member selection operator (a period).
  4. When a struct is used as a member of another struct.
1 Like
  1. Why do we need structs?
  • Structs, short for structure, are needed so we can group variables of mixed data types together into a single unit.
  1. What are members in a struct?
  • Members in a struct are variable that are declared within the struct but are not initialized
  1. How do you access a member inside a struct?
  • To access a member inside a struct, you will use the member selection operator. The member selection operator is just a period. Use the struct name, the member selection operator, and the member inside the struct (ex: chanel.age)
  1. What is a nested struct?
  • A nested struct is when one struct is used as a variable within another struct
1 Like

Why do we need structs?
It is so we can represent an object which consists of more than one variable

What are members in a struct?
They are the variables (can be different data types) which when taken as a whole represent the complete structure of an object.

How do you access a member inside a struct?
We use the member selection operator which is a period eg myStructure.name = “Simon”;

What is a nested struct?
It is a structure in which one of its members is another structure.

1 Like

Answers

  1. We need structs in order to aggregate data types into a single unit.

  2. Members are the variables that are part of a struct.

  3. We can use the member selection operator (struct.member).

  4. When a struct is defined into another struct.

1 Like
  1. Structs is the simplest of the aggregate data types. Aggregate data types group multiple individual variables together

  2. The variables that are part of the struct are called members. Members are the group of variables that make up the struct.

  3. Members can be accessed inside a struct by using the member selector operator. For example:
    StructName.memberName

  4. A nested struct is a struct inside a struct. Like functions they are declared outside the scope, but are then invoked inside the scope of the struct.

1 Like
  1. To represent objects with several variables.

  2. Variables that are parts of a struct.

  3. By using a dot, which is the member selection operator.
    For instance:

Person john;
john.age = 20;

In this example, john is the struct and age is the member.

  1. It is a struct that contains other structs.
1 Like
  1. They allow us to store more than one variable in order to represent an object.
  2. Variables that are part of a struct.
  3. In order to access individual members, we use the member selection operator (which is a period). Eg struct.member
  4. A struct that contains another struct.
1 Like
  1. We need structs because to simplify and organize groups of variables that have the same properties.
  2. The members are the variables in the struct.
  3. Struct.member ( i think is like Javascript).
  4. A nested struct is a struct inside a bigger struct.
1 Like
  1. Why do we need structs?

We use structs, an aggregate data type, when we need more than one variable in order to represent an object. With structs we can group variables of mixed data types together into a single unit.

  1. What are members in a struct?

Members are variables that are part of the struct.

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

In order to access the individual members, we use the member selection operator (period).
exampleStructVariable.memberX

  1. What is a nested struct?

Nested struct is the struct contained inside another struct.

1 Like
  1. Why do we need structs?
    To group several variables of different type into a single unit.

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

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

  4. What is a nested struct?
    It is a structure that is a member of another structure.

1 Like
  1. Why do we need structs?
    Because we can use more than one variable to represent an object.
  2. What are members in a struct?
    The variables in the struct
  3. How do you access a member inside a struct?
    By using the member selection operator which is basically a period.
    referenced_strunct.variable_of_referenced_strunct
  4. What is a nested struct?
    A struct within a struct .
1 Like

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

What is a nested struct?
Structs inside other structs

1 Like
  1. Because without it we would have to declare too many variables. Structs allow us to group variables of mixed data types together into a single unit.
  2. Members in the struct are the variables that are part of it.
  3. By typing the struct name with a dot and member name for example:
    tomcio.name
  4. It is a struct that contains another struct inside it. For example:
              struct structure1
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
              };

              struct structure2
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
                     struct structure1 obj;
              };
1 Like
  1. Structs are needed when you want describe something which consists of multiple data/information/values AND these values are also from different data type. When you have multiple data/values but only one from one data type you can use an array. In Short, when you have an “object” which is described through several data types (int, char, etc.) a struct is needed. It aggregates multiple data from multiple data types in just one variable.

  2. Inside your defined struct you have to declare and define some attributes which your struct object should have. These attributes (e. g. when you think of a person, it could be described through age, name and so on and so forth) are called members (or fields).

  3. First, you have to write the name of your struct object followed by a dot (.) and then you have to write the desired member (attribute). Lets take the example from answer 2 (person). My created Person struct is called person1 and I want to get access to the value from the field age:

person1.age
  1. When you declare and define a struct object as a member (field) inside another struct, then you have a nested struct.
1 Like
  1. Structs allow to gather variables of different data types in a single unit, to make the access to such data easier and faster.
  2. All the variables which are part of the struct itself.
  3. Using a period as a member selection operator to choose which member of which struct we are going to refer to.
  4. A struct containing one or more other strucks, which have their own members and structures.
1 Like
  1. We need structs when we need more than one variable in order to represent an object.

  2. Members in a struct are the variables that form part of the struct.

  3. You access a member inside a struct by using the member selection operator (a period).

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

1 Like

Why do we need structs?
Structs allow us to group together different variables in one object.

What are members in a struct?
Members are the variables that form and are stored in the struct

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

What is a nested struct?
This is when there is a struct inside another struct. This acts like a subgroup of the main group within the object.

1 Like
  1. For whenever we need to group variables of mixed data types together, like an object for javascript.

  2. Members are the variables in the structs.

  3. Struct.member

  4. A struct within another struct.

1 Like