Anda di halaman 1dari 4

Structs

A structure is a user defined data type which allows you to combine related data items of different kinds
and manipulate it as if it were one unit.
Structures are used to represent a record. A record is a group of fields that are related to a
specific thing. Suppose you want to keep track of the accounts in a bank. You might want to store the
following attributes about each account record:
• Account Number
• Account Type
• Balance

Defining a structure
To define a structure, you must use the struct statement.
The struct statement defines a new data type. It usually contains more than one member or field. The
fields can have different types and different lengths.
The format of the struct statement in C++ is:

struct [type-name] {
data-type identifier-name;
data-type identifier-name;
...
Data-type identifier-name;
};

• type-name is the name of the structure type.

• Within the braces {}, there is a list of fields. Each field has a data type and a valid identifier name.
• The statement ends with a semicolon

For example,
struct Account {
int accNumber;
char accType;
double balance;
};

• The type name is Account.


• The fields are
o an integer account number (int accNumber),

o a character account type (char accType) and


o a double account balance (double balance).

This declares a structure type called Account.

1
Structs

It has three members: accNumber, accType and balance each of a different data type.
Note: Once Account is declared, it can used just like any other type.

Account

accNumber width

accType length

balance area

Account structure in memory with 3 “compartments”

Question 1
Define a structure Employee to store data on an employee. Each employee has an integer employee
number, a job type (‘M’: Manager, ‘C’: clerical, ‘S’: salesperson) and a monthly salary.
Employee
Solution: empNum

struct Employee {
int empNum;
char jobType; jobType
double salary;
};

salary
This code is written before the main function.

Declaring structure variables


We use the keyword int to declare an integer variable. In Question 1 we defined an Employee structure.
To declare a variable of type Employee we use the name of the structure (Employee) followed by the
variable name, e1, say. In order to declare that emp1, Mark, and John are Employee variables, the
following statements should be written before the each variable is used:
Employee emp1;
Employee John;
Employee Mark;

2
Structs

Writing Data to and Reading Data From a Structure


Once a structure variable is declared, we need to know how to put (or write) a value in the
corresponding memory location and how to get the value out (or read the value from the memory
location). This is done by using the dot operator (.) and assignment statements.
Generally, a field is specified by using the name of the variable that refers to the structure, followed by a
dot, followed by the name of the field. For example:
If we declare a variable acc of type Account, then the three fields in acc are:
Account acc;
acc.accNumber;
acc.accType;
acc.balance;

To assign values to each of these fields, we use statements such as the following:
acc.accNumber = 123455;
acc.accType ='S';
acc.balance = 15000;

Similarly to output the data from a structure to the monitor, we use the dot operator:
cout << "Account Number :" << acc.accNumber<< endl;
cout << "Account Type : " << acc.accType<< endl;
cout << "Account Balance : " << acc.balance << endl;

Question 2
Using the Employee structure definition from Question 1, create an Employee structure, e1. e1 has an
employee number of 345892, is a manager and has a salary of $15,000. Print all the data for e1
appropriately labeled.

Solution: Employee

Employee e1; // create an Employee e1 empNum


345892

//Assign values to the fields of e1 jobType


e1. empNum = 345892; M
e1. jobType = 'M';
e1. salary = 15000;
salary
15000
//print the data in e1
cout << "******Data for e1*****" << endl;
cout << "Employee Number : " << e1.empNum<< endl;
cout << "Job Type : " << e1.jobType << endl;
cout << "Employee Salary : $" << e1.salary<< endl;

3
Structs

Question 3
Create another Employee structure, e2. Prompt the user for e2’s employee number, job type (M, C, or S)
and salary. Print all the data for e2. For the employee’s job type, print the name of the job type i.e., if
the job type is ‘M’ then print Manager, etc.

Solution:
Employee e2;
int eNum;
char type;
double s;

//prompt the user for the data


cout << "Enter the employee number: ";
cin >> eNum;
cout << "Enter the job type ('M', 'C' or 'S'): "
cin >> type;
cout << "Enter the salary: ";
cin >> s;

e2.empNum = eNum;
e2.jobType = type;
e2.salary = s;

Alternatively we could read the data immediately into the fields of the structure. For example,
//prompt the user for the data
cout << "Enter the employee number: ";
cin >> e2.empNum;
cout << "Enter the job type ('M', 'C' or 'S'): ";
cin >> e2.jobType;
cout << "Enter the salary: ";
cin >> e2.salary;

//print the data in e2


cout << "******Data for e2*****" << endl;
cout << "Employee Number : "<< e2.empNum << endl;
cout << "Job Type :";
if (e2.jobType == 'M')
cout << " Manager" << endl;
else
if (e2.jobType == 'S')
cout << " Salesperson" << endl;
else
cout << " Clerical” << endl;
cout << "Employee Salary : $" << e2.salary << endl;

Anda mungkin juga menyukai