Object Oriented Programming C++ Lecture No:2


  • Structures (Refreshing the Concepts)

Combining Data into Structures

  • C++ allows you to group several variables of relevance into a single item known as structure.
  • e.g., we can group all the variables relevant to a student into a group named Student.

Structure Example: Payroll

Two Steps to Implementing Structures

  • Create the structure declaration. This establishes the tag (or name) of the structure and a list of items that are members.
  • Declare variables (or instances) of the structure and use them in the program to hold data.

Accessing Structure Members

  • The dot operator (.) allows you to access structure members in a program

Structure Example: Payroll

// This program demonstrates the use of structures.

Output Like:

Enter the employee’s number: 489 [Enter]
Enter the employee’s name: Jill Smith [Enter]
How many hours did the employee work? 40 [Enter]
What is the employee’s hourly payrate? 20 [Enter]
Here is the employee’s payroll data:
Name: Jill Smith
Number: 489
Hours worked: 40
Hourly Payrate: 20
Gross Pay: Rs800.00

Displaying a Structure

  • The contents of a structure variable cannot be displayed by passing he entire variable to cout. For example, assuming employee is a PayRoll structure variable, the following statement will not work:

cout << employee << endl; //won’t work!

Structure Example: Circle

// This program uses a structure to hold geometric data about a circle.



Enter the diameter of a circle: 10 [Enter]
The radius and area of the circle are:
Radius: 5
Area: 78.54

Strings as Structure Members

  • When a character array is a structure member, use the same string manipulation techniques with it as you would with any other character array.

Structure Example: Name


Enter your first name: Johan [Enter]
Enter your middle name: Smith [Enter]
Enter your last name: X [Enter]

Your full name is Johan Smith X

Initializing a Structure

  • The members of a structure variable may be initialized with starting values when the structure variable is declared.
  • struct GeoInfo
    char cityName[30];
    char state[3];
    long population;
    int distance;
    GeoInfo location = {“Ashville”, “NC”, 50000, 28};

Arrays of Structures

  • Arrays of structures can simplify some programming tasks.
  • struct BookInfo
    char title[50];
    char author[30];
    char publisher[25];
    float price;
    BookInfo bookList[20];

Structure Example: PayInfo


Enter the hours worked by 5 employees and
their hourly rates.
Hours worked by employee #1: 10 [Enter]
Hourly pay rate for employee #1: 9.75 [Enter]
Hours worked by employee #2: 15 [Enter]
Hourly pay rate for employee #2: 8.62 [Enter]
Hours worked by employee #3: 20 [Enter]
Hourly pay rate for employee #3: 10.50 [Enter]
Hours worked by employee #4: 40 [Enter]
Hourly pay rate for employee #4: 18.75 [Enter]
Hours worked by employee #5: 40 [Enter]
Hourly pay rate for employee #5: 15.65 [Enter]
Here is the gross pay for each employee:
Employee #1: $97.50
Employee #2: $129.30
Employee #3: $210.00
Employee #4: $750.00
Employee #5: $626.00

Initializing a Structure Array

PayInfo workers[5] = {{ 10, 9.75f},
{15, 8.62f},
{20, 10.50f},
{40, 18.75f},
{40, 15.65f}};

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × 3 =