Structs C++ - Reading Assignments

1.- To describe multiple objects within a single variable.

2.- Variables

3.- struct.member

4.-A struct inside a struct.

1 Like
  1. Why do we need structs?

We need structs in order to represent objects with multiple variables.

  1. What are members in a struct?

Each variable in a struct is a member.

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

structName.member

  1. What is a nested struct?

Nested structs are structs that contain other structs.

1 Like
  1. Instances when you need to create more than one variable to represent an object.

  2. The various IDs we have created to define that overall struct, such as Struct called Andre, and within it there is, name, age, height, weight, etc., .

  3. Use the member selection operator which is a period / full stop (UK) e.g.,Andre.age = 30;

  4. A struct within a struct

1 Like
  1. Structs are like the object in javascript where we put multiple variable that represent the object and it is very helpful and easy to use when you have many srtucts you can get all its variable with ease.

  2. Members are variables in the struct that we can access by using period/

  3. struct.member

  4. When struct has another struct as a member.

1 Like
  1. We need structs to allow us to group variables of different data types together into a single unit.
  2. Variables of fields inside of a struct are called members.
  3. You access a member inside of a struct through a member selection operator (.), which provides consistency across multiple variables of the same struct type.
  4. A nested struct is a struck that is contained within another struct, that allows us to use variables across multiple structs.
1 Like

Why do we need structs?
Structs allow us to group different variables, even in types, under a single name.

What are members in a struct?
Member are each of the variables that make the struct.

How do you access a member inside a struct?
By simply typing its name after a dot. In the example of the struct Person if I define that Person Henrique we can access my age by typing Henrique.age.

What is a nested struct?
Nested struct is a struct within a struct.

1 Like

1- A struct allows us to group variables of mixed data types together into a single unit.

2- The variables that are part of the struct are called members.

struct Person{

    string name;
    int age;
    string gender;

};
  

a string named as name, and int named as age, and another string named as gender are the members of the Person struct.

3- When we define a variable such as Person tolga , tolga refers to the entire struct (which contains the member variables). In order to access the individual members, we use the member selection operator (which is a period).

Person tolga;
tolga.name = "Tolga";
tolga.age = 28;
tolga.gender = "erkek";

4- Struct within another structs are called as nested struct.

1 Like
  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? variables are grouped together

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

  4. What is a nested struct? Struct within a struct

1 Like
  1. Structs help us group variables in a certain way, which can be used to create more complicated programs.
  2. The variables in a struct individually, are called members.
  3. StructName.memberName
  4. A nested struct is, when we put a struct inside of another, as a member.
1 Like
  1. Why do we need structs?

A struct is a data type that can hold multiple variables of different data types in one unit. This is very useful because datasets consisting of different data types don’t have to be stored as individual variables, instead they can be stored in one unit.

  1. What are members in a struct?

Members are the variables that are defined inside a struct and thus are part of a struct.

  1. How do you access a member inside a struct?
structName.memberName
  1. What is a nested struct?

A nested struct is a struct that is a member of another struct, i.e. a struct inside another struct.

2 Likes
  1. Structs allow the user to group variables of different data types together.
  2. The variables within the struct.
  3. By using the member selection operator (period).
  4. A struct within a struct is a nested struct.
2 Likes

FORUM QUESTIONS

  1. Why do we need structs?
    Structs allow programmers to set customisable data types. These struct data types can have multiple different variables inside, each with unique data types onto themselves.
    Structs allow programmers to easily handle things where it may require multiple variables pertaining one thing. Not to mention, the thing might require many different data types for each variable.
    We can therefore create a struct to represent something, and use that as a data type. Everything then added within can become a variable, meaning we can do some important fetching and manipulating of data in a streamlined and easy-to-read manner.

  2. What are members in a struct?
    Members in a struct are essentially fields of the struct. So every new variable which has the data type of that struct will have some data for each of these fields (or members).
    An example of this could be a struct of a Phone, and the members of the struct could be particular detail classes, which will differ between each phone.
    So, some members could be:

    • string brand
    • string model
    • int manufactureYear
    • etc.
      When a variable is called with data type Phone, we can add this personal data, or go fetching for it easily.
  3. How do you access a member inside a struct?
    You access a member inside a struct the same way you would a variable. Say you have a variable jacksPhone which has data type Phone, you can find that specific member brand by calling cout << jacksPhone.brand;

  4. What is a nested struct?
    A nested data struct can exist as a regular data type when initializing a non-nested struct. Instead of initializing a variable as int, for example - you can initialize it with a struct - which is just a custom data type!
    An example could be a struct, Person, with members:

    • string name
    • int age
    • string gender
      Then you may also have a separate struct called Family, which has members:
    • string address
    • Person mother
    • Person father
    • string familyHeritage
      Notice that there are nested structs with the mother and father in the Family struct.
      This means that if you wanted to find the Mother’s age of a family, you could call: cout<< familyName.mother.name;

