Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Data Types array array union union struct struct class class.

Similar presentations


Presentation on theme: "Structured Data Types array array union union struct struct class class."— Presentation transcript:

1

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

3 Data Types a simple or atomic a structured * char, int, float, double array, union, struct, class

4 Structured Data Types array - homogeneous struct - heterogenous

5 Abstract Data Type ADT = a programmer defined defined data type whose properties (domain [values] and operations) are specified independently of any particular implementation. * It has a what and a how.

6 Data Storage character field field record record table table database database

7 Structured Data Types = an abstract data type with a fixed number of components that are accessed by name, not by index. struct = an abstract data type with a fixed number of components that are accessed by name, not by index.

8 Structure Declaration // customarily initial caps struct Date// customarily initial caps { // int month; // // data_type member_name int day; // data_type member_name // int year; // };

9 Structure Declaration struct TypeName { MemberList // DataType MemberName };};};}; }

10 structure declaration vs. object declaration (object variable) (object  variable)

11 Structure Declaration struct Date { int month; int day; int year; };

12 Object Instantiation int num; double x; Date myBirth; Date today, bill_Date, lily_Bday; data type variable name *

13 Assigning Values Date myBirth = {2, 29, 1963}; Date today = {4, 30, 2007}; Date bill_Date = {5, 4, 2007}; Date lily_Bday = {1, 20, 1985};

14 Assigning Values myBirth = {2, 29, 1963}; today = {4, 30, 2007}; bill_Date = {5, 4, 2007}; lily_Bday = {1, 20, 1985};

15 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

16 elementat_numat_massdensity boron (B) 510.8112.34 tungsten (W) 74183.8519.30 iodine (I)53126.94.94 In Class Assignment 1 1.declare a structure for this table. 2. instantiate variables of the structure type 3.initialize the data for each element (use two different methods)

17 How to declare and instantiate #include using namespace std; struct Element// the declaration of the struct { char symbol;// note different data types int at_num;// of members double 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';}

18 Assigning Values // assign to a variable year = lily_Bday.year new_mo = lily_Bday.month + 1 // assign contents of a variable to it lily_Bday.month = someMonth;

19 Assigning Values Date today, bill_date; cout << “Enter month, day and year: “; cin >> today.month >>today.day >> today.year; // an aggregate action bill_date = today;// an aggregate action *

20 Structures as Arguments // in a prototype int Overdue(Date, int); * * * * // in a function call cout << Overdue(today, bill_Date.year); // in a function header int Overdue(Date now, int purchased)

21 In Class Assignment 2 Elementat. num.at. massdensity 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.

22 In Class Assignment 2 7.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 the data 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?

23 In Class Assign. 2-ans // display data - formatted void 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

24 In Class Assign. 2-ans #include 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()


Download ppt "Structured Data Types array array union union struct struct class class."

Similar presentations


Ads by Google