Chapter 6Learning ObjectivesStructuresStructure TypesDeclare Structure VariableAccessing Structure MembersStructure Example: Display 6.1 A Structure Definition (1 of 3)Structure Example: Display 6.1 A Structure Definition (2 of 3)Structure Example: Display 6.1 A Structure Definition (3 of 3)Structure PitfallStructure AssignmentsStructures as Function ArgumentsInitializing StructuresClassesClass DefinitionsDeclaring ObjectsClass Member AccessClass Member FunctionsClass Member Functions DefinitionComplete Class Example: Display 6.3 Class With a Member Function (1 of 4)Complete Class Example: Display 6.3 Class With a Member Function (2 of 4)Complete Class Example: Display 6.3 Class With a Member Function (3 of 4)Complete Class Example: Display 6.3 Class With a Member Function (4 of 4)Dot and Scope Resolution OperatorA Class’s PlaceEncapsulationAbstract Data TypesMore EncapsulationPrinciples of OOPPublic and Private MembersPublic and Private ExamplePublic and Private Example 2Public and Private StyleAccessor and Mutator FunctionsSeparate Interface and ImplementationStructures versus ClassesThinking ObjectsSummary 1Summary 2Chapter 6Structures and ClassesCopyright © 2008 Pearson Addison-Wesley. All rights reservedLearning Objectives•Structures–Structure types–Structures as function arguments–Initializing structures•Classes–Defining, member functions–Public and private members–Accessor and mutator functions–Structures vs. classes6-2Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structures•2nd aggregate data type: struct•Recall: aggregate meaning "grouping"–Recall array: collection of values of same type–Structure: collection of values of different types•Treated as a single item, like arrays•Major difference: Must first "define" struct–Prior to declaring any variables6-3Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structure Types•Define struct globally (typically)•No memory is allocated–Just a "placeholder" for what our struct will "look like"•Definition:struct CDAccountV1 Name of new struct "type"{double balance; member namesdouble interestRate;int term;};6-4Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Declare Structure Variable•With structure type defined, now declarevariables of this new type:CDAccountV1 account;–Just like declaring simple types–Variable account now of type CDAccountV1–It contains "member values"•Each of the struct "parts"6-5Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Accessing Structure Members•Dot Operator to access members–account.balance–account.interestRate–account.term•Called "member variables"–The "parts" of the structure variable–Different structs can have same name member variables•No conflicts6-6Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structure Example: Display 6.1 A Structure Definition (1 of 3)6-7Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structure Example: Display 6.1 A Structure Definition (2 of 3)6-8Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structure Example: Display 6.1 A Structure Definition (3 of 3)6-9Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structure Pitfall•Semicolon after structure definition–; MUST exist:struct WeatherData{double temperature;double windVelocity;}; REQUIRED semicolon!–Required since you "can" declare structurevariables in this location6-10Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structure Assignments•Given structure named CropYield•Declare two structure variables:CropYield apples, oranges;–Both are variables of "struct type CropYield"–Simple assignments are legal:apples = oranges;•Simply copies each member variable from applesinto member variables from oranges6-11Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Structures as Function Arguments•Passed like any simple data type–Pass-by-value–Pass-by-reference–Or combination•Can also be returned by function–Return-type is structure type–Return statement in function definitionsends structure variable back to caller6-12Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Initializing Structures•Can initialize at declaration–Example:struct Date{int month;int day;int year;};Date dueDate = {12, 31, 2003};–Declaration provides initial data to all three member variables6-13Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Classes•Similar to structures–Adds member FUNCTIONS–Not just member data•Integral to object-oriented programming–Focus on objects•Object: Contains data and operations•In C++, variables of class type are objects6-14Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Class Definitions•Defined similar to structures•Example:class DayOfYear name of new class type{public:void output(); member function!int month;int day;};•Notice only member function’s prototype–Function’s implementation is elsewhere6-15Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Declaring Objects•Declared same as all variables–Predefined types, structure types•Example:DayOfYear today, birthday;•Declares two objects of class type DayOfYear•Objects include:–Data•Members month, day–Operations (member functions)•output()6-16Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Class Member Access•Members accessed same as structures•Example:today.monthtoday.day–And to access member function:today.output(); Invokes member function6-17Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Class Member Functions•Must define or "implement" class memberfunctions•Like other function definitions–Can be after main() definition–Must specify class:void DayOfYear::output(){…}•:: is scope resolution operator•Instructs compiler "what class" member is from•Item before :: called type qualifier6-18Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Class Member Functions Definition•Notice output() member function’sdefinition (in next example)•Refers to member data of class–No qualifiers•Function used for all objects of the class–Will refer to "that object’s" data when invoked–Example:today.output();•Displays "today" object’s data6-19Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Complete Class Example: Display 6.3 Class With a Member Function
View Full Document