ARTICLE QUESTIONS

  1. You are running a website, and you are trying to keep track of how much money you make per day from advertising. Declare an advertising struct that keeps track of how many ads you’ve shown to readers, what percentage of ads were clicked on by users, and how much you earned on average from each ad that was clicked. Read in values for each of these fields from the user. Pass the advertising struct to a function that prints each of the values, and then calculates how much you made for that day (multiply all 3 fields together).
#include <iostream>
using namespace std;

struct Advertising {
    int dailyViews{};
    double percentClicked{};
    double clickRev{};
};

void calculateRev(Advertising ad){
    cout<< "How many ads do you show to your users on a daily basis?" << endl;
    cin>> ad.dailyViews;
    cout<< "What percentage of those ads you show get clicked? (Type number)" <<endl;
    cin>> ad.percentClicked;
    cout<< "How much revenue do you receive for your ads per click?" <<endl;
    cin>> ad.clickRev;
    double dailyRev = (ad.dailyViews*(ad.percentClicked/100)*ad.clickRev);
    cout<< "Thanks for your information."<<endl<<"Your daily revenue should be $"<<dailyRev<<endl;
    }


int main()
{
    Advertising myWebsite;
    calculateRev(myWebsite);

    return 0;
}
  1. Create a struct to hold a fraction. The struct should have an integer numerator and an integer denominator member. Declare 2 fraction variables and read them in from the user. Write a function called multiply that takes both fractions, multiplies them together, and returns the result as a decimal number. You do not need to reduce the fraction to its lowest terms. Print the result of the multiplication of the 2 fraction variables.
#include <iostream>
using namespace std;

struct Fraction {
    int numerator{};
    int denominator{};
    };

double multiply(Fraction fr1, Fraction fr2) {
    cout<< "Hi there, we're going to define the first fraction!"<<endl<<endl;
    cout<< "What is the numerator for the first fraction?" <<endl;
    cin>> fr1.numerator;
    cout<< "What is the denominator for the first fraction?" <<endl;
    cin>> fr1.denominator;

    cout<< "Great! Now we're going to define the second fraction!"<<endl<<endl;
    cout<< "What is the numerator for the second fraction?" <<endl;
    cin>> fr2.numerator;
    cout<< "What is the denominator for the second fraction?" <<endl;
    cin>> fr2.denominator;
    double result = (fr1.numerator/fr1.denominator)*(fr2.numerator/fr2.denominator);
    cout<< "The multiplication of your two fraction "<< "("<<fr1.numerator<<"/"<<fr1.denominator<<")*("<<fr2.numerator<<"/"<<fr2.denominator<<") is "<<result;
}

int main()
{
    Fraction fraction1{};
    Fraction fraction2{};
    multiply(fraction1, fraction2);

    return 0;
}
2 Likes
  1. in order to reflect entire objects/persons/things with characteristics belonging to them.
    E.g. a car struct could contain variables such as max speed, fuel per 100 km, number of doors, owner,…
    Defining single variables to reflect one or more cars would be way less efficient and much more complicated to handle.

  2. Each contained variable of a struct is called a member

  3. structname.variablename

  4. A nested struct is a struct contained in a struct.

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

  2. What are members in a struct?
    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?
    Struct within a struct.

1 Like
  1. structs help us group variables into different groups with neater code

  2. the variables in the struct

3.Employee joe{};

4.a struct in a struct

1 Like
  1. Why do we need structs?

Structs allow us to group individual variables into one new variable. Without structs, we would have to call the same individual variables multiple times in the case of complex programming projects.

  1. What are members in a struct?

Members in a struct are the individual variables that are wrapped within the struct.

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

We can access members by using the member selection operator, which is simply a dot. For example, if I had struct Person, and one of the variables inside Person was age, I would access that with another variable called bob and type in bob.age.

  1. What is a nested struct?

Nested structs are simply structs within structs that are used for a more complex assignment of structs.

1 Like
  1. Because it allows us to group variables of mixed data types together into a single unit. Without it, too many variables would have to be created in certain cases that it would be very confusing and a lot more work would be involved.

  2. Members are the variables inside the struct.

  3. By using a member selection operator (".")

  4. Nested struct is on that contains at least one other struct inside it.

1 Like
  1. We need to aggregate data as structs in order to define new object types having multiple properties.
    Without structs, doing operations on large sets of data having a defined list of properties would technically be possible but extremely cumbersome.

  2. Members are the variables embedded in a struct. Logically speaking, they are the properties of the object defined as struct.

  3. With the member selection operator (period symbol) placed between struct identifier and member identifier (example: joe.age)

  4. Nested structs are structs containing other structs.

1 Like
  1. We need structs so that we can group variables of mixed data type together into a single unit, an aggregate data type.

  2. Members are variables that are part of the struct.

  3. Use the member selector operator, the period (.), to access individual members inside a struct, e.g. person.age

  4. A nested struct is a struct that is contained within another struct.

1 Like
  • Why do we need structs?
    In order to easily group together variables belong to one thing or object
  • What are members in a struct?
    Members are the varibles inside the struct
  • How do you access a member inside a struct?
    you can call on them with a period struct name “.” variable name
  • What is a nested struct?
    this is a struct inside of a struct therefore being nested inside of another struct
1 Like