lecture 18: structured data professor: dr. miguel alonso jr. fall 2008 cgs2423/cop1220

22
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Upload: myra-hardy

Post on 28-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Lecture 18: Structured Data

Professor: Dr. Miguel Alonso Jr.

Fall 2008

CGS2423/COP1220

Abstract Data Types

Concept: Abstract data types (ADTs) are data types created by the programmer

They have their own range (or domain) of data and their own sets of operations that may be performed on them

Abstraction

A general model of something Includes only the general characteristics Example: a dog

captures the essence without specifying details However, a real life dog is not abstract, it is

concrete Example: poodle

Data types

C++ has primitive data types bool, char, unsigned char, short int int, long int, unsigned short int, unsigned int unsigned long int, float, double, long double

A data type defines what a variable can hold and its range Data types also define what operations can be performed

on them + - * / > < >= <= == !=

int x = 1; int is the abstract data type x is the concrete occurrence

Abstract data types

Data type created by the programmer Composed of one or more primitive data

types Programmer decides what values are

acceptable for the data type, as well as what operations may be performed on the data type

Combining Data into Structures

C++ allows you to group several variable together into a single item know as a structure

struct tag{ variable declaration; // more declarations, etc.};

const int SIZE = 25; // array size

struct PayRoll{

int empNumber; // Employee numberchar name[SIZE]; // Employee’s namedouble hours; // Hours workeddouble payRate; // Hourly pay ratedouble grossPay; // Gross Pay

};

Tips

Semicolon (;) is needed after the closing curly bracket of the structure declaration

Name of the structure tag begins with an uppercase letter (convention used in the book)

You can declare variables of the same type on a single line as before

This does not define a variable, simply tells the compiler what a PayRoll structure is made of

This defines a variable of type PayRoll PayRoll deptHead;

Accessing structure members

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

deptHead.empNumber = 475; cout << deptHead.name << endl;

This however, does not work! cout << deptHead << endl;

// This program stores data about a circle in a structure.#include <iostream>#include <cmath> // For the pow function#include <iomanip>using namespace std;

// Constant for Pi.const double PI = 3.14159;

// Structure declarationstruct Circle{ double radius; // A circle's radius double diameter; // A circle's diameter double area; // A circle's area};

int main(){ Circle c; // Define a structure variable

// Get the circle's diameter. cout << "Enter the diameter of a circle: "; cin >> c.diameter;

// Calculate the circle's radius. c.radius = c.diameter / 2;

// Calculate the circle's area. c.area = PI * pow(c.radius, 2.0);

// Display the circle data. cout << fixed << showpoint << setprecision(2); cout << "The radius and area of the circle are:\n"; cout << "Radius: " << c.radius << endl; cout << "Area: " << c.area << endl; return 0;}

Comparing Structure Values

Just like arrays, we cannot perform comparison operations directly on structure variables if (circle1 == circle2) // Error! if (circle1.radius == circle2.radius &&

circle1.diameter == circle2.diameter && circle1.area = circle2.area)

Strings as structure members

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

Initializing a structure

Concept: The members of a structure variable may be initialized with starting values when the structure variable is defined

No Skipping! struct CityInfo{

char cityName[30];char state[3];long population;int distance;

};

CityInfo location = {“Asheville”, “NC”, 50000, 28};CityInfo location = {“Asheville”, “NC”};CityInfo location = {“Asheville”, “NC”, 50000};

Arrays of Structures

Concept: Arrays of structures simply some programming tasks

struct BookInfo{

char title[50];char author[30];char publisher[25];double price;

};

BookInfo bookList[20];cout << bookList[5].title;cout << bookList[10].title[0];for ( int index = 0; index < 20; index++){

cout << bookList[index].title;}

Initializing a Structure Array

PayInfo workers[NUM_WORKERS] = {{10, 9.75},{15, 8.62},{20, 10.5},{40, 15.65}

}

Nested Structures

Concept: It is possible for a structure variable to be a member of another structure

struct Costs{

double wholesale;double retail;

};

struct Item{

char partNum[10];char description[25];Costs pricing;

}

Structures as function arguments, and function returns Concept: Structure variables may be passed

as arguments to functions Concept: A function may return a structure