Structs C++ - Reading Assignments

Reading Assignment: Structs C++

1. Why do we need structs?

When you need to keep a record for a lot of details.

2. What are members in a struct?

There are different items/variables that’s belongs to struct

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

By creating a functions, and call the function with one of the members name
Or via direct call
std::cout << "Wage: " << employee.wage << ‘\n’;

void printInformation(Employee employee)

{

std::cout << "ID: " << employee.id << ‘\n’;

std::cout << "Age: " << employee.age << ‘\n’;

std::cout << "Wage: " << employee.wage << ‘\n’;

}

printInformation(frank);

4. What is a nested struct?

It’s a struct that contains other structs

1 Like

Actually its a struct within a struct :stuck_out_tongue:

1 Like
  1. To group a set of Variables inside of the likes of a Name. It can tag all the info about the tag in one Function.

  2. You have a name of a struct and inside that struct is different members which are variables tied within that Struct.

  3. The Member Selection Operator

  4. Structs that contain other Structs within it.

1 Like

Its the other way around, like nested functions (though these are not allowed in C++) :wink:

2 Likes
  1. We need structs to aggregate variables of mixed data types that describe an object.
  2. Members in a struct are the variables that are part of it.
  3. To access members in a struct, we use the member selection operator – a period(.)
  4. A nested struct is a struct within a struct.

Quiz Q#1:

struct Advertising {

    int adsShown;
    short clicksPercentage;
    double avgEarningsPerClick;

};

Advertising readTodayAdvertising() {

    Advertising a;
    cout << "\nEnter number of ads shown today: ";
    cin >> a.adsShown;
    cout << "\nEnter percentage of ads cicked by users today: ";
    cin >> a.clicksPercentage;
    cout << "\nEnter average earnings per click today: ";
    cin >> a.avgEarningsPerClick;

    return a;
}

void printTodayAdvertisingStats(Advertising a) {

    cout << "\nThe number of ads shown today: " << a.adsShown;
    cout << "\nThe percentage of ads cicked by users today: " << a.clicksPercentage;
    cout << "\nThe average earnings per click today: " << a.avgEarningsPerClick;

}

void totalEarningsToday(Advertising a) {


    double totalEarnings = a.adsShown * a.clicksPercentage * a.avgEarningsPerClick;
    cout << "\nTotal earnings today: " << totalEarnings << endl;

}

int main()
{

    Advertising todaysAds{ readTodayAdvertising() };
    printTodayAdvertisingStats(todaysAds);
    totalEarningsToday(todaysAds);

    return 0;
}

Quiz Q#2:

struct Fraction {

    int numerator;
    int denominator;

};

Fraction readFraction(short fNum) {

    Fraction f;
    cout << "\nEnter numerator for fraction#" << fNum << ": ";
    cin >> f.numerator;
    cout << "\nEnter denominator for fraction#" << fNum << ": ";
    cin >> f.denominator;
    if (f.denominator == 0) {
        cout << "\nFraction denominator can't be 0! \nEnter different denominator for fraction#" << fNum << ": ";
        cin >> f.denominator;
    }

    return f;
}

void multiply(Fraction fr1, Fraction fr2) {

    double multiplyResult = static_cast<double>(fr1.numerator * fr2.numerator) / (fr1.denominator * fr2.denominator);
    cout << "\nThe two fractions multiplied is: " << multiplyResult << endl;

}

int main()
{

    Fraction fraction1{ readFraction(1) };
    Fraction fraction2{ readFraction(2) };
    multiply(fraction1, fraction2);

    return 0;
}
1 Like
  1. Structs are aggregate data types, which basically means they allow us to “aggregate” and treat more primitive type in a single structure allowing for way more efficient use in such cases.

  2. members (or fields) are the variables defined inside the struct.

  3. you use the member selection operator (that is the . ) after the struct name, followed by the field name

  4. It’s a struct which has a struct declared inside of it as a member

1 Like

Nope, its a struct within a struct :stuck_out_tongue:

1 Like
  1. Why do we need structs?

So we can group multiple individual variables together into a single unit

  1. What are members in a struct?

Variables that make up a struct

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

Using the member selection operator, which is a period.

  1. What is a nested struct?

Where structs contains other structs

1 Like

1. Why do we need structs?
When we need to store variables that represent an object with caracteristics. chained list and trees use it as node. The blockchain is somewhere a list of struct.

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

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

4. What is a nested struct?
This is when a struct has another struct like member. a struct of individual that includes a struct called parent. In this case, the variable parent is a individual struct. This way, you build genealogical tree. The blockchain is a nested struct in a way.

1 Like

Its actually a struct within a struct. In case of a block, a nested struct would be the block header. :slight_smile:

1 Like
  1. Why do we need structs?
    – We need structs (short for structure) to group related variables of various data types together into a single container

  2. What are members in a struct?
    – The variables that are part of the struct are called members or fields

  3. How do you access a member inside a struct?
    – A struct variable refers to the entire struct (which contains the member variables) and 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 defined within another struct is a nested struct

1 Like

thanks for the clarification Alko89 :slight_smile:

1.) Structs allow us to build single units that can contain multiple variables in different types of data.
2. Members are fields within a structure.
3.) You use a period (member selection operator) to access a member inside of a structure.
4.) A nested structure is a structure build within a structure.

1 Like

We need structs to store different data types.

Mebers are variables in the struct.

Using a member selection operator.
structName.memberName

Struct within a struct.

1 Like
  1. ***Why do we need structs?
    We need them to store variables that represent the objects in the code language.
  2. ***What are members in a struct?
    They are variables (Values) in the struct
  3. ***How do you access a member inside a struct?
    by typing struct.member
  4. ***What is a nested struct?
    It’s when a struct has another structure to it.
1 Like

Not sure what you mean. It stores different variables that make sense to group together by a person writing the struct. :slight_smile:

For example:

struct product {
  int weight;
  double price;
} ;
1 Like
  1. struct (short for structure) allows us to group variables of mixed data types together into a single unit.
  2. Members are variables part of the struct
  3. Struct.member
  4. A nested struct is a struct inside another struct.
1 Like

1. Why do we need structs?

they allow us to group mixed data types into a single unit

2. What are members in a struct?

different variables that are contained within the struct

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

struct.member

4. What is a nested struct?

a struct that is contained within another struct

1 Like

1.To simplify our code, and group variables into a single unit.
2. Variables inside a struct.
3. We use the member selection operator which is a period.
4. Structs inside other structs.

1 Like
  1. Why do we need structs?
    It is important if you have objects with many variables as a single variable.

  2. What are members in a struct?
    They are the variables

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

  4. What is a nested struct?
    you can have a struct inside the other struct

1 Like