Structs C++ - Reading Assignments

  1. since there are times that we would need to store datas of different types, for instance adding a profile to database which contains of different data types such as digits and strings therefore we need Structs since it allows us to do so.

  2. The different variables that are in a struct are called members.

  3. we use member selection operator

  4. structs can contain other structs, thats what we call nested struct.

1 Like
  1. Why do we need structs?
    -Structs enable us to create user defined data structures. For example, I can create a data structure that has multiple attributes: Name, Address, Phone, Age, Birthdate, etc.
  2. What are members in a struct?
    -The members are the individual components making up the struct as indicated in the above example.
  3. How do you access a member inside a struct?
    -You use the β€œdot” syntax: Member.name, Member.address, Member.phone, etc.
  4. What is a nested struct?
    -A nested struct is when you have a struct as one of the components within another struct.
1 Like
  1. Because build-in data types are too limiting and often we need to represent more complexed objects with multiple variables, struct is providing us a way to do that properly.

  2. Variables declared in a struct are called members or fields/properties. They usually represent particular traits of the struct (the name or age of a person for example).

  3. structIdentifier.memberIdentifier

  4. This is a struct which is a member of another struct.

1 Like
  1. We need structs to group variables of mixed data types together into a single unit.

  2. Members are the variables in a struct; they are also called fields.

  3. To access a member inside a struct, use the member selection operator, which is only a period; in short,struct.member.

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

Thank you!

1 Like
  1. Why do we need structs? We need structs to aggregate data type of multiple individual variables together, customized to the programmer.
  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? StructName.memberName
  4. What is a nested struct? It is a struct onside other struct.
1 Like

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

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

3. How do you access a member inside a struct?
We use the member selection operator struct.member

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

1 Like
Why do we need structs?
What are members in a struct?
How do you access a member inside a struct?
What is a nested struct?

1- we need structs to store several variables into a single unit.
2-members of structs are variables.
3-structname.membername
4-a nestesd struct is a a struck that is inside another struct as a member.

1 Like
  1. Why do we need structs? // To group variables of different data types into a single object.

  2. What are members in a struct? // The variables put into the struct.

  3. How do you access a member inside a struct? // The member selection operator or ( . ) is used between the struct name and struct member.

  4. What is a nested struct? // A struct inside another struct.

1 Like
  1. Why do we need structs?
    It helps to organize variables together and cuts having to assign multiple variables tediously.

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

  3. How do you access a member inside a struct?
    By using a period which is the member selection operator. (e.g. structref.member)

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

1 Like

1.Why do we need structs?
-> There are situations you need to represent multiple feature related to one object.

2.What are members in a struct?
-> it is the fields in the struct.

3.How do you access a member inside a struct?
-> by adding . and the name of the member.

4.What is a nested struct?
-> It is storing a struct in another struct. Company (Employee)

1 Like
  1. We can use structs to represent objects by grouping variables of mixed types into a single struct data type.

  2. Members (or fields) within a struct are the variables it contains.

  3. Members can be accessed with the member selection operator ’ . ’ .

  4. A nested struct is a struct within a struct. It is contained as a member of another struct, and it’s members can be accessed by using the member selection operator β€˜.’ twice. Nested structs can also be initialized with a nested initializer list.

1 Like
  1. Structs are needed to aggregate multiple variables of different data types into a single unit. They are good solution to represent a real-life object and manipulate it in a comfortable way.
  2. Members are component variables of a struct. They are also known as fields.
  3. We can access a field through the member selection operator which is a fancy name for a period. Example structName.structsMember.
  4. A nested struct is simply a member of another struct.
1 Like
  1. Structs enable us to merge multiple, different types of variables into single unit.
  2. Members in struct are different variables inside struct, defined by user.
  3. structName.memberName
  4. Struct that contains another struct inside of them.
1 Like
  1. Why do we need structs?

Structs help eliminate the redundancy of creating several variables for the same type of object.

  1. What are members in a struct?

