Structs C++ - Reading Assignments

Welcome to the discussion about the reading assignment about Structs in C++.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

  1. Why do we need structs?
  2. What are members in a struct?
  3. How do you access a member inside a struct?
  4. What is a nested struct?
15 Likes

1. Why do we need structs?
When we need to store variables that represent an object.

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

4. What is a nested struct?
This is when a struct has another struct like member

4 Likes
  1. Because of logistic reasons: we want to structure/group variables together, so that we can deal with only one group istead of many individual items. Almost like .zip-files.
  2. Members or fields of a struct are the variables that are part of the struct
  3. structName.memberName
  4. A nested struct is a struct inside another struct. If you create a struct to list all the stuff you have in your house, you may group all the kitchen stuff into a kitchen struct. So you have a kitchen struct inside your house struct.
1 Like

Is there any significance to using

static_cast<float>(f1.numerator * f2.numerator)

vs.

 (float)(f1.numerator * f2.numerator)

?

Yes, static_cast is a bit stricter and offers compile-time checks for invalid casting. Check this for more info: https://stackoverflow.com/questions/103512/why-use-static-castintx-instead-of-intx

1 Like
  1. Why do we need structs?
    To describe objects with multiple atributes all bundled together within a single variable.
  2. What are members in a struct?
    Variables
  3. How do you access a member inside a struct?
    struct.member
  4. What is a nested struct?
    A structure defined within a structure
1 Like
  1. Structs are used to group values together and generalize their properties.
  2. Members are the individual values of a struct.
  3. Struct.member
  4. When a struct has another struct as a member.
  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.

  • Why do we need structs?
    To stream line coding (calling to functions) by allowing multiple variables to be assigned to a custom data type.
  • What are members in a struct?
    Each individual variable.
  • How do you access a member inside a struct?
    using a member selection operator. Example Struct.member
  • What is a nested struct?
    A struct that used inside a class.

1- We need structs for storing customized data types
2- Members of a struct are the fields in which the struct are divided
3- You can access a member of a struct typing its name preceded by a point
4- A nested struct is a struct inside other struct

Why do we need structs?
- Structs allow us to group variable data types together as one element.

What are members in a struct?
- The variables that are part of the struct are called members.

How do you access a member inside a struct?
- struct.member;

What is a nested struct?
- A struct within a struct.

1. To group variables of mixed data types togheter into a single unit.

2. Variables that are part of the of the struct.

3. struct.member

4. A struct inside another struct.

Why do we need structs?

This is a good way to group several variables together (even different types unlike arrays!).

What are members in a struct?

A member in a struct is a variable that is contained within a struct. For instance in the container struct, we can see it can contain coffee, juice or water, depending on the boolean value that gets assigned to it.

struct Container {
bool coffee;
bool juice;
bool water;
};

How do you access a member inside a struct?

Using the example above, we can access a member using dot notation like so;

Container mug = { true, false, false};
cout << "Does this mug have coffee in it? " << mug.coffee << endl;

(comes out as 1 instead of true but you get the idea haha)

What is a nested struct?

A nested struct is a struct within a struct.

  1. Why do we need structs?
  2. In C++, structs (short for structure) is the simplest of the aggregate data types. Aggregate data types group multiple individual variables together. This is similar to objects in JavaScript, allowing you give a better representation of an object by describing it with multiple variables instead of a single one. By convention, struct names start with a capital letter to distinguish them from variable names.

  3. What are members in a struct?
  4. The variables that are part of the struct are called members. Members are the group of variables that make up the struct.

  5. How do you access a member inside a struct?
  6. Members can be accessed inside a struct by using the member selector operator. For example:

    StructName.memberName
    

  7. What is a nested struct?
  8. 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 be able to connect several variables to one object, so we dont have to store information which longs together in many different variables, resulting in confusing code.
  2. variables that are part of the struct.
  3. through the dot operator: objectName.member
  4. a nested struct is a struct within another struct. You can think of it like a kind of subcategory.
