Download - CS125 - Lab 08 - Structures
-
8/13/2019 CS125 - Lab 08 - Structures
1/3
CS125: Programming Fundamentals
Lab No. 8: Structures
Lab No. 8
Structures
Objective1. Structure Declaration2. Defining a Structure Variable3. Accessing Structures members4. Array of Structures5. Pointers to Structures6. Get and Set Functions.
Programming Practices
Declaring the Structures
The structure declaration tells how the structure is organized: It specifies what members thestructure will have. Here it is:
structpart
{intmodelnumber;
intpartnumber;floatcost;
};
Defining a Structure VariableThe first statement in main(),
intmain(){
part part1;
}
defines a variable, called part1, of type structure part. This definition reserves space in memory for
part1. How much space?
Exercise:Use sizeof() to check the size of part1variable.
Accessing Structure Membersintmain(){
part part1;part1.modelnumber = 6244;part1.partnumber=1234;
part1.cost=1200.99;
}
-
8/13/2019 CS125 - Lab 08 - Structures
2/3
CS125: Programming Fundamentals
Lab No. 8: Structures
Initializing Structure membersintmain(){
//initialize variable
part part1 = { 6244, 373, 217.55F };part part2; //define variable
//display first variablecout
-
8/13/2019 CS125 - Lab 08 - Structures
3/3
CS125: Programming Fundamentals
Lab No. 8: Structures
Pointer to Structureintmain(){
part* part1;
part1->partnumber=1234;part1->modelnumber=2007;part1->cost=129.75;
return0;}
Using Get and Set Functionsstructpart{intmodelnumber;intpartnumber;
floatcost;
voidset(intmn, intpn, floatc){
modelnumber=mn;partnumber=pn;
cost=c;}
intgetModelNumber(){
returnmodelnumber;
}intgetPartNumber(){
returnpartnumber;
}intgetCost(){
returncost;}};intmain()
{part part1;part1.set(1234,7657,22.3);cout