Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
In this lab session, you will learn on how to create a program that includes structure. A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths.
};
structName is a name for the structure type. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name. Data structures should be declared before you write/outside of the main function. Once a data structure is declared, a new type with the identifier specified as structName is created and can be used in the rest of the program as if it was any other type. Example:
struct }; car{ char model[20]; float price;
From the above example; car a) What is the structure type? Answer: __________________________ 2 b) How many data members of this new structure type? Answer: ________________ Now, lets create an object of this new type of structure.
car Proton; car Perodua;
This new object can be created within main or any other function (local variable/object) or can be placed outside of any function (global variable/object). Local variable/object is visible and can be used within that specific function. Example of local object:
struct }; int main() { car Proton; car Perodua; } car{ char model[20]; float price;
Global variable/object is visible to every function of the same program. Example of global object:
COIT NSMS Page 1 of 5 Nov 2013
It is important to clearly differentiate between what is the structure type name, and what is an object (variable) that has this structure type. From the above example; 2 a) How many objects that we have created? Answer: ________________ 2 b) List the name of the object(s) that we have created. Answer: _________________ Lets see a real example.
#include <iostream> #include <string> using namespace std; struct }; int main() { car Proton; car Perodua; strcpy(Proton.model, "Exora"); //assign value to a structure string Proton.year=2010; //assign value to a structure - int cout << "Perodua" << endl; cout << "Enter a model name: "; cin.getline(Perodua.model,20); cout << "Enter the year: "; cin >> Perodua.year; cout << "I have a Proton " << Proton.model << " Year " << Proton.year <<endl; cout << "Your car is Perodua " << Perodua.model << " Year " << Perodua.year <<endl; } car{ char model[20]; int year;
COIT NSMS
Here Proton is an object of structure type car, and ptrProton is a pointer to point to objects of structure type car. The value of the pointer ptrProton would be assigned the reference to the object Proton (its memory address).
ptrProton = &Proton;
We will now go with another example that includes pointers, which will serve to introduce a new operator: the arrow operator (->):
#include <iostream> #include <string> using namespace std; struct }; int main() { car Proton; car *ptrProton; ptrProton = &Proton; strcpy(Proton.model, "Exora"); //assign value to a structure string Proton.year=2010; //assign value to a structure - int cout << "The following display is printed via ptrProton\n"; cout << "I have a Proton " << ptrProton->model << " Year " << ptrProton->year <<endl; delete ptrProton; }
COIT NSMS Page 3 of 5 Nov 2013
}; void printDetails(car); //function declaration int main() { car Proton; printDetails(Proton); //function call - pass a structure } void printDetails(car carDetail) //function definition { }
From the above example; 1 a) How many objects that we have created? Answer: ________________ proton b) List the name of the object(s) that we have created. Answer: _________________ 1 c) How many arguments that printDetails function will receive? Answer: __________ d) Name of the object that has been created in printDetails function. Answer: car detail __________ You can pass a structure as a function argument in very similar way as you pass any other variable. You would access structure variables in the similar way as you have accessed in the above example. Lets see a real example.
#include <iostream> #include <string> using namespace std; struct car{ char model[20]; int year; float price;
Exercise 3 Write a program that consists of the following new structure type; Structure Name: mobilephone Structure Members: a) char pModel[10] b) int price Based on the structure type created, create three objects; apple, blackberry, samsung. The value of the object should be initialized by the programmer. There should be a function to print out the details, and the value should be displayed by using a pointer.
COIT NSMS