Electrical Engineering - Chapter 14: Object Oriented Design

Called with an object as an argument General declarator Class :: Class (Class& alias) Class is class name alias is alias for object being copied Values directly copied can be listed in initialization section :member1 (alias.member1), member2 (alias.member2)

ppt32 trang | Chia sẻ: thuongdt324 | Lượt xem: 407 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 14: Object Oriented Design, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 14 – Object Oriented DesignCopy ConstructorCalled with an object as an argumentGeneral declarator Class :: Class (Class& alias)Class is class namealias is alias for object being copiedValues directly copied can be listed in initialization section:member1 (alias.member1), member2 (alias.member2)Lesson 14.1DestructorMember function automatically called when object goes out of scopeName of destructor function required to be class name preceded by tilde (~)Cannot return a value or contain argumentsGeneral destructor declaration and declaratorLesson 14.1~Class ( );Class :: ~Class ( ) Declaration in class definitionDeclarator in function definitionstatic Storage Class SpecifierGives permanent storage for variablePersists through entire program executionMemory accessibilitystatic used with local variableAccess allowed only from function where declaredstatic used with global variableAccess allowed from file with declarationstatic used with data member of a classaccess allowed by all objects of the classLesson 14.2static Data MembersShared by all objects of a classDeclared by keyword static in class definitionInitialized outside any member functions type Class :: variable = value;Accessible and modifiable by invoking ordinary member function on any objectAccessible and modifiable by invoking static member function on classSpecified as public or private in accessibilityLesson 14.2static Function MembersDeclared static by preceding declaration with keyword staticNot necessarily invoked using object and dot operatorWithout keyword static in function declaratorNot allowed to access to nonstatic data and function membersAllowed to be called even when no class objects exist in programLesson 14.2const Special QualifierData members and objects qualified by const cannot be modified after initializationFunction members qualified with const do not modify invoking object's dataLesson 14.3Pointer Data Members with constThree optionsUsing const before the data typevalue pointed to cannot be modifiedUsing const after the data typeAddress stored by pointer variable cannot be modifiedUsing const before and after the data typeNeither value pointed to nor address stored can be modifiedLesson 14.3const Reference Data MemberGeneral form const type& ref; Class :: Class (const type& ref_var) : ref (ref_var) Class object (base_variable);Lesson 14.3Class definitionConstructorfunctionDeclaration of objectConstant Member FunctionsProtects data members from being inadvertently modifiedTo declare function (with 2 arguments) const type function (type, type) const;Form of declarator for above function type Class :: function (type arg1, type arg2) constLesson 14.3Constant ObjectsEffectively makes all data members constNone of data members of const object can be changed after initializationForm: declare const object with 3 arguments const Class object (arg1, arg2, arg3);Can use const object to call function object.function (arg1b, arg2b);Lesson 14.3Mutable Data Membersmutable used with non constant data membersCan be modified by constant member functionCan overrides the const of a function and change the non constant data memberLesson 14.3friend FunctionIndicates that function has special relationship with a classfriend specified for function that is not member of any classAllows function to access private data members directlyForm for declaration with 2 arguments friend type function (type, type);Lesson 14.4friend FunctionsDefining friend functionsKeyword not used in function definitionDirectly accesses private data members of object (where class declared friend)Calling friend functionsNo invoking object used since friend not a member function function (arg1, arg2);Lesson 14.4Class That Grants FriendshipLesson 14.4Granting classobject 1Granting classobject 2Private datamembers forobject 1, granting classPrivate datamembers forobject 2, granting classPublic function members,granting classFriend function(nonmember)Member Functions As FriendsCan be member of another classGeneral form (with 2 arguments)Lesson 14.4class Class1{ friend Class2 :: function (type, type); };friend ClassAll member functions of friend class can access all private data and function member of class granting friendshipGeneral form for declaring friend class Name;Common practice to list friend classes before public and private access specifiersLesson 14.5Defining a Friend ClassBasic general form for defining class Friend { public: type functionf (Granting&); };Basic general form for member function type Friend :: functionf (Granting& objectga) { objectga.functiong ( ) ; objectga.datag = ; }Lesson 14.5Calling Function of Friend ClassBasic form for calling friend class objectf . functionf (objectg);objectf is the friend class objectfunctionf is the friend class member function nameobjectg is the granting class objectLesson 14.5Operator OverloadingCreates new definitions of operators for use with objectsCreate function for a class called operator+operator is the keywordFollow by math operator of your choiceWrite code to perform member-by-member addition within function bodyWhen client of class, adds two objects, operator+ ( ) function automatically calledLesson 14.6Equivalent Function CallsGenerated by the operator expressionDependent upon classification of the operator function being friend or member, binary or unaryBinary friend functionsBinary member functionsUnary member functionsUnary friend functions (not commonly used)Lesson 14.6Binary friend FunctionsLesson 14.6object1 + object2operator+ (object1, object2);ExpressionEquivalent Function CallBinary Member FunctionsDifferent type call because have invoking objectsLesson 14.6 object1 = object2object1 . operator = (object2);ExpressionEquivalent Function CallPlaceholder (any binary operator +, -, =,*, or /Unary Member FunctionsAdditional complication of being either prefix or postfixLesson 14.6++ object1 object1.operator++ ( );Equivalent Function Callobject1 ++ object1.operator++ (dummy);ExpressionBinary friend FunctionsGeneral form exampleLesson 14.6Class1 operator- (const Class1& objecta, const Class1& objectb){ Class1 temp; temp.member1 = objecta.member1 - objectb.member1; temp.member2 = objecta.member2 - objectb.member2; return temp;}Rules for Operator OverloadingSet of available and unavailable operatorsTable 14.3 in textCannot change classification of operatorOperator precedence rules still applyEach friend or freestanding operator function must take at least one object as argumentEquivalent function call for each operator cannot be modifiedLesson 14.6Operator OverloadingTable 14.5 shows many examplesOnly member functions can return *thisComparison operators return a bool>> and << operators are commonly overloaded to simplify input and output( ) operator is called using an object name followed by an argument list enclosed in ( )Lesson 14.6UMLUnified Modeling LanguageDeveloped by James Rumbaugh, Grady Booch, and Ivar Jacobson in mid 1990sCreate diagrams to develop programCollaborationUse caseState chartClassMany othersLesson 14.7UML Class Diagram SymbolsRectangles used to enclose classesLines used to connect classes that interactArrows and descriptors used to indicate type and direction of interactionSmall filled diamond heads used to indicate objects of one class are members of another classNumbers next to rectangles used to indicate number of objects of a classLesson 14.7Composition"Has a" relationshipQuite commonly in object-oriented programmingObjects contained within another object Lesson 14.7Car classEngineclassWheelclassAssociation"uses" or client-server relationshipKeeps server class independent of client class Has simple interface between the two classesServer class designer must create both interface and implementationLesson 14.7SummaryUse the special class specifiers static and constWork with friend functions and classesOverload operatorsRead some UML and the difference between "has a" and "uses" relationshipsLearned how to:
Tài liệu liên quan