Chapter 10Overview10.1 StructuresWhat Is a Class?Class DefinitionsStructuresThe CD DefinitionUsing the StructureThe Structure ValueSpecifying Member VariablesUsing Member VariablesSlide 12Slide 13Slide 14Duplicate NamesStructures as ArgumentsStructures as Return TypesUsing Function shrink_wrapAssignment and StructuresHierarchical StructuresUsing PersonInfoInitializing Classes10.2 ClassesClassesA Class ExampleClass DayOfYear DefinitionDefining a Member FunctionMember Function DefinitionThe ‘::’ Operator‘::’ and ‘.’Calling Member FunctionsSlide 32Slide 33EncapsulationProblems With DayOfYearIdeal Class DefinitionsFixing DayOfYearPublic Or Private?Private VariablesPublic or Private MembersA New DayOfYearSlide 42Slide 43Using Private VariablesGeneral Class DefinitionsDeclaring an ObjectThe Assignment OperatorProgram Example: BankAccount ClassSlide 49Slide 50Slide 51Slide 52Calling Public MembersCalling Private MembersConstructorsConstructor DeclarationConstructor DefinitionCalling A Constructor (1)Calling A Constructor (2)Overloading ConstructorsThe Default ConstructorDefault Constructor DefinitionCalling the Default ConstructorSlide 64Slide 65Slide 66Initialization SectionsParameters and Initialization10.3 Abstract Data TypesAbstract Data TypesClasses To Produce ADTsADT InterfaceADT ImplementationADT BenefitsProgram Example The BankAccount ADTSlide 76Slide 77Slide 78Interface PreservationInformation HidingChapter 10Defining ClassesSlide 10- 2Overview10.1 Structures 10.2 Classes10.3 Abstract Data Types10.1 StructuresSlide 10- 4What Is a Class?A class is a data type whose variables are objectsSome pre-defined classes you have used are –int–char–ifstreamYou can define your own classes as wellSlide 10- 5Class DefinitionsA class definition includes–A description of the kinds of values the variable can hold–A description of the member functionsWe will start by defining structures as a firststep toward defining classesSlide 10- 6StructuresA structure can be viewed as an object–Contains no member functions (The structures used here have no member functions)–Contains multiple values of possibly different typesThe multiple values are logically related as a single itemExample: A bank Certificate of Deposit (CD) has the following values: a balance an interest ratea term (months to maturity)Slide 10- 7The Certificate of Deposit structure can bedefined asstruct CDAccount { double balance;double interest_rate; int term; //months to maturity};Keyword struct begins a structure definitionCDAccount is the structure tag or the structure’s type Member names are identifiers declared in the bracesThe CD DefinitionRemember this semicolon!Slide 10- 8Using the StructureStructure definition is generally placed outsideany function definition–This makes the structure type available to all code that follows the structure definitionTo declare two variables of type CDAccount: CDAccount my_account, your_account;–My_account and your_account contain distinct member variables balance, interest_rate, and termSlide 10- 9The Structure ValueThe Structure Value–Consists of the values of the member variablesThe value of an object of type CDAccount–Consists of the values of the member variables balance interest_ratetermSlide 10- 10Specifying Member VariablesMember variables are specific to the structure variable in which they are declared–Syntax to specify a member variable:Structure_Variable_Name . Member_Variable_Name–Given the declaration: CDAccount my_account, your_account;Use the dot operator to specify a member variablemy_account.balance my_account.interest_ratemy_account.termSlide 10- 11Member variables can be used just as any othervariable of the same type–my_account.balance = 1000;your_account.balance = 2500;Notice that my_account.balance and your_account.balance are different variables!–my_account.balance = my_account.balance + interest;Using Member VariablesSlide 10- 15Member variable names duplicated between structure types are not a problem. super_grow.quantity and apples.quantity are different variables stored in different locationsstruct FertilizerStock{ double quantity; double nitrogen_content;};FertilizerStock super_grow;struct CropYield{ int quantity; double size;};CropYield apples;Duplicate NamesSlide 10- 16Structures as ArgumentsStructures can be arguments in function calls–The formal parameter can be call-by-value–The formal parameter can be call-by-referenceExample:void get_data(CDAccount& the_account);–Uses the structure type CDAccount we saw earlier as the type for a call-by-reference parameterSlide 10- 17Structures as Return TypesStructures can be the type of a value returned bya functionExample:CDAccount shrink_wrap(double the_balance, double the_rate, int the_term){ CDAccount temp; temp.balance = the_balance; temp.interest_rate = the_rate; temp.term = the_term; return temp;}Slide 10- 18Using Function shrink_wrapshrink_wrap builds a complete structure value in temp, which is returned by the functionWe can use shrink_wrap to give a variable of type CDAccount a value in this way: CDAccount new_account; new_account = shrink_wrap(1000.00, 5.1, 11);Slide 10- 19Assignment and StructuresThe assignment operator can be used to assignvalues to structure typesUsing the CDAccount structure again:CDAccount my_account, your_account;my_account.balance = 1000.00;my_account.interest_rate = 5.1;my_account.term = 12;your_account = my_account;–Assigns all member variables in your_account the corresponding values in my_accountSlide 10- 20Structures can contain member variables that are also structuresstruct PersonInfo contains a Date structurestruct Date{ int month; int day; int year;};struct PersonInfo{ double height; int weight; Date birthday;};Hierarchical StructuresSlide 10- 21Using PersonInfoA variable of type PersonInfo is declared by PersonInfo person1;To display the birth year of person1, first access the birthday member of person1 cout << person1.birthday…But we want the year, so we now specify the year member of the birthday member cout << person1.birthday.year;Slide 10- 22A structure can be initialized when declaredExample: struct Date{ int month; int day; int year;};–Can be initialized in this way Date due_date = {12, 31,
View Full Document