Download presentation
Presentation is loading. Please wait.
1
Structured Data Types array union struct class
2
Data Types simple or atomic structured char, int, float, double
array, union, struct, class * *
3
Structured Data Types array - homogeneous struct - heterogenous
4
Abstract Data Type ADT = a programmer defined data type whose properties (domain [values] and operations) are specified independently of any particular implementation. It has a what and a how. *
5
Data Storage character field record table database
6
Structured Data Types struct = an abstract data type with a fixed number of components that are accessed by name, not by index.
7
Structure Declaration
struct Date // customarily initial caps { int month; // int day; // data_type member_name int year; // };
8
Structure Declaration
struct TypeName { MemberList // DataType MemberName }; }
9
structure declaration
vs. object declaration (object variable)
10
Structure Declaration
struct Date { int month; int day; int year; };
11
Object Instantiation int num; double x; Date myBirth;
variable name data type Date myBirth; Date today, bill_Date, lily_Bday; *
12
Assigning Values Date myBirth = {2, 29, 1963};
Date today = {11, 25, 1999}; Date bill_Date = {3, 25, 2001}; Date lily_Bday = {1, 20, 1995};
13
Assigning Values myBirth = {2, 29, 1963}; today = {11, 25, 1999};
bill_Date = {3, 25, 2001}; lily_Bday = {1, 20, 1995};
14
member of myBirth instance
Assigning Values member of myBirth instance instance of Date myBirth.month = 2; myBirth.day = 29; myBirth.year = 1963; bill_Date.month = 3; bill_Date.day = 25; lily_Bday.year = 1995; today.month = 4; * *
15
In Class Assignment 1 element at_num at_mass density boron (B) 5
10.811 2.34 tungsten (W) 74 183.85 19.30 iodine (I) 53 126.9 4.94 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)
16
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;
17
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 *
18
Structures as Arguments
// in a prototype int Overdue(Date, int); // in a function call Overdue(today, bill_Date.year); // in a function header int Overdue(Date now, int purchased) * * * *
19
In Class Assignment 2 Element at. num. at. mass density
hydrogen (H) fluorine (F) 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. (Call the object atom.)
20
In Class Assignment 2 7. Create a program that uses these functions.
a) What needs to be done? 7. Create a program that uses these functions. a) What needs to be done? a) What needs to be done? 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. * * * * *
21
In Class Assign. 2-ans #7 b // 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 << setprecision(3) << setw(10) << any_at.density; cout << endl; }
22
In Class Assign. 2-ans #include<iostream.h>
#include<iomanip.h> 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()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.