Defining A Structure: Structs
Defining A Structure: Structs
Defining A Structure: 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;
};
• 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;
};
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
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.
2
Structs
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
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;
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;