Programming in c++ Structured Types, Data Abstraction and Classes Dale/eems/Headington
1 Structured Types, Data Abstraction and Classes
Programming in C++ Chapter 11 Topics 8 Meaning of a Structured Data Type o Declaring and Using a struct Data Type s C++ union Data Type s 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 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
Programming in C++ C++ Data Types simple structured integral enum floating array struct union class char short int long bool float double long double address pointer reference
3 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double
Programming in C++ 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 o each component can be accessed individually
4 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
Programming in C++ C++ Structured Type % often we have related information of various types that we'd like to store together for convenient access under the same identifier, for example
5 C++ Structured Type ❖often we have related information of various types that we’d like to store together for convenient access under the same identifier, for example . .
Programming in C++ thisAnimal 5000 2037581 name “ giant panda genus Ailuropoda species“ melanoluka” country"China .age 18 weight 234.6 health Good
6 thisAnimal 5000 .id 2037581 .name “giant panda” .genus “Ailuropoda” .species “melanoluka” .country “China” .age 18 .weight 234.6 .health Good
Programming in C++ anotherAnimal 6000 5281003 name llama genus “Lama .species"peruana country"Peru .age weight 278.5 health Excellent
7 anotherAnimal 6000 .id 5281003 .name “llama” .genus “Lama” .species “peruana” .country “Peru” .age 7 .weight 278.5 .health Excellent
Programming in C++ struct AnimalT'ype enum HealthType Poor, Fair, Good, Excellent) struct Animaltype // declares a struct data type M does not allocate memory long id string name 3 string genus 3 string species 3 struct members string country 3 int age float weight: Health Type health AnimalType thisAnimal; //declare variables of AnimalType AnimalType anotherAnimal; 8
8 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 ; } ; AnimalType thisAnimal ; // declare variables of AnimalType AnimalType anotherAnimal ;
Programming in C++ struct type Declaration SYNTAX struct TypeName /does not allocate memory Mem berlist } ∥ ends with a semicolon Mem berList SYNTAX Data Type Mem benAme; Data Type Mem benAme 9
9 struct type Declaration SYNTAX struct TypeName // does not allocate memory { MemberList } ; // ends with a semicolon MemberList SYNTAX DataType MemberName ; DataType MemberName ; . .
Programming in C++ struct type Declaration The struct declaration names a type and names the members of the struct Note that each member name is given a type Also, member names must be unique within a struct type y It does not allocate memory when you make a struct declaration None of the struct members are associated with memory until we declare a struct variable 10
10 struct type Declaration ➢ The struct declaration names a type and names the members of the struct. Note that each member name is given a type. Also,member names must be unique within a struct type. ➢ It does not allocate memory when you make a struct declaration. ➢ None of the struct members are associated with memory until we declare a struct variable