- Structs are needed when we need to group together various different data types.
- Members are individual variables in a struct.
- Members are accessed using the structName(dot)memberName notation.
- Nested structs are structs defined within another struct.
1.) When we need more than one variable in order to represent an object, we can use structs, which allow us to group mixed data types into a single unit. (Like a World of Warcraft character.)
2.) Variables that are part of the struct are called members.
3.) To access a member, we declare a variable type of the struct and use the member selection operator (which is a period).
4.) Nested structs are structs that contain other structs.
Why do we need structs?
Structs ae a way to define a new variable with several members. It allows you to associate attributes of a variable together and pass them to a function if you like.
-
What are members in a struct?
The members of a structure are the attributes that make up the structure like for a person would be the age height and other characteristics. -
How do you access a member inside a struct?
You can access members of a struct by using the dot command. -
What is a nested struct?
A nested struc is a struc inside of a struc.
Questions at the end of the Chapter.
#include <iostream>
using namespace std;
//create a structure called Point3d with three members.
struct Point3d
{
double x;
double y;
double z;
};
//Create a function called 'getZeroPoint' that will
//crate a new variable called 'temp' based on Point3d structions.
//and will initalize the variable using uniform list
Point3d getZeroPoint()
{
// We can create a variable and return the variable.
Point3d temp { 0.0, 0.1, 0.2 };
return temp;
}
Point3d getZeroPoint2()
{
// We can return directly. We already specified the type
// at the function declaration (Point3d), so we don't need
// it again here.
return { 0.3, 0.4, 0.5 }; //uniform list initialization
}
Point3d getZeroPoint3()
{
// We can use empty curly braces to zero-initialize all
// members of `Point3d`.
return {};
}
//Question #1 keep track of money made from website.
// set up structure webmoney
struct webmoney{
int numberadds;
int perclicked;
double cash;
};
// define new variable digigalsolutions using webmoney attributes
webmoney digitalsolutions {500,50,25.00};
//Define a function "adds" that will take input from user
//and use these inputs for a new variable based on webmoney structure
webmoney adds(){
int numberadds; //these variable are within this scope not global
int perclicked;
double cash;
cout<<"How many adds are you advertising on the website? ";
cin>>numberadds;
cout<<"What percentage of the adds are actually being clicked on? ";
cin>>perclicked;
cout<<"What is the average amount of daily cash you receive from each advertisement?";
cin>>cash;
return{numberadds,perclicked,cash};
};
struct Fraction{
double numerator;
double denominator;
};
Fraction multiply(){
double numerator;
double denominator;
cout<<"For the first fraction please input the numerator";
cin>>numerator;
cout<<"For the first fraction please input the denominator";
cin>>denominator;
return {numerator,denominator};
}
int main()
{
cout << "Hello world!" << endl;
cout<<endl;
cout<<endl;
// Here we define a Point3d structure named zero and it is initialized using a function called getZeroPoint().
// So a function initializes a variable of type Point3d.
cout<<"This is example from book on how to use functions to return structures"<<endl;
cout<<"And how to print out results from newly created structure members."<<endl;
cout<<"Based on conditions it will tell you if all 'zero' members equal zero(0) or not."<<endl;
cout<<"And it will print out the values of structure 'zero', 'zero2', and 'zero3'"<<endl;
cout<<endl;
Point3d zero{ getZeroPoint() };//function getZeroPoint returns a structure zero with three(3) points.
if (zero.x == 0.0 && zero.y == 0.0 && zero.z == 0.0)
std::cout << "All members of structure 'zero' are equal to '0' is true\n"<<zero.x<<" " <<zero.y<<" " <<zero.z<<endl;
else std:: cout<<"All members of structure 'zero' are equal to '0' is false\n"<<zero.x<<" "<<zero.y<<" " <<zero.z<<endl;
cout<<endl;
cout<<"The members of structure 'zero' created from the function 'getZeroPoint' are equal to: " <<zero.x<<" " <<zero.y<<" "<< zero.z<<endl;
cout<<endl;
Point3d zero2{getZeroPoint2()};
cout<<"The member of structure 'zero2' created from the function 'getZeroPoint2' are equal to: " <<zero2.x<<" " <<zero2.y<<" "<< zero2.z<<endl;
cout<<endl;
Point3d zero3{getZeroPoint3()};
cout<<"The members of structure 'zero3' created from the function 'getZeroPoint3' are equal to: " <<zero3.x<<" " <<zero3.y<<" "<< zero3.z<<endl;
cout<<endl;
cout<<endl;
//Question #1 using structure webmoney and variable digitalsolutoins with all values initialized at start.
cout<<"These are the results for Question #1 from reading assigment."<<endl;
cout<<"For this example I first set up a structure 'webmoney' and define a new variable 'digitalsolutions"<<endl;
cout<<"I initialize the variable with set values for each member"<<endl;
cout<<endl;
cout<<"I have made a lot of money by advertising on my website Digital Solutions Technology "<<endl;
cout<<endl;
cout<<"I had placed a certain number of adds totaling: "<<digitalsolutions.numberadds<<endl;
cout<<"The percent of adds that were actually clicked on was: "<< digitalsolutions.perclicked<<" percent"<<endl;
cout<<"This gave the actual number of adds that were clicked on equal to: "<< digitalsolutions.numberadds*digitalsolutions.perclicked/100<<endl;
cout<<"The total income I made from all the adds that were clicked on is: $" <<digitalsolutions.numberadds * digitalsolutions.perclicked*digitalsolutions.cash/100<<endl;
cout<<endl;
cout<<endl;
//Here I create a new variable addTotal using the function add to initialize the variable
//based on user input variables.
cout<<"I will now redo this Question #1 using input from the user to initialize a new structure variable that will be created"<<endl;
cout<<"from a function that will ask the user for each input and then assign these values to structure members"<<endl;
cout<<endl;
webmoney addTotal{adds()};
cout<<endl;
cout<<endl;
//Use variable addTotal members to record results of user input.
cout<<"I have made a lot of money by advertising on my website Digital Solutions Technology "<<endl;
cout<<endl;
cout<<"I had placed a certain number of adds totaling: "<<addTotal.numberadds<<endl;
cout<<"The percent of my adds that were actually clicked on were: "<< addTotal.perclicked<<" percent"<<endl;
cout<<"This gave the actual number of adds that were clicked on equal to: "<< addTotal.numberadds*addTotal.perclicked/100<<endl;
cout<<"The total income I made from all the adds that were clicked on was: $" <<addTotal.numberadds * addTotal.perclicked*addTotal.cash/100<<endl;
//Question # ask that you take input of two fractions from the user and then multiply those fraction to get a decimal
//For this problem I created two fraction structure with members as numerator and denominator.
//I then created a function that will as user for input of two fractions numerator and denominator and
//Will then return two structure fractions. Fraction first and Fraction second
cout<<endl;
cout<<"The result of multiplying two fractions based on user input are shown below as the user will input"<<endl;
cout<<"and the denominator for both fraction, the first and the second"<<endl;
cout<<endl;
Fraction first{multiply()};
Fraction second{multiply()};
cout<<"The decimal equivalent of multiplying the two fractions"<<endl;
cout<<endl;
cout<<"first fraction: numerator = "<<first.numerator<<endl;
cout<<"The first fraction denominator = " <<first.denominator<<endl;
cout<<"second fraction: numerator = "<<second.numerator<<endl;
cout<<"The second fraction denominator = " <<second.denominator<<endl;
cout<< "The result of multiplying these two fractions are:"<<(first.numerator/first.denominator)*(second.numerator/second.denominator)<<endl;
cout<<endl;
return 0;
}
-
We needs structs so that we can group variables of mixed data types together into a single unit.
-
Members, also known as fields, are the variables of various data types within a struct.
-
In order to access the individual member within a struct we use the member selection operator which is a period. Example. struct_name.member or Employee joe β joe.age, joe.id, joe.wage
-
Nested structs are structs that contain other structs as their field members.
1. Why do we need structs?
We need structs (structures) so we can group multiple individual variables together.
2. What are members in a struct?
The members in a struct are the different variables that are part of the the struct.
3. How do you access a member inside a struct?
In order to access members inside a struct we use the member selection operator (which is a period).
4. What is a nested struct?
A nested struct is a struct within a struct.
-
Why do we need structs?
Easier to organize many details about something. -
What are members in a struct?
The variables inside the struct. -
How do you access a member inside a struct?
use period. -
What is a nested struct?
Structs have other structs inside of it.
- We need structs in order to group variables of mixed data types together into a single unit.
- Members are variables that are part of the struct.
- We can access a member inside a struct by using the member selection operator.
- Nested are Structs that contain other structs.
-
Why do we need structs?
Structs can be used to represent objects with a group of mixed data types. -
What are members in a struct?
Members are variables defined in a struct. -
How do you access a member inside a struct?
A member inside a struct can be accessed by the member selection operator β a period. The format is in the form of struct.member. -
What is a nested struct?
Nested struct is formed when a struct is a member of another struct.
How would putting multiple inputs into a member in the struct work? For examples:
struct Favfood{
string food;
};
Favfood getdata () {
Favfood temp {};
cout << "what are 3 of your fav foods" << endl;
cin >> temp.food;
return temp;
}
void printFood (Favfood f){
std::cout << "these are your fav foods " << f.food<<endl;
}
int main () {
Favfood f{getdata ()};
}
I would use an array in that case
- A struct allows us to group variables of mixed data types together in a single unit.
- Variables in a struct.
- With a member selection operator - which is a period.
Employee joe{}
joe.id= 14;
joe.age=32;
- Structs that contain other structs.
- To have data types that contain many different types of elements.
- They are the variable types and names within a struct.
- By using the name of the struct followed by a period and then the name of the member that you would like to get. Struct.member[0]
- A struct that is a member within another struct.
- Sometimes we may need to group multiple variables of mixed data types together into a single unit, which saves us the trouble of declaring so many variables. Structs come in handy for this.
- Members are the variables that are part of a struct
- the same way you access any other variable, just use structname.member
- When a struct contains another struct, that way we can select members from individual structs within a broader struct
1. Why do we need structs?
To create custom or composite data types.
2. What are members in a struct?
Individual variables that make up the struct.
3. How do you access a member inside a struct?
Struct.member;
4. What is a nested struct?
A struct with another struct as a member.
-
Why do we need structs?
We need structs as it is a way of storing multiple variable types under an umbrella object. It saves us lots of time as we donβt have to declare each new variable type for each property of the object. We can aggregate all of the different data types under an object. -
What are members in a struct?
Members are the different variables that are stored in the struct. -
How do you access a member inside a struct?
You use the member selection operator (which is a period) after the name of the struct. -
What is a nested struct?
Nested structs are when there is a struct within a struct. This can happen for many reasons. To find a specific variable value you can use two different periods to identify the value you are after.
1 - Structs allow us to aggregate variables that are of different data-types into a single unit, as opposed to passing each variable individually, and if you need to assign the same variables to another object you would have to declare the same variables again:
struct Employee
{
int id{};
int age{};
double wage{};
};
The example above shows how a struct is formatted. We can see we have created a struct and named it Employee, inside which contains 3 variables (or members). To be able to use this Employee struct we would declare a variable named Employee. When naming a struct we must always ensure that the name starts with a capital, so that the program can differentiate between the struct and the variables (which are all written in lower case). We can also use the same struct type to define multiple variables:
*Employee joe{};
Employee frank{};*
As in the example above, we can see that the Employee struct can be assigned to 2 individuals. This can be useful for when creating a payroll, in which each Employee can be assigned a struct of the same type but have different values for their wage, the hours the worked and the bank account into which their wage is payed etc.
2 - Structs all contain members - these are the variables that are inside the struct. These members are not initialized upon their creation, they have to be done so manually. C++ allows us to use intitializer lists to create individual declaration statements for each individual struct, allowing us to have neat and concise code.
3 - To access these members we must use the member selection operator - also known as a period:
Employee joe{};
joe.id = 14;
joe.age = 32;
joe.wage = 24.15;
This example shows that to access the id member for variable joe, we must write the code as, joe.id and then assign it a value. And if we were assigning values for another variable we would simply put the name of the variable which we are defining - frank.id = 15;.
4 - A nested struct is where a struct contains other structs
-
Why do we need structs?
There are many instances in programming where we need more than one variable in order to represent an object. A struct (short for structure) allows us to group variables of mixed data types together into a single unit. -
What are members in a struct?
Members are variables contained by a struct. -
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). -
What is a nested struct?
A nested struct is a struct that contains another struct as itβs member.
-
Why do we need structs?
So we can store many variables in one object. -
What are members of a struct?
Members or fields are the variables stored in the struct -
How do you access a member inside a struct?
Through member selection operator which is the dot β.β -
What is a nested struct?
Itβs a struct contained in the other struct.
- Structs are useful to group individual values that belong together.
- Members of a struct are its variables
- structName.memberName
- A nested struct is a struct inside another.
- Why do we need structs?
Structs allow us to define custom variables. We use them to group a set of variables of any datatype together into a structure.
- What are members in a struct?
A struct is made up of members. Members are the different datatypes that make up the struct.
- How do you access a member inside a struct?
structname.membername;
- What is a nested struct?
A struct inside a struct. This means that a struct can be a member of another struct.