DOC PREVIEW
GSU CSC 2311 - ch10

This preview shows page 1-2-3-4-5-38-39-40-41-42-43-76-77-78-79-80 out of 80 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 80 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 objectsSome pre-defined classes you have used are –int–char–ifstreamYou can define your own classes as wellSlide 10- 5Class DefinitionsA class definition includes–A description of the kinds of values the variable can hold–A description of the member functionsWe will start by defining structures as a firststep toward defining classesSlide 10- 6StructuresA 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 typesThe multiple values are logically related as a single itemExample: A bank Certificate of Deposit (CD) has the following values: a balance an interest ratea term (months to maturity)Slide 10- 7The Certificate of Deposit structure can bedefined asstruct CDAccount { double balance;double interest_rate; int term; //months to maturity};Keyword struct begins a structure definitionCDAccount 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 StructureStructure definition is generally placed outsideany function definition–This makes the structure type available to all code that follows the structure definitionTo 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 ValueThe Structure Value–Consists of the values of the member variablesThe value of an object of type CDAccount–Consists of the values of the member variables balance interest_ratetermSlide 10- 10Specifying Member VariablesMember 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- 11Member 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- 15Member 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 ArgumentsStructures can be arguments in function calls–The formal parameter can be call-by-value–The formal parameter can be call-by-referenceExample: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 TypesStructures can be the type of a value returned bya functionExample: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_wrapshrink_wrap builds a complete structure value in temp, which is returned by the functionWe 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 StructuresThe assignment operator can be used to assignvalues to structure typesUsing 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- 20Structures can contain member variables that are also structuresstruct PersonInfo contains a Date structurestruct Date{ int month; int day; int year;};struct PersonInfo{ double height; int weight; Date birthday;};Hierarchical StructuresSlide 10- 21Using PersonInfoA 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- 22A structure can be initialized when declaredExample: struct Date{ int month; int day; int year;};–Can be initialized in this way Date due_date = {12, 31,


View Full Document

GSU CSC 2311 - ch10

Documents in this Course
ch14

ch14

65 pages

ch13

ch13

68 pages

ch06

ch06

110 pages

Load more
Download ch10
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ch10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ch10 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?