-
Why do we need structs?
In order to define an object with multiple variable types. -
What are members in a struct?
Variables defined within the struct. -
How do you access a member inside a struct?
StructName.MemberName -
What is a nested struct?
It’s a struct inside a struct.
Why do we need structs?
To group related variables together in an organised fashion.
What are members in a struct?
They are the variables that can be accessed through it.
How do you access a member inside a struct?
You can use the member selection operator ‘.’ For example struct.member = 0;
What is a nested struct?
A struct within a struct. For example when a member of a struct is of the type of another struct.
1. Why do we need structs?
To collect data from various variables (also of different variable types) and store it a single data structure.
2. What are members in structs?
They are the different variables in a struct.
3. How do you access a member within a struct?
With a period: structName.member
4. What is a nested struct?
A struct within a struct - where a struct is a member of another struct.
-
Why do we need structs?
Because we can use more than one variable to represent an object. -
What are members in a struct?
all the variables in the struct -
How do you access a member inside a struct?
*by using sructName.member * -
What is a nested struct?
A struct in a struct .
When we want a group of data elements grouped together under one name is easy to use Structs.
Members are the variables declared inside the struct.
In order to access the individual members, we use the member selection operator (which is a period => struct.member).
Structs can contain other structs.
-
We need structs because, there are many instances in programming where we need more than one variable in order to represent an object.
-
Members in a struct are variables within the struct.
-
we access a member we use the member selection operator which is a period. example; joe.wage
-
a nested struct is a struct that contains another struct or structs.
Actually its a struct within a struct
Think about the following questions while reading:
-
Why do we need structs?
A struct (short for structure) allows us to group variables of mixed data types together into a single unit. This allows us to group several data types, which characterize the same thing, together. Returning structs in a function allow us to return several values of different types. -
What are members in a struct?
The variables that are part of the struct are called members (or fields). -
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, which contains another struct.
Its a struct within a struct
This is the correct way to describe it brief and short! Thank you!
1. Why do we need structs?
to make structured objects with aggregated data
2. What are members in a struct?
The variables
3. How do you access a member inside a struct?
structname DOT variable
4. What is a nested struct?
a struct where one of the members is another struct
Why do we need structs?
Structs are a candidate when we need more than one variable in order to represent an object. You would need a variable and all its attributes
What are members in a struct?
Variables in a struct are called members or fields
How do you access a member inside a struct?
To access a member inside a struct, just call it: Employee joe;
What is a nested struct?
This happens when the CEO of a company is listed, in the company struct. His information is listed in struct employee
#include <iostream>
struct Advertising
{
int adsShown;
double clickThroughRatePercentage;
double AverageEarningsPerClick;
};
Advertising getAdvertising()
{
Advertising temp;
std::cout<<"How many ads were shown today?";
std::cin>>temp.adsShown;
std::cout<<"what percentage of ads were clicked on by users?";
std::cin>>temp.AverageEarningsPerClick;
return temp;
}
void printAdvertising(Advertising ad)
{
std::cout<<"number of ads shown:" <<ad.adsShown <<'\n';
std::cout <<"Click through rate:"<<ad.clickThroughRatePercentage <<'\n';
std::cout <<"average earnings per click: $" <<
(ad.adsShown * ad.clickThroughRatePercentage / 100 * ad.AverageEarningsPerClick) <<'\n';
}
int main()
{
Advertising ad {getAdvertising()};
printAdvertising(ad);
}
#include
struct Fraction
{
int numerator;
int denominator;
};
Fraction getFraction()
{
Fraction temp;
std::cout << "Enter a value for numerator: ";
std::cin >> temp.numerator;
std::cout << "Enter a value for denominator: ";
std::cin >> temp.denominator;
std::cout << ‘\n’;
return temp;
}
void multiply(Fraction f1, Fraction f2)
{
// Don’t forget the static cast, otherwise the compiler will do integer division!
std::cout << static_cast(f1.numerator * f2.numerator) /
(f1.denominator * f2.denominator) << ‘\n’;
}
int main()
{
// Allocate our first fraction
Fraction f1{ getFraction() };
Fraction f2{ getFraction() };
multiply(f1, f2);
return 0;
}
We need structs in order to construct our own variable types and hold particular values.
Members of a struct are variables declared as part of the struct.
In the following example, “x” would be a member of the struct, as it’s being declared here in the main function.
struct Rectangle
{
double length { 1.0 };
double width { 1.0 };
};
int main()
{
Rectangle x { 2.0, 2.0 };
return 0;
}
You access a member of a struct by using [ ]. Rectangle [0] from the above example will give you the length of a rectangle. Rectangle [1], the width.
A nested struct is a struct within another struct. Therefore a variable (member) of a struct will have a set of values from being a struct, as well as an overarching set of values from the original struct it’s nested within.
A member inside a struct is accessed using the selector operator (dot), for example employee.name
You access the member using selector operator dot, in your example x.width
I’m still trying to assimilate the information in structs. I don’t understand void. This seems redundant.
Advertising getAdvertising()
{
Advertising temp;
std::cout<<"How many ads were shown today?";
std::cin>>temp.adsShown;
std::cout<<"what percentage of ads were clicked on by users?";
std::cin>>temp.averageEarningsPerClick;
return temp;
}
void printAdvertising(Advertising ad)
{
std::cout<<"number of ads shown:"<<ad.adsShown<<'\n';
std::cout<<"clik through rate:" <<ad.clickThroughRatePercentage<<'\n';
std::cout<<"average earnings per click: $" <<ad.averageEarningsPerClick<<'\n';
std::cout<<"Total Earnings: $" <<
(ad.adsShown*ad.clickThroughRatePercentage/100*ad.averageEarningsPerClick)<<'\n';
}
/*both void and temp ask send info to the console, whether it is cout, or cin. Only one thing makes it's way to the console. Why do I need both?*/
I see that it works, the console only asks once, but in the code it is listed twice.
The getAdvertising
returns the object Advertising
that is build from the inputs inserted from a keyboard and the printAdvertising
is the function that prints the object Advertising
to the console.
The void
function is simply a function that doesn’t return anything. For functions that print an input to console this is not necessary therefore it is defined as a void function
So, you are saying that the void function does the math, and returns it as another member of Advertising?? or does it send it to temp advertising??
the last line in the void function is do the math then -> cout how does it send it anywhere except the console…in fact, I don’t see anywhere else that it would send the information to the console. The only other place where the console is referenced as the last thing is in advertising.temp, and it’s cin…
Sorry I don’t understand.
The temp is used in the getAdvertising
function as a temporary name for the Advertising
object before it gets returned to the main
function. You input the values of the object by using cin
which reads a stream from a standard input (which is the keyboard).
The object is then returned to the main function and the main function then passes it to the printAdvertising
function which takes an argument Advertising
. The print function then uses cout
to output the values of the Advertising
object to the default output (which is the console log).
After that the function is complete and the execution resumes back to the main function without returning any value to the calling function (notice that the printAdvertising
function does not have a return
statement).