Structs C++ - Reading Assignments


#81
  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;

}


#82
  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.


#83

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.


#84

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.


#85
  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.

#86
  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