Sei sulla pagina 1di 5

Programming II with C++ (CSNB244) Lab 5 Topics: Structure in C++ Part II

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.

Data structures are declared in C++ using the following syntax:


struct structName{ datatype variableInStruct1; //member of this struct datatype variableInStruct2; //member of this struct datatype variableInStruct3; //member of this struct

};

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

Programming II with C++ (CSNB244) Lab 5


struct }; car Proton; car Perodua; int main() { } car{ char model[20]; float price;

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;

strcpy string copy

Write the output of the program.


Perodua Enter a model name: SATRIA NEO Enter the year: 1909 I have a Proton Exora Year 2010 Your car is Perodua SATRIA NEO Year 1909
Page 2 of 5 Nov 2013

COIT NSMS

Programming II with C++ (CSNB244) Lab 5


Exercise 1 Write a program that consists of the following new structure type; Structure Name: Beverages Structure Members: a) char name[10] b) int price Based on the structure type created, create two objects; coffee, tea. The program should be able to get the input from user and print out the details of the entered coffee and tea. Pointer to Structures Example:
struct }; int main() { car Proton; car *ptrProton; } car{ char model[20]; float price;

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

car{ char model[20]; int year;

Programming II with C++ (CSNB244) Lab 5


Exercise 2 Write a program that consists of the following new structure type; Structure Name: Book Structure Members: a) char b_name[50] b) char author[50] Based on the structure type created, create two objects; book1, book2. The value of the object should be initialized by the programmer. To print out the details, the value should be displayed by using pointer. Function and Structures Example:
struct car{ char model[20]; int year; float price;

}; 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;

}; void printDetails(car); //function declaration int main() { car Proton1;


COIT NSMS Page 4 of 5 Nov 2013

Programming II with C++ (CSNB244) Lab 5


car Proton2; strcpy(Proton1.model, "Saga"); //copy value to a structure string Proton1.year=1995; //assign value to a structure - int Proton1.price=20000; //assign value to a structure - float strcpy(Proton2.model, "Iswara"); //copy value to a structure string Proton2.year=2000; //assign value to a structure - int Proton2.price=30000; //assign value to a structure - float cout << "Proton Booklet\n" ; printDetails(Proton1); //function call - pass a structure printDetails(Proton2); //function call - pass a structure } void printDetails(car carDetail) { cout << carDetail.model << "\nYear " << carDetail.year <<"\nPrice: RM" << carDetail.price << endl << endl; }

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.

Please read and follow the following instructions.


Dont forget to submit your source code, the screen capture of your program and the tracing asnwer to shakirah@uniten.edu.my with the subject: CSNB244 Lab5 <sectionNum> <YourStudentID> <Your Full name> at the end of the lab session. If you want to submit a softcopy file, please use the following file naming format: CSNB244 Lab5 <sectionNum> <YourStudentID> <Your Full name>.docx

COIT NSMS

Page 5 of 5 Nov 2013

Potrebbero piacerti anche