Structs C++ - Reading Assignments

1)Why do we need structs?
To represent an object with more than one variable.

2)What are members in a struct?
The variables that comprise a struct

3)How do you access a member inside a struct?
stuct_instance_name.member = value

4)What is a nested struct?
A structure that contains other structures

1 Like

Answer:

    1. We need struct to store multiple variables easily in one object. It makes it easier to pass variables to functions.
    1. Members are variable names stored inside of the struct.
    1. We use a member selection operator which is period. To access the member, we write struct name, period, and member name.
    1. A nested struct is a type of struct that exists inside of another struct.
2 Likes

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? The variables that are part of the struct are called members.

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? Structs inside other structs are called nested structs.

1 Like
  1. It allows us to group variable of mixed data types together
  2. Members are the variables that are inside the struct.
  3. struct.member
  4. a struct within another struct
1 Like
  1. To group multiple attributes together to make the programming easier (as opposed calling each attribute separately every time)
  2. Variables
  3. struct.member
  4. A struct within a struct
1 Like
1. Why do we need structs?

To provide a container for related data. This grouping makes it easier to pass this common data around and provides a useful abstraction.

2. What are members in a struct?
  • Named variables that define the “shape” (or structure) of the struct. In javascript these are called properties.
  • While functions can be members of a struct, it is generally preferable to keep structs as simple dumb objects with data only- sometimes referred to as Data Transfer Objects (DTOs) or POCOs (Plain Old C++/C#/etc Object).

Initialization

  • Just like variables struct members are not initialized so using them without a known value can result in “undefined behaviour”
  • Members can be given a default value in the struct definition (this must be new since I last learned about c++)
  • Can also use an initializer list (curly brackets) which will assign values according to the order in the struct definition :sunglasses:
3. How do you access a member inside a struct?

With the dot operator. I.e person.name.

4. What is a nested struct?

A struct that contains another struct. I.e

struct Person {
string name;
int age;
}

struct Family {
string address;
Person parents[2];
Person children[10];
}

Note it’s also possible for a struct to contain a self reference, but only using a pointer (something not yet discussed). This is useful for implementing tree structures with parent and child nodes.

Quiz

1. Advertising struct

Notes

  • The printAdRevenue method would be better served as a method in an AdRevenue class that overrides ToString() to provide native support for converting the object into a string representation
  • Pretty much all the functionality here would be moved into a class
#include <iostream>

using namespace std;

struct AdRevenue {
    string date; // yyyy-mm-dd
    int numAddsShown;
    float clickPercentage;
    double revenuePerClick;
};

void printAddRevenue(AdRevenue r) {
    cout << "\nDaily Add Revenue " << r.date << "\n"
         << "Adds Shown: " << r.numAddsShown << "\n"
         << "Click Through %: " << r.clickPercentage << "\n"
         << "Revenue/click: " << r.revenuePerClick << "\n"
         << "Daily Total Revenue: $"
         << r.numAddsShown * r.clickPercentage * r.revenuePerClick << "\n";
}

AdRevenue getAddRevenueFromUser() {
    AdRevenue r {"2020-01-01", 0, 0.0, 0};

    cout << "Enter Daily Add Revenue\n\n"
         << "Date (YYYY-MM-DD): ";
    cin >> r.date;

    cout << "Adds Shown: ";
    cin >> r.numAddsShown;

    cout << "Click Through %: ";
    cin >> r.clickPercentage;
    r.clickPercentage /= 100;

    cout << "Revenue/click: ";
    cin >> r.revenuePerClick;

    return r;
}

int main()
{
    AdRevenue r = getAddRevenueFromUser();
    printAddRevenue(r);
    return 0;
}
2. Fraction struct

Notes:

  • Apparently the runtime does not throw an error when you divide by zero!
  • Again printing and multiplying fractions should be class members. If I remember correctly you can even override the * operator to provide native support for multiplication.
#include <iostream>

using namespace std;

struct Fraction {
    int numerator;
    int denominator;
};

Fraction getFractionFromUser(string message) {
    Fraction f {0, 1};
    cout << message;
    cout << "Enter numerator: ";
    cin >> f.numerator;

    cout << "Enter denominator: ";
    cin >> f.denominator;

    return f;
}

string printFraction(Fraction f) {
    return to_string(f.numerator) + "/" + to_string(f.denominator);
}

double multiply(Fraction a, Fraction b) {
    return (1.0 * a.numerator * b.numerator) / (1.0 * a.denominator * b.denominator);
}

int main()
{
    Fraction a = getFractionFromUser("\nEnter first fraction\n");
    Fraction b = getFractionFromUser("\nEnter second fraction\n");

    cout << "\n" << printFraction(a) << " * " << printFraction(b)
         << " = " << multiply(a, b);

    return 0;
}
1 Like
  1. Why do we need structs?
    Because in order to represent an object we need more variables.
  2. What are members in a struct?
    The variables inside a structure are called members.
  3. How do you access a member inside a struct?
    To acces a member inside a structure we use the selection operator which is a period.
    E.g. struct Employee {
    int age;
    double wage;
    };
    Employee John {24, 1900};
    John.age; // acces John’s age
    John.wage; // acces John’s wage
  4. What is a nested struct?
    A nested struct contain another struct inside or many.
1 Like

Why do we need structs?
To group variables of mixed data types together into a single unit. So that you don’t need to make a big amount of variables for example person data storage.
What are members in a struct?
The variables you store inside of a struct.
How do you access a member inside a struct?
struct.member;
What is a nested struct?
A struct declared inside of another struct. For example an employee from a struct inside of a company struct.

1 Like
  1. Structs eliminate the need for many variables and can assign many values to one variable.

  2. Members are child values assigned to each struct.

  3. NameOfStruct.member

  4. A struct within an individual struct or member.

1 Like
  1. Why do we need structs?
    Far better way to organize logical groups of variables like an object.

  2. What are members in a struct?
    variables to hold values within the struct

  3. How do you access a member inside a struct?
    reference a member selection operator using struct.member

  4. What is a nested struct?
    struct within a struct as a member

1 Like
  1. Why do we need structs?
    Structs/structures are needed because most real things are need more than one variable to describe them. Structure makes it much easier to group these variables together for ease of use.

  2. What are members in a struct?
    These are the variables in the struct that can represent mixed data types.

  3. How do you access a member inside a struct?
    By using the member selection operator, which is a period in the form StructName.memberName

  4. What is a nested struct?
    A struct that contains other structs.

1 Like
  1. Why do we need structs?
    To be able to group multiple variables (of, maybe, different types) together.

  2. What are members in a struct?
    The variables that are part of it.

  3. How do you access a member inside a struct?
    Using the member selector . operator.

  4. What is a nested struct?
    A struct that contains another struct.

1 Like
  1. Structs are needed because they allow us to “group variables of mixed data types together into a single unit”.

  2. Members are the individuals variable inside of a struct.

  3. To access a member inside of a struct you must first have a variable defined for your struct. Once that is done you can use the variable name followed by a period, then the individual struct name. After that you can assign a value to the variable through the use of the equals sign followed by the desired assignment.

  4. A nested struct is when a struct contains other structs.

1 Like
  1. Why do we need structs?
    To represent an object by a set of variables.

  2. What are members in a struct?
    Members are the fields of the struct.

  3. How do you access a member inside a struct?
    Using the member selection operator.
    struct.member

  4. What is a nested struct?
    A field within a struct that itself is a struct.

1 Like

1. Why do we need structs?
If we want to store an aggregated set of data into one object

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

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

4. What is a nested struct?
A struct that is a member inside another struct.
A struct within a struct. Just like inception…

1 Like

1 - Why do we need structs?
Structs allow us to group multiple data types together in a single unit.

2 - What are members in a struct?
The variables that are present in the struct are called members.

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

4 - What is a nested struct?
A nested struct is a struct inside another struct. We could access data this way:

MajorStruct.MinorStruct.member

Quiz 1 solution:

#include <iostream>
using namespace std;

struct Advertising {
    int numberOfAdsShown;
    float percentageClicked;
    float earningPerClick;
};


float findTotal(Advertising result){
    float total = ((result.numberOfAdsShown * result.percentageClicked)*result.earningPerClick);
    return total;
};


int main() {

    Advertising today;

    cout << "How many ads shown today?";
    cin >> today.numberOfAdsShown;
    cout << endl;

    cout << "What percent were clicked?";
    cin >> today.percentageClicked;
    cout << endl;

    cout << "How much did you earn per click?";
    cin >> today.earningPerClick;
    cout << endl;

    cout << "The result was " << findTotal(today);

    return 0;
}

1 Like

Solution for quiz part 2, I needed to look up help.

#include <iostream>
using namespace std;

struct Fraction {
    int numerator;
    int denominator;
};

void multiply(Fraction One, Fraction Two){
   cout << static_cast<double>(One.numerator * Two.numerator) /
                 (One.denominator * Two.denominator) << '\n';
}


int main() {
    Fraction theFraction;

   cout << "Enter the numerator ";
   cin >> theFraction.numerator;
   cout << "Enter the denominator ";
   cin >> theFraction.denominator;

    Fraction theOtherFraction;

    cout << "Enter numerator for the second fraction ";
    cin >> theOtherFraction.numerator;
    cout << "Enter the denominator for the second fraction";
    cin >> theOtherFraction.denominator;

    multiply(theFraction, theOtherFraction);
    return 0;
}

1 Like
  1. You can store multiple data together in a struct instead of having
    multiple different variables. More effective when implementing in a function for example

  2. The different variables in the struct

  3. By writing the name of the type of the variable, period button and the variable (jon.age ex)

  4. When you have the name of the variable inside another struct (within the struct)

1 Like
  1. Structs allow us to group variables into a single object.
  2. Members in a struct refer to the variables being grouped.
  3. A member can be accessed by using “.” operator.
  4. A nested struct is a member which is also a struct.
1 Like
  1. We use structs in cases when we need more than one variable in order to represent an object. For example, it can be in cases when I want to have a list of employees and ascribe to them their age, salary, years of experience etc. Structs are the simplest form of these aggregate data types.
  2. Members in a struct are variables which are a part of the struct and provide values. Just like in the aforementioned example of struct with employees and their members such as age, salary, years of experience.
  3. We use the member selection operator (simply a period). For example,
struct Employee
{
    short id;
    int age;
    double wage;
};

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
  1. Nested structs are contained within other structs.
struct Employee
{
    short id;
    int age;
    double wage;
};
 
struct Company
{
    Employee CEO; // Employee is a struct within the Company struct
    int numberOfEmployees;
};
 
Company myCompany;
1 Like