Sie sind auf Seite 1von 4

Programming II

Universiti Tenaga Nasional (UNITEN)

CMPD 244
Programming II
Lab 4: Structure in C++
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;
a) What is the structure type? Answer:
_____car_____________________
b) How many data members of this new structure type? Answer:
_________2_______
Now, lets create an object of this new type of structure.
car Proton;
car Perodua;

Prepared by Nur Hanani Azami

2014

Programming II
Universiti Tenaga Nasional (UNITEN)

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 car{
char model[20];
float price;
};
int main()
{
car Proton;
car Perodua;
}

Global variable/object is visible to every function of the same


program.
Example of global object:
struct car{
char model[20];
float price;
};
car Proton;
car Perodua;
int main()
{
}

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;
a) How many objects that we have created? Answer:
_________2_______
b) List the name of the object(s) that we have created. Answer:
______proton, perodua___________
Lets see a real example:

Prepared by Nur Hanani Azami

2014

Programming II
Universiti Tenaga Nasional (UNITEN)

#include <iostream>
#include <string>
using namespace std;
struct car{
char model[20];
int year;
};
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;
}

Write the output of the program.

Enter a model name: Proton


Enter the year: 2010
I have a Proton Exora Year 2010
Your car is Perodua Proton Year 2010
Press any key to continue . . .
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
#include <iostream>
#include <string>
using namespace std;

Prepared by Nur Hanani Azami

2014

Programming II
Universiti Tenaga Nasional (UNITEN)

struct baverages{
char name[10];
int price;
};
int main()
{
baverages coffee;
baverages tea;
strcpy_s(coffee.name, "Nescafe"); //assign value to a structure - string
coffee.price = 20; //assign value to a structure - int
strcpy_s(tea.name, "Boh Teh"); //assign value to a structure - string
tea.price = 10; //assign value to a structure - int
cout << "Enter a coffee or tea name: ";
cin.getline(coffee.name, 10);
cout << "Name: " << coffee.name << "\n PRICE RM: "
<< coffee.price << endl;
}

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.

Dont forget to submit the screen capture of your output to wanie.cfgs@gmail.com with the
subject: CMPD244 Lab3 <YourStudentID><Your Full Name> at the end of this lab session.
Good Luck!

Prepared by Nur Hanani Azami

2014

Das könnte Ihnen auch gefallen