Structs C++ - Reading Assignments

  1. Why do we need structs?

“Structs” are needed to represent more than one variable in order for an object. So, structs allows us to group variables of mixed data types together in a single unit.

  1. What are members in a struct?

Members (fields) are a group of variables.

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

We access a member inside a struct by using a “member selection operator; which is a period, but since struct member variables are not initialized, they must be initialized manually.

  1. What is a nested struct?

A nested struct is a struct within a struct. This means that once you have created one struct, it can be used to an existing struct field.

QUIZ

#include <iostream>

struct Advertising

{

int adsShown;

double clickThroughRatePercentage;

double averageEarningsPerClick;

};

Advertising getAdvertising()

{

Advertising temp;

std::cout << "How many ads were shown today? ";

std::cin >> temp.adsShown;

std::cout << "What percentage of users clicked on the ads? ";

std::cin >> temp.clickThroughRatePercentage;

std::cout << "What was the average earnings per click? ";

std::cin >> temp.averageEarningsPerClick;

return temp;

}

void printAdvertising(Advertising ad)

{

std::cout << "Number of ads shown: " << ad.adsShown << ‘\n’;

std::cout << "Click through rate: " << ad.clickThroughRatePercentage << ‘\n’;

std::cout << “Average earnings per click: $” << ad.averageEarningsPerClick << ‘\n’;

std::cout << "Total Earnings: $" <<

(ad.adsShown * ad.clickThroughRatePercentage / 100 * ad.averageEarningsPerClick) << ‘\n’;

}

int main()

{

Advertising ad = getAdvertising();

printAdvertising(ad);

return 0;

}

QUIZ #2

#include <iostream>

struct Fraction

{

int numerator;

int denominator;

};

Fraction getFraction()

{

Fraction temp;

std::cout << "Enter a value for numerator: ";

std::cin >> temp.numerator;

std::cout << "Enter a value for denominator: ";

std::cin >> temp.denominator;

std::cout << "\n";

return temp;

}

void multiply(Fraction f1, Fraction f2)

{

std::cout << static_cast<float>(f1.numerator * f2.numerator) /

(f1.denominator* f2.denominator);

}

int main()

{

Fraction f1 = getFraction();

Fraction f2 = getFraction();

multiply(f1, f2);

return 0;

}

  1. Why do we need structs?

    Structs are used in instances where more than one variable is needed in order to represent an object. A struct groups variables of mixed data types together into a single unit.

  2. What are members in a struct?

    Members (or fields) are the variables that are part of the struct.

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

    To access a member inside a struct, use the member selection operator (which is a period).
    i.e. struct.member.

  4. What is a nested struct?

    Structs that contain other structs.

I noticed in the section about “Initializing Structs” that I get the same results for the uninitialized struct variables that Ivan got. (name = “”, age= 7274156, gender=""). I also get the same result when I rerun it. This is not the crazy behavior that Ivan promised.

Why do we need structs?
Structs are very useful whenever we need to store multiple data inside a single variable.

What are members in a struct?
Members in a struct are the different variables, or fields, contained inside the struct.

How do you access a member inside a struct?
Struct members can be accessed using a member selection operator (a period): example.name. Before using members, they must first be initialized.

What is a nested struct?
It is a struct within another struct. This is perfectly valid in C++ and a powerful feature.

  1. Why do we need structs?
    For the case of an aggregate data type where we need to group multiple individual variables together. In this case a struct as as such.

  2. What are members in a struct?
    members are variables that are part of the sruct.
    e.g
    using namespace std;
    int main ( )
    {
    struct Employee
    {
    short id;
    int age;
    double wage;
    }
    }

  3. How do you access a member inside a struct?
    We use what is referred to as a ‘member selection operator’

if we declare a variable or multiple variables from the same struct such as;
Employee joe;
Employee alex;
(NB: remember to leave the names i.e. joe and alex beginning with small letters)

using joe and alex as our example;
we access the member by using a dot(.) i.e.
Employee joe; //create an Employee struch for joe
joe.id =14;//assign value to member id within struct joe
joe.age = 32;//assign value … age within joe
joe.wage = 24.15;//assign value … wage within joe

Employee alex;
alex.id = 15
alex.age = 28
alex.wage = 12.27

You can look at code that you may use to see an initialization.
using namespace std;

