Structured Types - Problem Solving and Structured Programming - Lecture Slides, Slides of Advanced Computer Programming

During the course of work of the programming, we learn the core of the programming. The main points disucss in these lecture slides are:Structured Types, Data Abstraction, Abstract Data Type, Separate Specification, Implementation Files, Member Functions in Client Code, Allocate Memory, Member Selection Operator, Aggregate Operation, Unions in C

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banamala
banamala šŸ‡®šŸ‡³

4.4

(19)

114 documents

1 / 57

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 11
Structured
Types,
Data Abstraction
and Classes
1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39

Partial preview of the text

Download Structured Types - Problem Solving and Structured Programming - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 11

Structured

Types,

Data Abstraction

and Classes

1

Chapter 11 Topics

• Meaning of a Structured Data Type

• Declaring and Using a struct Data Type

• C++ union Data Type

• Meaning of an Abstract Data Type

• Declaring and Using a class Data Type

• Using Separate Specification and

Implementation Files

• Invoking class Member Functions in Client

Code

• C++ class Constructors

2

Structured Data Type

A structured data type is a type in which each

value is a collection of component items

  • The entire collection has a single name
  • Each component can be accessed individually
  • Used to bundle together related data of various types for convenient access under the same identifier

For example...

4

thisAnimal

5

.id 2037581 .name ā€œgiant pandaā€ .genus ā€œAiluropodaā€ .species ā€œmelanolukaā€ .country ā€œChinaā€ .age 18 .weight 234. .health Good

struct AnimalType

enum HealthType { Poor, Fair, Good, Excellent }; struct AnimalType // Declares a struct data type { // does not allocate memory long id; string name; string genus; string species; struct members string country; int age; float weight; HealthType health; }; // Declare variables of AnimalType AnimalType thisAnimal; AnimalType anotherAnimal; 7 7

struct type Declaration

SYNTAX

struct TypeName // Does not allocate memory { MemberList };

MemberList SYNTAX

DataType MemberName; DataType MemberName; . . . 8

More about struct type declarations

Scope of a struct

  • If the struct type declaration precedes all functions, it will be visible throughout the rest of the file
  • If it is placed within a function, only that function can use it

It is common to place struct type declarations in a (.h)

header file and #include that file

It is possible for members of different struct types to have

the same identifiers; also a non-struct variable may have

the same identifier as a structure member

10

Accessing struct Members

Dot (period) is the member selection operator

After the struct type declaration, the various members can be

used in your program only when they are preceded by a

struct variable name and a dot

EXAMPLES

thisAnimal.weight

anotherAnimal.country

11

Aggregate Operation

An aggregation operation is an operation

on a data structure as a whole, as opposed

to an operation on an individual

component of the data structure

13

Aggregate struct Operations

• Operations valid on struct type variables are

ļ‚§ Assignment to another struct variable of the same type

ļ‚§ Pass as an argument (by value or by reference)

ļ‚§ Return as value of a function

• I/O, arithmetic, and comparisons of entire struct

variables are NOT ALLOWED!

14

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); } 16 Docsity.com

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

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...

19

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)

20