Members are the individual variables or fields within the struct, and they can be be assigned certain values.

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

The dot operator (.) can be used after declaring the struct name. Ex: Fraction.numerator

  1. What is a nested struct?

A struct where one of the fields is a struct type.

1 Like
  1. to organize things in neat little tables of values (which can be of different data types) all pertaining to different variables of a group or a single object within same group
  2. variable in a specific struct group
  3. call them by calling up a struct, then add a point, then the member, and you get the value, like this Struct.member
  4. a struct within a struct, a substruct :slight_smile:
1 Like
  1. Its much more logical/efficient to use structs. As it allows us to easily store a group of variables and mixed data types together into a single unit. Instead of individually declaring independent variables each time we want to add a new variable.
  2. Variables that are part of structs
  3. By using the member selection operator (which is a period). For example, bob.age
  4. Structs that contain other structs.
1 Like

We need structs in order to create structures that can hold variable information in a scaleable manner.

The members of a struct is the individual variables that are held in the struct.

By calling the member in the following manner: nameofstruct.nameofmember.

For instance: Person.name.

Its a struct within a struct. It is used when information inside the mother struct has more information paired with it.

1 Like
  1. Why do we need structs?
    It allows us to group variables of many mixed data types together in single unit. For example a struct could be created to hold variables that represent an object, such as a Car, with characteristics such as doors, wheels, miles, number plate, fuel, transmission and colour.

  2. What are members in a struct?
    These are the variables in the struct, they can also be known as fields. In the car example above, the members are doors, wheels, miles, number plate, fuel, transmission and colour.

  3. How do you access a member inside a struct?
    To access individual members in the strut, use the member selection operator(full stop/period).
    For example, mercedes.fuel

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

QUIZ 1

#include <iostream>
using namespace std;

struct Advertising
{
    int adsShown;
    double clicked;
    double earned;
};

Advertising getAdvertising()
{
    Advertising temp;
    cout<<"How many ads were shown today?"<<endl;
    cin>>temp.adsShown;
    cout<<"What percentage of ads were clicked on by users?"<<endl;
    cin>>temp.clicked;
    cout<<"What was the average earnings per click?"<<endl;
    cin>>temp.earned;
    return temp;
}
void printInfo(Advertising a)
{
    cout<<"The number of ads shown to readers today is: "<< a.adsShown<<endl;
    cout<<"The number of ads clicked on today is: "<< a.clicked<<endl;
    cout<<"The average earned for each click today is: "<< a.earned<<endl;
    cout<<"Total Earnings today: "<< (a.adsShown * a.clicked/100 * a.earned)<<endl;
}

int main()
{
    Advertising a{getAdvertising()};
    printInfo(a);
    return 0;
    }

QUIZ 2

#include <iostream>
using namespace std;
struct Fraction
{
    int numerator;
    int denominator;
};
Fraction getFraction()
{
    Fraction temp;
    cout<<"Enter the numerator and denominator:"<<endl;
    cin>>temp.numerator;
    cin>>temp.denominator;
    cout<<'\n';
    return (temp);
}
void multiply(Fraction a,Fraction b)
{
    cout<<static_cast<double>(a.numerator*b.numerator)/(a.denominator*b.denominator); '\n';
}
int main()
{
    Fraction a{getFraction()};
    Fraction b{getFraction()};
    multiply (a,b);
    return 0;
}
2 Likes
  1. to combine multiple variables of an object (e.g. name, age, gender of object person)
  2. variables of the struct. e.g name, age etc
  3. struct.variable
  4. a struct inside a struct e.g. object company has variables employees, buildings, name and the object employee has variables name, age, salary
2 Likes

1 With a struct yo can give a function several arguments at the same time, as you do not have to handle every variable separatly.

2 The members are the parts of the struct. A struct is like an array, but each element can be a different type. The members correspond to the elements.

3 struct.member

4 You may define a struct as a list of variables (members). But a struct is also a variable so if you have a struct as on of the variables in your struct, you have a nested struct.

1 Like