1 Like
  1. We need them to store several collective variables about an object

  2. Members are variables

  3. structTitle.member

  4. A struct with a struct like member.

1- There are many instances in programming where we need more than one variable in order to represent an object. A struct allows us to group variables of mixed data types together into a single unit.
2- variables that are part of the struct are called members (or fields).
3- In order to access the individual members, we use the member selection operator.for example :
struct Students{
short id;
int age;
double grade;
};
Students student1;
student1.age=14;

4- Structs can contain other structs and have variables of that type.For example :

struct Employee
{
short id;
int age;
double wage;
};

struct Company
{
Employee CEO; // Employee is a struct within the Company struct
int numberOfEmployees;
};

In this example CEO is a variable of other struct type called Employee.

Quiz)
1-
#include

using namespace std;

struct Advertising
{
int numberOfAdvertizes;
double percentageClicked;
double averageEarned;
};

 double printAdvertizingData(Advertising advert)
 {
     double totalIncome;
     cout<<"Number of Ads: " <<advert.numberOfAdvertizes<<endl;
     cout<<"Percentage of ads clicked on by users : " <<advert.percentageClicked<<endl;
     cout<<"Average of earnings from each ad: " <<advert.averageEarned<<endl;
     totalIncome= advert.numberOfAdvertizes * advert.percentageClicked * advert.averageEarned;


     return totalIncome;
 }

int main()
{
int noa=0;
double poc=0;
double aoa=0;
cout<<“Enter the number of ads”<<endl;
cin>>noa;
cout<<"Enter the Percentage of ads clicked on by users : "<<endl;
cin>>poc;
cout<<“Enter the Average of earnings from each ad :”<<endl;
cin>>aoa;

 Advertising adv;
 adv.numberOfAdvertizes=noa;
 adv.percentageClicked=poc;
 adv.averageEarned=aoa;


 cout<<"The total income is " <<printAdvertizingData(adv)<<" $"<<endl;
return 0;

}

2-
#include
using namespace std;

struct Fraction
{
int numerator;
int denominator;
};

double multiply(Fraction fraction1, Fraction fraction2)
{

return static_cast<double>(fraction1.numerator * fraction2.numerator) /
    (fraction1.denominator* fraction2.denominator);

}

int main()
{

Fraction fraction1;
Fraction fraction2;
cout << "Enter the numerator: ";
cin >> fraction1.numerator;
cout << "Enter the denominator: ";
cin >> fraction1.denominator;
cout << "Enter the numerator: ";
cin >> fraction2.numerator;
cout << "Enter the  denominator: ";
cin >> fraction2.denominator;

cout<<"The fraction in decimal is  "<<multiply(fraction1,fraction2)<<endl;

return 0;

}

1- when we want to create variables that are related to one object, it is easier to “concentrate” all the variables into one object. Structs allow us to group variables of mixed data types together into a single unit.

2- variables that are part of the struct are called members (or fields)

3- structName.memberName

4- when a struct contains another struct

1. Why do we need structs?
They allow up to group multiple variables of different data types together into a single unit.

2. What are members in a struct?
The members of a struct are the variables that will contain the values.

3. How do you access a member inside a struct?
members are accessed using the member selection operator ( . ), after the name given to the struct and before the member being accessed.

4. What is a nested struct?
A nested struct is a struct that is a member of another struct (a struct within a struct).

1. Why do we need structs?
Structs allow us to group variables of mixed data types together into a single unit.

2. What are members in a struct?
Members are variables declared inside the struct.

3. How do you access a member inside a struct?
After creating an instance of the struct, we can use the member selection operator to access individual members. For example:

Employee joe; // create an Employee struct for Joe
joe.id = 14; // assign a value to member id within struct joe

4. What is a nested struct?
A nested struct is a struct contained inside another struct. For example:

struct Company
{
    Employee CEO; // Employee is a struct within the Company struct
    int numberOfEmployees;
};