Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.

Similar presentations


Presentation on theme: "1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington."— Presentation transcript:

1 1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington

2 2 Chapter 11 Topics l Meaning of a Structured Data Type Declaring and Using a struct Data Type C++ union Data Type l Meaning of an Abstract Data Type Declaring and Using a class Data Type l Using Separate Specification and Implementation Files Invoking class Member Functions in Client Code C++ class Constructors

3 3 Revising Lecture 24 l What are the structured data types in C++? l One data value==>simple data type l Multiple data values with a common name==>structured data type l How do we declare members of a structure?

4 4 Revising Lecture 24 l Can we start using the members once we have declared the structure? l Once we have defined a variable of the declared structure type, how can we access its members? l Are the following operations allowed on the struct variable current?

5 5 Revising Lecture 24 l struct time l { l int hours; l int minutes; l int seconds; l char ampm; l }; l time current; l cout<<current; l current = current*3;

6 6 Examples of aggregate struct operations anotherAnimal = thisAnimal ; // assignment WriteOut(thisAnimal); // value parameter ChangeWeightAndAge(thisAnimal); // reference parameter thisAnimal = GetAnimalData( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE...

7 7 void WriteOut( /* in */ AnimalType thisAnimal) // Prints out values of all members of thisAnimal // Precondition: all members of thisAnimal are assigned // Postcondition: all members have been written out { cout << “ID # “ << thisAnimal.id << thisAnimal.name << endl ; cout << thisAnimal.genus << thisAnimal.species << endl ; cout << thisAnimal.country << endl ; cout << thisAnimal.age << “ years “ << endl ; cout << thisAnimal.weight << “ lbs. “ << endl ; cout << “General health : “ ; WriteWord ( thisAnimal.health ) ; }

8 8 void ChangeAge ( /* inout */ AnimalType& thisAnimal ) // Adds 1 to age // Precondition: thisAnimal.age is assigned // Postcondition: thisAnimal.age == thisAnimal.age@entry + 1 { thisAnimal.age++ ; } Passing a struct Type by Reference

9 9 AnimalType GetAnimalData ( void ) // Obtains all information about an animal from keyboard // Postcondition: // Function value == AnimalType members entered at kbd { AnimalType thisAnimal ; char response ; do {// have user enter all members until they are correct. } while (response != ‘Y’ ) ; return thisAnimal ; }

10 10 Class Exercise l Use the time struct to declare a variable called current and then allocate the various fields by passing it to a function. That function should request the user to input the values

11 11 Solution l #include l using namespace std; l struct time l { l int hours; l int minutes; l int seconds; l char ampm; l };

12 12 Solution l void timetake(time&); l void main() l { l time now; l timetake(now); l cout<<now.hours<<endl; l }

13 13 Solution l void timetake(time& current) l { l cout<<"hours"; l cin>>current.hours; l }

14 14 Class Exercise l You have your personal library of 1000 books at home. You are asked to write a program to computerize the catalogue. You define an array of structures and input all the data. Now you are asked to implement a search function that searches for a book by its catalogue number (an exact match is required)

15 15 Hierarchical Structures The type of a struct member can be another struct type. This is called nested or hierarchical structures. Hierarchical structures are very useful when there is much detailed information in each record. FOR EXAMPLE...

16 16 struct MachineRec Information about each machine in a shop contains: an idNumber, a written description, the purchase date, the cost, and a history (including failure rate, number of days down, and date of last service).

17 17 struct DateType {int month ; // Assume 1.. 12 int day ;// Assume 1.. 31 int year ; // Assume 1900.. 2050 }; struct StatisticsType {floatfailRate ; DateTypelastServiced ; // DateType is a struct type intdownDays ; } ; struct MachineRec {int idNumber ; string description ; StatisticsType history ; // StatisticsType is a struct type DateType purchaseDate ; float cost ; } ; MachineRec machine ;

18 18 struct type variable machine 7000.idNumber.description. history.purchaseDate.cost.month.day.year 5719 “DRILLING…” 3 21 1995 8000.0.failrate.lastServiced.downdays.02 1 25 1999 4.month.day.year machine.history.lastServiced.year has value 1999


Download ppt "1 Lecture 25 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington."

Similar presentations


Ads by Google