structured data types array array union union struct struct class class

23
Structured Data Types array array union union struct struct class class

Upload: clinton-west

Post on 12-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structured Data Types array array union union struct struct class class

Structured Data Types

array array

unionunion

structstruct

classclass

Page 2: Structured Data Types array array union union struct struct class class

Data Types

a simple or atomicsimple or atomic

a structuredstructured

* *

char, int, float, double

array, union, struct, class

Page 3: Structured Data Types array array union union struct struct class class

Structured Data Types

array - array - homogeneoushomogeneous

struct - struct - heterogenousheterogenous

Page 4: Structured Data Types array array union union struct struct class class

Abstract Data TypeAbstract Data Type

ADT = a ADT = a programmer definedprogrammer defined data data type whose properties (domain type whose properties (domain [values] and operations) are [values] and operations) are specified independently of any specified independently of any particular implementation.particular implementation.

*

It has a It has a whatwhat and a and a howhow..

Page 5: Structured Data Types array array union union struct struct class class

Data Storage

charactercharacter

fieldfield

recordrecord

tabletable

databasedatabase

Page 6: Structured Data Types array array union union struct struct class class

Structured Data Types

struct = an abstract data type = an abstract data type

with a fixed number of with a fixed number of

components that are accessed components that are accessed

by name, not by index.by name, not by index.

Page 7: Structured Data Types array array union union struct struct class class

Structure Declaration

struct Date // customarily initial caps// customarily initial caps{

int month; // // int day; // data_type member_name// data_type member_nameint year; // //

};

Page 8: Structured Data Types array array union union struct struct class class

Structure Declaration

struct TypeName struct TypeName {{

MemberListMemberList // DataType// DataType

MemberNameMemberName}};;

}

Page 9: Structured Data Types array array union union struct struct class class

structure declaration

vs.

object declaration

(object (object variable) variable)

Page 10: Structured Data Types array array union union struct struct class class

Structure Declaration

struct Date{

int month; int day; int year;

};

Page 11: Structured Data Types array array union union struct struct class class

Object Instantiation

intint numnum;;doubledouble xx;;

DateDate myBirthmyBirth;;

DateDate todaytoday, , bill_Datebill_Date, , lily_Bdaylily_Bday;;

data type variable name

*

Page 12: Structured Data Types array array union union struct struct class class

Assigning Values

Date myBirth = {2, 29, 1963};

Date today = {4, 30, 2007};

Date bill_Date = {5, 4, 2007};

Date lily_Bday = {1, 20, 1985};

Page 13: Structured Data Types array array union union struct struct class class

Assigning Values

myBirth = {2, 29, 1963};

today = {4, 30, 2007};

bill_Date = {5, 4, 2007};

lily_Bday = {1, 20, 1985};

Page 14: Structured Data Types array array union union struct struct class class

Assigning Values

myBirth.month = 2;myBirth.day = 29;myBirth.year = 1963;

bill_Date.month = 5;bill_Date.day = 4;

lily_Bday.year = 1985;today.month = 4;

* *

instance of Date

member of myBirth instance

Page 15: Structured Data Types array array union union struct struct class class

element at_num at_mass densityboron (B) 5 10.811 2.34

tungsten (W) 74 183.85 19.30

iodine (I) 53 126.9 4.94

In Class Assignment 1

1. declare a structure for this table.2. instantiate variables of the structure type3. initialize the data for each element

(use two different methods)

Page 16: Structured Data Types array array union union struct struct class class

How to declare and instantiateHow to declare and instantiate#include <iostream> using namespace std;struct Element // the declaration of the struct{ char symbol; // note different data typesint at_num; // of membersdouble at_mass;double density;};main (){ Element boron; //an instantiation boron.symbol = 'B'; // initialize each member boron.at_num = 5; boron.at_mass = 10.811; boron.density = 2.34;

// instantiate and initialize all at once Element tungsten = {'W', 74, 183.85, 19.30}; Element iodine = {'I', 53, 126.9, 4.94}; cout << "Atomic mass of tungsten is " // print one piece

<< tungsten.at_mass << '\n'; }

Page 17: Structured Data Types array array union union struct struct class class

Assigning Values

// assign to a variable// assign to a variable

year = lily_Bday.year

new_mo = lily_Bday.month + 1

// assign contents of a variable to it// assign contents of a variable to it

lily_Bday.month = someMonth;

Page 18: Structured Data Types array array union union struct struct class class

Assigning Values

Date today, bill_date;

cout << “Enter month, day and year: “;cin >> today.month >>today.day

>> today.year;

bill_date = today; // an aggregate action// an aggregate action

*

Page 19: Structured Data Types array array union union struct struct class class

Structures as Arguments

// in a prototype// in a prototype

int Overdue(Date, int);

* * * *

// in a function call// in a function callcout << Overdue(today, bill_Date.year);

// in a function header// in a function headerint Overdue(Date now, int purchased)

Page 20: Structured Data Types array array union union struct struct class class

In Class Assignment 2

Element at. num. at. mass density

hydrogen (H) 1 1.008 0.071

fluorine (F) 9 18.998 1.505

4. Write the code for user input for H and F.5. Write the code to display data .6. Put each of these codes into separate

functions.

Page 21: Structured Data Types array array union union struct struct class class

In Class Assignment 27. Create a program that uses these functions.

a) What needs to be done?

declare (define) the structure declare the prototypes in main(): declare the objects call the functions

8. As a formatting exercise, display thedata neatly in a table.

a) What needs to be done?

* * ** *

a) What needs to be done?

7. Create a program that uses these functions.

a) What needs to be done?

Page 22: Structured Data Types array array union union struct struct class class

In Class Assign. 2-ans// display data - formatted// display data - formattedvoid showData(Element any_at) {cout << setiosflags(ios::fixed);cout << '\n' << setw(12) << any_at.symbol << setprecision(0) << setw(5) << any_at.at_num << setprecision(3) << setw(10) << any_at.at_mass << setw(10) << any_at.density;cout << endl;}

#7 b

Page 23: Structured Data Types array array union union struct struct class class

In Class Assign. 2-ans#include<iostream>#include<iomanip>Using namespace std;struct Element { char symbol; int at_num; double at_mass; double density;};

void getData(Element&); void showData(Element);

void main(){

Element hydrogen, fluorine;getData(hydrogen);getData(fluorine);

showData(hydrogen);showData(fluorine);

cout << endl<<endl;} // end main()// end main()