C++ Language - Session 5: Class Basics (cont) - FPT University

ppt 34 trang hoanguyen 4080
Bạn đang xem 20 trang mẫu của tài liệu "C++ Language - Session 5: Class Basics (cont) - FPT University", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptc_language_session_5_class_basics_cont_fpt_university.ppt

Nội dung text: C++ Language - Session 5: Class Basics (cont) - FPT University

  1. Session 5- Class Basics Contd Normal Member Functions Overloaded Member Functions Helper Functions Session 5 – Class Basics contd 1/34
  2. Objectives • Normal Member Functions – Implicit Parameters – Queries – Modifiers – this • Overloaded Member Functions – Constructors – Operators – Pre-Fix Operators – Post-Fix Operators Session 5 – Class Basics contd 2/34
  3. Objectives • Helper Functions – Operand Symmetry – Friends • Friend Functions • Friend Classes – Helpers • Overloaded Operators (Modifier, Helper) Session 5 – Class Basics contd 3/34
  4. Normal Member Functions • An object communicates with the outside world through queries and modifiers. • Queries and modifiers are member functions of the object's class. object.identifier( argument, argument, ) Session 5 – Class Basics contd 4/34
  5. Implicit Parameters • Each member function has direct access to all members of its class, including private members. • There is no need to pass the values of these instance variables through a function's parameter list. Session 5 – Class Basics contd 5/34
  6. Queries • A query is a member function that reports the current state of its object. • A query leaves the state of its object intact. • Use the keyword const after the closing parenthesis of the function header and before the opening brace of the function definition void display() const; Session 5 – Class Basics contd 6/34
  7. Modifiers • A modifier is a member function that changes the state of its object. Session 5 – Class Basics contd 7/34
  8. this keyword • Refer to the entire set of instance variables of an object, rather than to one particular instance variable. • To refer to the entire set of instance variables (*this). • The keyword this holds the address of the current object. • Can only use this from within a member function Session 5 – Class Basics contd 8/34
  9. This keyword example • The member function named promote that promotes a Student object to the next semester and returns a reference to the promoted object. Student Student::promote() { semester++; return *this; } Session 5 – Class Basics contd 9/34
  10. Overloaded Member Functions • A member function can have several different meanings. • A function that has several meanings is an overloaded function. • We identify the different meanings of a member function through its different signatures. The signature of a member function consists of – the function name, – the data types of its parameters, – the order of the parameters and possibly – the const status of the function. Session 5 – Class Basics contd 10/34
  11. Overload the constructor • If a class declaration includes a constructor that takes arguments but does not include a no-arguments constructor, the compiler DOES NOT insert an empty body no-argument constructor. Session 5 – Class Basics contd 11/34
  12. Single-Argument Constructors • Single argument constructors play a special role. These constructors enable implicit promotions. Session 5 – Class Basics contd 12/34
  13. Single-Argument Constructors Limit the number of single-argument constructors to avoid potential ambiguities in converting one data type to another. Session 5 – Class Basics contd 13/34
  14. Casting (single-argument constructor ) • A single-argument constructor defines the rules for a cast from the argument data type to the object data type. • The form of a cast is Identifier(argument) For example, harry = Student(1234); Session 5 – Class Basics contd 14/34
  15. Overload Operators(1) • We can overload the built-in operators that we use with primitive data types to extend their scope of operation to include objects of our own derived data types. However, we may not define new operators. Session 5 – Class Basics contd 15/34
  16. Overload Operators(2) • We may overload the following operators: – unary arithmetic (+ -) – binary arithmetic (+ - * / %) – relational (== = !=) – logical (&& || !) – assignment (= += -= *= /= %=) – post-fix/pre-fix (++ ) • We may NOT overload – the scope resolution operator (::), – the member selection operator (.), – the member selection through pointer to member operator (.*), – the ternary operator (?:), – the sizeof operator (sizeof) or – the typeid operator (type()). Session 5 – Class Basics contd 16/34
  17. Overloaded Member Operators(1) • Overloaded operators are functions and introducing new signatures. The header for an overloaded operator consists of – the return data type, – the keyword operator, – the operator symbol, and – the operands as parameters. Session 5 – Class Basics contd 17/34
  18. Overloaded Member Operators(2) • The form of the function header for a unary member operator is type operator symbol () • The form of the function header for a binary member operator is type operator symbol (type identifier) Session 5 – Class Basics contd 18/34
  19. Operator overloaded example • For example, to add to our Student class the ability to compare two students, let us overload the equality operator (==) as a member function. Session 5 – Class Basics contd 19/34
  20. Operator overloaded example Session 5 – Class Basics contd 20/34
  21. Operator overloaded example Session 5 – Class Basics contd 21/34
  22. Pre-Fix Operators • Prefix operators are unary operators that increment/decrement their operand before returning its value. Their operand is the object upon which they are called. type operator++() or type operator () Session 5 – Class Basics contd 22/34
  23. Post-Fix Operators • Postfix operators are unary operators that increment/decrement their operand after returning its value. Their operand is the object upon which they are called. type operator++(int) or type operator (int) The presence of the int data type as a parameter distinguishes the post-fix operators from pre-fix operators. Session 5 – Class Basics contd 23/34
  24. Pre/Post-Fix Operators Example Session 5 – Class Basics contd 24/34
  25. Helper Functions • A class may be supported by global functions. These functions receive object data through explicit function parameters and are called non-member functions. • Non-member functions are preferable to member functions in the sense that non- member functions keep the number of members within a class to a minimum. Session 5 – Class Basics contd 25/34
  26. Operand Symmetry • Overloading operators as member functions results in asymmetric syntax. Consider a member version of the == operator for our Student class (asymmetric syntax ) The non-member version of the == operator receives both operands as explicit parameters (symmetric syntax) Session 5 – Class Basics contd 26/34
  27. Friends(1) • The non-member function solution is undesirable in that it grows the class definition, which may be considered poor design. • To avoid adding queries, we can explicitly grant a non-member function access to the private members of the class. Session 5 – Class Basics contd 27/34
  28. Friends(2) • The form of a friendship declaration is friend type identifier(type [,type[, ]]); Session 5 – Class Basics contd 28/34
  29. Friends(3)-Example Session 5 – Class Basics contd 29/34
  30. Friend Classes • A class can grant access to its private members to, not just one member, but all of the members of another class. friend class ClassName; Session 5 – Class Basics contd 30/34
  31. Helpers(1) • Non-member functions that support a class are called helpers. • Helpers include at least one parameter of the class type. • The header file that contains the declaration of a class includes the declarations for all of its helper functions. • The class declaration identifies as friends those helper functions that require access to private members. • The declaration of helper functions that do not require access to private members is outside the class declaration. Session 5 – Class Basics contd 31/34
  32. Helpers(2) • The implementation file that contains the definitions of the class member functions also includes the definitions of all of the class' helpers. Session 5 – Class Basics contd 32/34
  33. Helpers(3) • Overloaded operators are either modifiers or helpers. – Modifier operators change the state of the leftmost operand in the expression. Candidates include +=, -=, *=, /=, =, ++, and . Modifier operators are ideal candidates for member functions. – Helper operators do not change the state of any operand in the expression. Candidates include +, - , *, /, ==, >, >=, <, <=, and !=. Session 5 – Class Basics contd 33/34
  34. Summary • Normal Member Functions • Helper Functions – Implicit Parameters – Operand Symmetry – Queries – Friends – Modifiers • Friend Functions – this • Overloaded Member Functions • Friend Classes – Constructors – Helpers – Operators • Overloaded Operators – Pre-Fix Operators (Modifier, Helper) – Post-Fix Operators Q&A Session 5 – Class Basics contd 34/34