Unformatted text preview:

OutlineRemarks on Problem Set 2ClassesDerivationConstruction, Initialization, and DestructionOutline Problem Sets Classes Derivation Construction/DestructionCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 9September 30, 2010CPSC 427a 1/26Outline Problem Sets Classes Derivation Construction/DestructionRemarks on Problem Set 2ClassesDerivationConstruction, Initialization, and DestructionCPSC 427a 2/26Outline Problem Sets Classes Derivation Construction/DestructionRemarks on Problem Set 2Seth HammanCPSC 427a 3/26Outline Problem Sets Classes Derivation Construction/DestructionCoding conventions for this courseIUse #pragma once at top of include files.IThe functions say() and fatal() in tools.cpp are to helpyou with code development and debugging while you arelearning C++. They should not be used in a finished productonce you have learned the standard C++ ways for handlingconsole output and exception handling.IDon’t use STL until it has been covered in class.IDon’t define global variables.IUse file name suffix .cpp for C++ code files and .hpp for C++header files.IUse convention that .cpp file only includes corresponding .hppfile. The .hpp file contains all necessary includes.CPSC 427a 4/26Outline Problem Sets Classes Derivation Construction/DestructionCoding conventions (cont.)IUse short identifiers in local contexts; longer more descriptivenames in larger contexts.IUse consistent naming style, e.g., data member ends in ‘_’.ITypical structure of a class definition:class Name {private:varsmethodspublic:constructorsdestructormethods};CPSC 427a 5/26Outline Problem Sets Classes Derivation Construction/DestructionError checking and error handlingWhat should your program do with “bad” inputs? Options:1. Detect and report/handle all bad inputs.2. Detect and report bad inputs that would cause unexpectedbehavior such as a crash, divide by 0, and so forth, but don’tcheck for inputs that violate the input specs if they arehandled reasonably by your program. (E.g., a 6-digit numberwhere the specs said to expect a 5-digit number.)3. Don’t bother with error checking.(1) is most desirable.(2) is acceptable in this course but not in industrial strength codewhere the specs and code should agree.(3) is not acceptable.CPSC 427a 6/26Outline Problem Sets Classes Derivation Construction/DestructionDesign choicesProgramming involves design choices.There is not always one “right” choice; rather, there are tradeoffs,and the choice requires judgement.Use report.txt to discuss and justify the design choices youmake in your own programs.CPSC 427a 7/26Outline Problem Sets Classes Derivation Construction/DestructionPS2 issuesIatoi() is deprecated; use strtol() instead.IRefer to man pages for documentation of unfamiliar functions,e.g., man atoi or man 3 atoi.Iclass Random should not be instantiated more than once,since all instances share rand(), and a subsequent call onsrand() will interfere with previous instances. Thus, it shouldbe a singleton class (later).IPut game logic into the Craps class, not into Dice.IUse switch for clean coding of game logic.CPSC 427a 8/26Outline Problem Sets Classes Derivation Construction/DestructionClassesCPSC 427a 9/26Outline Problem Sets Classes Derivation Construction/DestructionWhat is a class?IA collection of things that belong together.IA struct with associated functions.IA way to encapsulate behavior: public interface, privateimplementation.IA way to protect data integrity, providing world with functionsthat provide a read-only view of the data.IA data type from which objects (instances) can be formed.We say the instances belong to the class.IA way to organize and automate allocation, initialization, anddeallocation.IA way to break a complex problem into manageable,semi-independent pieces, each with a defined interface.IA reusable module.CPSC 427a 10/26Outline Problem Sets Classes Derivation Construction/DestructionClass relationshipsClasses relate to and collaborate with other classes.Many ways in which one class relates to other.We first explore derivation, where one class modifies and extendsanother.CPSC 427a 11/26Outline Problem Sets Classes Derivation Construction/DestructionDerivationCPSC 427a 12/26Outline Problem Sets Classes Derivation Construction/DestructionWhat is derivation?One class can be derived from another.Syntax:class A {public:int x;...};class B : public A {int y;...};A is the base class; B is the derived class.B inherits data members from A.CPSC 427a 13/26Outline Problem Sets Classes Derivation Construction/DestructionInstancesA base class instance is contained in each derived class instance.Similar to composition, except for inheritance.Function members are also inherited.Data and function members can be overridden in the derived class.Derivation is a powerful tool for allowing variations to a design.CPSC 427a 14/26Outline Problem Sets Classes Derivation Construction/DestructionSome uses of derivationDerivation has several uses.ITo allow a family of related classes to share common parts.ITo describe abstract interfaces `a la Java.ITo allow generic methods with run-time dispatching.ITo provide a clean interface between existing, non-modifiablecode and added user code.CPSC 427a 15/26Outline Problem Sets Classes Derivation Construction/DestructionConstruction, Initialization, and DestructionCPSC 427a 16/26Outline Problem Sets Classes Derivation Construction/DestructionStructure of an objectA simple object is like a struct in C.It consists of a block of storage large enough to contain all of itsdata members.An object of a derived class contains an instance of the base classfollowed by the data members of the derived class.Example:class B : A { ...};B b;Then “inside” of b is an A-instance!CPSC 427a 17/26Outline Problem Sets Classes Derivation Construction/DestructionReferencing a composed objectConstrast the previous example toclass B { A a; ...};B b;Here B composes A.The embedded A object can be referenced using data membername a.CPSC 427a 18/26Outline Problem Sets Classes Derivation Construction/DestructionReferencing a base objectHow do we reference the base object embedded in a derived class?Example:class A { public: int x; int y; ...};class B : A { int y; ...};B b;IThe data members of A can be referenced directly by name.x refers to data member x in class A.y refers to data member y in class B.A::y refers to data member y in class A.Ithis points to the whole object.Its type is B*.It can be coerced to type A*.CPSC


View Full Document

Yale CPSC 427 - Lecture 9

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