Structs C++ - Reading Assignments


#101

Why do we need structs?
Allows groups of different variables to be referred to as a distinct unit

What are members in a struct?
Fields, consisting of different variables

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

What is a nested struct?
A struct declared within another struct


#102

1. Why do we need structs?
When u need to group up many variables into a single data type.
2. What are members in a struct?
variables inside the struct parameters.
3. How do you access a member inside a struct?
struct.memberDataType
4. What is a nested struct?
An struct defined has a member inside another struct.


#103
1. Why do we need structs?

We need structs in order to group multiple individual variables of different data types together. This make it easier to pass information more easily to a function and declare faster several “entities”.

2. What are members in a struct?

The members are the variables inside the struct that define it. For example declaring an employee in a program might include its age, wage, department etc…

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

Accessing a member in a struct is done by using the name of variable that is the struct, and then appending the member selection operator, which is simply a period, and then the member previously declared in the struct. For example, I declare my struct:

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

Then I declare a variable, which type is the struct I created:

Employee joe; 
Then I access the members like so:
joe.id = 14;
4. What is a nested struct?

A nested struct is a struct within a struct

struct Employee
{
    short id;
    int age;
    double wage;
};
 
struct Company
{
    Employee CEO; // Employee is a struct within the Company struct
    int numberOfEmployees;
};
Company myCompany;