DOC PREVIEW
Yale CPSC 427 - Lecture 8
School name Yale University
Pages 18

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 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 18 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 18 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 18 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 18 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 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineStorage ManagemetBells and WhistlesClassesOutline Storage Managemet Bells and Whistles ClassesCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 8September 27, 2011CPSC 427a, Lecture 8 1/18Outline Storage Managemet Bells and Whistles ClassesStorage ManagemetBells and WhistlesClassesCPSC 427a, Lecture 8 2/18Outline Storage Managemet Bells and Whistles ClassesStorage managementCPSC 427a, Lecture 8 3/18Outline Storage Managemet Bells and Whistles ClassesVariables and storageAn ordinary variable consists of three parts:A type, a name and a storage register.IThe type determines the size and encoding of the storageregister.IThe name is used to access the storage register.IThe storage register is a machine register long enough to holdany of the legal values of the specified type.CPSC 427a, Lecture 8 4/18Outline Storage Managemet Bells and Whistles ClassesExample of a variableDeclaration: int n = 123;This declares a variable of type int, name n, and an int-sizedstorage register, which will be initialized to 123.The sizeof operator returns the size of its operand (in bytes).The operand can be an expression or a type name in parentheses,e.g., sizeof n or sizeof(int).In case of an expression, the size of the result type is returned, e.g.,sizeof (n+2.5) returns 8, the size of a double on my machine.CPSC 427a, Lecture 8 5/18Outline Storage Managemet Bells and Whistles ClassesProperties of variablesNot all variables are created equal.The name may not be visible in all contexts.IIt is not visible from outside of the block in which it is defined.IIf a data member in a class, the name’s visibility may berestricted by the private keyword.Each storage register has a lifetime – the interval of time betweenthe creation or allocation of the variable, and the deletion ordeallocation of the variable.A variable can also be anonymous, in which case it has no nameand can only be accessed via a pointer or subscript. The notion oflifetime still applies.CPSC 427a, Lecture 8 6/18Outline Storage Managemet Bells and Whistles ClassesStorage classesC++ supports three different storage classes.1. auto objects are created by variable and parameterdeclarations. (This is the default.)Their visibility and lifetime is restricted to the block in whichthey are declared.The are deleted when control finally exits the block (asopposed to temporarily leaving via a function call).2. static objects are created and initialized at load time andexist until program termination.3. new creates anonymous dynamic objects. They exist untilexplicitly destroyed by delete or the program terminates.CPSC 427a, Lecture 8 7/18Outline Storage Managemet Bells and Whistles ClassesAssignment and copyingThe assignment operator = is implicitly defined for all types.Ib=a does a shallow copy from a to b.IShallow copy on objects means to copy all data members fromone object to the other.ICall-by-value uses the copy constructor to copy the actualargument to the function parameter.IIf the argument object contains pointer data members, thepointers are copied but not the objects they point to. Thisresults in aliasing—multiple pointers to the same object.CPSC 427a, Lecture 8 8/18Outline Storage Managemet Bells and Whistles ClassesStatic data membersA static class variable must be declared and defined.IA static class member is declared by preceding the memberdeclaration by the qualifier static.IA static class member is defined by having it appear in globalcontext with an initializer but without static.IMust be defined only once.ExampleIn mypack.hpp file, inside class definition:class MyPack {static int instances; // count # instantiationsIn mypack.cpp file:int MyPack::instances = 0;CPSC 427a, Lecture 8 9/18Outline Storage Managemet Bells and Whistles ClassesStatic function membersFunction members can also be declared static.IAs with static variables, the are declared inside class byprefixing static.IThey may be defined either inside the class (as inlinefunctions) or outside the class.IIf defined outside the class, the :: prefix must be used andthe word static omitted.CPSC 427a, Lecture 8 10/18Outline Storage Managemet Bells and Whistles ClassesFive common kinds of failures1. Memory leak—Dynamic storage that is no longer accessiblebut has not been deallocated.2. Amnesia—Storage values that mysteriously disapper.3. Bus error—Program crashes because of an attempt to accessnon-existant memory.4. Segmentation fault—Program crashes because of anattempt to access memory not allocated to your process.5. Waiting for eternity—Program is in a permanent wait stateor an infinite loop.Read the textbook for examples of how these happen and what todo about them.CPSC 427a, Lecture 8 11/18Outline Storage Managemet Bells and Whistles ClassesBells and whistlesCPSC 427a, Lecture 8 12/18Outline Storage Managemet Bells and Whistles ClassesOptional parametersThe same name can be used to name several different memberfunctions if the signatures (types and/or number of parameters)are diffent. This is called overloading.Optional parameters are a shorthand way to declare overloading.Exampleint myfun( double x, int n=1 ) { ... }This in effect declares and defines two methods:int myfun( double x ) {int n=1; ...}int myfun( double x, int n ) {...}The body of the definition of both is the same.If called with one argument, the second parameter is set to 1.CPSC 427a, Lecture 8 13/18Outline Storage Managemet Bells and Whistles Classesconstconst declares a variable (L-value) to be readonly.const int x;int y;const int* p;int* q;p = &x; // okayp = &y; // okayq = &x; // not okay -- discards constq = &y; // okayCPSC 427a, Lecture 8 14/18Outline Storage Managemet Bells and Whistles Classesconst implicit argumentconst should be used for member functions that do not changedata members.class MyPack {private:int count;public:// a get functionint getCount() const { return count; }...};CPSC 427a, Lecture 8 15/18Outline Storage Managemet Bells and Whistles ClassesOperator extensionsOperators are shorthand for functions.Example: <= refers to the function operator <=().Operators can be overloaded just like functions.class MyObj {int count;...bool operator <=( MyObj& other ) const {return count <= other.count; }};Now can write if (a <= b) ... where a and b are of typeMyObj.CPSC 427a, Lecture 8 16/18Outline Storage Managemet Bells and Whistles ClassesClassesCPSC 427a, Lecture 8 17/18Outline Storage Managemet Bells and Whistles ClassesWhat is a class?IA collection of things


View Full Document

Yale CPSC 427 - Lecture 8

Download Lecture 8
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 Lecture 8 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 Lecture 8 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?