int main ()
{
struct Employee
{
short id;
int age;
double wage;
};
Employee joe = {1, 32, 60000.0};
Employee frank = {2, 28, 5}; //frank’s wage will be zero

int totalAge = joe.age + frank.age;
std::cout<<"total age "<<totalAge<<endl;

if(joe.wage > frank.wage)
    std::cout<<"Joe makes more than frank\n";
else if ( joe.wage<frank.wage)
    std::cout<<"Joe makes less than frank\n";
else
    std::cout << "joe and frank make the same amount\n";

//frank gets a promotion
frank.wage += 2.50;
std::cout<<"frank's wage "<< frank.wage << endl;
//joe.age;
++joe.age;

}

  1. What is a nested struct
    In this case we now have structs containing other structs.
  1. Structs are needed when we need to store more than one variable to describe an object.
  2. Variable that are part of a struct.
  3. You write the name of the struct and the name of the member separated by a period.
  4. A struct that contains another struct as a member
  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.

  1. What are members in a struct?

Members are variables part of the struct

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

struct.member

  1. What is a nested struct?

a struct within a struct.

  1. Why do we need structs?
    • to store a structured set of variables of mixed data types together into a single object.
  2. What are members in a struct?
    • variables defined in a struct are called members
  3. How do you access a member inside a struct?
    • struct.member
  4. What is a nested struct?
    • a struct that contains a struct as a member
  1. We need structs because it easier and fast to group multiple individual variable types together or to put it simply, structs are used to create a objects.
  2. Members are the variables inside the struct.
  3. To access a member inside a struct we use
    struct.member
  4. A nested struct is when a struct has another struct as a member.
  1. To define a object with multiple properties.
  2. The variables a struct contains are its members.
  3. A member inside a struct can be accessed using . operator.
  4. A struct defined inside other struct.
  1. Why do we need structs?
    "structs" allows us to group variables of mixed data types together into a single unit.
  2. What are members in a struct?
    "Members" are 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?
    When a struct contains another struct, it is called a "nested struct"
  1. Why do we need structs?
    It 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.

  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 can containing other structs.

Hello @ivan and dear Community,


  1. Why do we need structs?

To aggregate native and/or user-defined variables. Thus, the cod will be more overseeable and manageable.

  1. What are members in a struct?

The datafields: native and/or user-defined variables.

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

With the DOT (PERIOD) “.” operator -> e.g.: Struct-variable.member

  1. What is a nested struct?

A “struct”, which is called a “user-defined datatype”, acting as a member within another struct.


Kind regards
Kevin

  1. Why do we need structs?
    To store variables into a more structured way that represent an object.
  2. What are members in a struct?
    The members in a struct is the variables that could be stored in struct
  3. How do you access a member inside a struct?
    the struct name dot the member e.g. struct.member
  4. What is a nested struct?
    When a struct is a member of another struct.
  1. Why do we need structs? These allow us to group variables of varying data types into a unit
  2. What are members in a struct? These are variables that are part of the struct
  3. How do you access a member inside a struct? Write the variable of that struct type, followed by a period (which denotes the member selection operator), followed by the member you want to access.
  4. What is a nested struct? This is a struct inside another struct; Or a struct that contains another struct

Hi Ivan, when trying to access the website “http://www.learncpp.com/cpp-tutorial/47-structs/”, my browsers shows a message saying “Error establishing a database connection”
Do you know other resource(s) to replace this one?
Thanks,

  1. Why do we need structs?
  • structs are equivalent to objects. We use structs when we have variables with multiple attributes
  1. What are members in a struct?
  • members are the attributes or sub-variables of a struct
  1. How do you access a member inside a struct?
  • struct . member (with the dot)
  1. What is a nested struct?
  • A struct inside another struct
  1. Why do we need structs?
    To easily group together variables of mixed data types into a single unit.

  2. What are members in a struct?
    Variables inside of a struct

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

  4. What is a nested struct?
    When a struct contains another struct as an member

Why do we need structs?

We need structs to declare several members that is part of one variable.

What are members in a struct?

A member is a part of the struct variable for example ”struct employee” have wage as a member,

How do you access a member inside a struct?

You can access the member by declaring the struct and use ”.” afterwards.

What is a nested struct?

Its a struct that is within another struct.

  1. Why do we need structs?
    To store multiple variable in a single data type.

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

  3. How do you access a member inside a struct?
    Using the fullstop operator: structName.member;

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