2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.

Slides:



Advertisements
Similar presentations
ITEC200 – Week03 Inheritance and Class Hierarchies.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Classes: A Deeper Look Systems Programming.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
Inheritance, Polymorphism, and Virtual Functions
Chapter 10 Classes Continued
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 12: Adding Functionality to Your Classes.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Inheritance in the Java programming language J. W. Rider.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
A First Book of C++ Chapter 12 Extending Your Classes.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Understanding Inheritance
Operator Overloading; String and Array Objects
Advanced Java Topics Chapter 9
Inheritance, Polymorphism, and Virtual Functions
Week 6 Object-Oriented Programming (2): Polymorphism
Operator overloading Dr. Bhargavi Goswami
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Chapter 11: Inheritance and Composition
Chapter 8: Class Relationships
Java Programming Language
Presentation transcript:

2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance and Polymorphism Common Programming Errors

3 Operator Functions C++ operators listed in Table 14.1 can be redefined for class use, subject to certain restrictions –Symbols not in Table 14.1 cannot be redefined., ::, and ? : symbols cannot be redefined –New operator symbols cannot be created –Neither the precedence nor the associativity of C++’s operators can be modified

4 Operator Functions (continued) Additional restrictions on redefining C++ operators for class use –Operators cannot be redefined for built-in types –Unary operator cannot be changed to binary operator, and vice versa –Operator must be member of a class or be defined to take at least one class member as an operand

5 Operator Functions (continued)

6

7 Providing class with operators requires determination of: –Operations that make sense for class –How operations should be defined Example ( Date class of Chapter 12): Define a small meaningful set of operators –Addition of two dates not meaningful –Addition of an integer to a date or subtraction of an integer from a date is meaningful Integer defined as number of days to/from a date

8 Operator Functions (continued) Defining more Date class operators –Subtraction of 2 dates is meaningful Number of days between two dates –Comparison of two dates is meaningful Does one date occur before another? User-defined function: Function that redefines C++’s built-in operator symbols for class use

9 Operator Functions (continued) Declaring and implementing operator functions: Same as for all C++ member functions except: –Function’s name connects appropriate operator symbol to operator defined by the function –Format: operator where symbol is an operator listed in Table 14.1 Examples: operator++ is the name of addition function operator== is the name of comparison function

10 Operator Functions (continued) Writing operator function: Function accepts desired inputs and produces correct return value Creating comparison function for Date class: Compare two dates for equality –Select C++’s equality operator –Function name is operator== Accepts two Date objects Compares objects Returns a boolean value indicating result –True for equality, False for inequality

11 Operator Functions (continued) Prototype of comparison function bool operator==(const Date&); –Indicates that function is named operator== –Function returns integer –Accepts a reference to Date object –Second Date object calls the function

12 Operator Functions (continued) Writing comparison function: bool Date::operator==(const Date& date2) { if( day == date2.day && month == date2.month && year == date2.year) return true; else return false; } –const keyword ensures that the dereferenced Date object cannot be changed in the function

13 Operator Functions (continued) Calling operator function: Same syntax as calling C++ built-in types Example: If a and b are objects of type Date, the expression if (a == b) is valid –if (a.operator ==b) is also valid Program 14.1 is complete program including: –Declaration and definition of operator function –if statement with comparison operator function

14 Operator Functions (continued)

15 Operator Functions (continued)

16 Operator Functions (continued) Creating an addition function for Date class –Select C++’s addition operator –Function name is operator+ Accepts Date object and an integer object Adds integer to Date to return a Date –Suitable prototype: Date operator+(int) –Data conversion for Date class required Resulting day returned must be in range 1 – 30 Resulting month returned must be in range 1 – 12

17 Operator Functions (continued) Suitable addition function implementation Date Date::operator+(int days) { Date temp; // a temporary Date to store the result temp.day = day + days; // add the days temp.month = month; temp.year = year; while (temp.day > 30) // now adjust the months { temp.month++; temp.day -= 30; } while (temp.month > 12) // adjust the years { temp.year++; temp.month -= 12; } return temp; // the values in temp are returned }

18 Operator Functions as Friends Functions in Programs 14.1 and 14.2 constructed as class members All operator functions (except for =, (), [], -> ) may be written as friend functions Example: operator+() function from Program 14.2 –If written as friend, suitable prototype would be friend Date operator+ (Date&, int); –Friend version contains reference to Date object that is not contained in member version Table 14.2: shows equivalence of functions and arguments required

19 Operator Functions as Friends (continued)

20 Operator Functions as Friends (continued) Program 14.2’s operator+() function, written as a friend Date operator+(Date& op1, int days) { Date temp; // a temporary Date to store the result temp.day = op1.day + days; // add the days temp.month = op1.month; temp.year = op1.year; while (temp.day > 30) // now adjust the months { temp.month++; temp.day -= 30; } while (temp.month > 12) // adjust the years { temp.year++; temp.month -= 12; } return temp; // the values in temp are returned }

21 Operator Functions as Friends (continued) Difference between the member and friend versions of operator+() function: Explicit use of additional Date parameter ( op1 ) Convention for choosing between friend or member implementation: –Member function: Better for binary functions that modify neither operand (such as ==, +, - ) –Friend function: Better for binary functions that modify one of their operands

22 The Assignment Operator Revisited Drawback of simple assignment operator function (from Section 13.1): It returns no value –Multiple assignments ( a = b = c ) not possible Can use operator function that has return type together with this pointer Prototype and implementation of modified function illustrated on next slide – Function in Program 14.3 allows multiple assignment

23 The Assignment Operator Revisited (continued) Prototype for modified assignment operator Date operator=(const Date&); Implementation of prototype Date Date::operator=(const Date &newdate) { day = newdate.day; // assign the day month = newdate.month; // assign the month year = newdate.year; // assign the year return *this; }

24 Two Useful Alternatives – operator() and operator[ ] Sometimes operations are needed that have more than two arguments –Limit of two arguments on binary operator functions –Example: Date objects contain three integer data members: month, day, and year Might want to add an integer to any of these three members (instead of just the day member as done in Program 14.2)

25 Two Useful Alternatives – operator() and operator[ ] (continued) operator() (parentheses operator): No limit on number of arguments that can be passed to it –Addresses problem of passing three integers to Date addition operator function operator[ ] (subscript operator): Used where single non-object argument is required Parentheses and subscript operators must be defined as member (not friend) functions

26 Data Type Conversions Conversion from one built-in data type to another discussed in Chapter 3 Introduction of user-defined data types expands possibilities to following cases –Conversion from built-in type to built-in type –Conversion from built-in type to class (user-defined) type –Conversion from class (user-defined) type to built-in type –Conversion from class (user-defined) type to class (user-defined) type

27 Data Type Conversions (continued) Conversion from built-in to built-in: –Implicit conversion: Occurs in context of a C++ operation Example: When double precision number is assigned to integer variable, only integer portion stored –Explicit conversion: Occurs when cast is used Format dataType (expression)

28 Data Type Conversions (continued) Conversion from built-in to class: Done using constructor functions –Type conversion constructor: Constructor whose first argument is not member of its class and whose remaining arguments, if any, have default values –If the first argument is built-in data type, the constructor can be used to cast built-in data type to class object –Program 14.5 demonstrates this conversion type

29 Data Type Conversions (continued) Conversion from class to built-in: Done by using: –Conversion operator function: Member operator function that has name of built-in data type or class –When operator function has built-in data type name, it is used to convert from class to built-in data type –Usage of this type of function demonstrated in Program 14.6

30 Data Type Conversions (continued) Conversion from class to class: Done using member conversion operator function –Same as cast from user-defined to built-in data type –In this case, operator function uses class name being converted to rather than built-in data name –Demonstrated in Program 14.7

31 Class Inheritance and Polymorphism Ability to create new classes from existing ones is underlying motivation behind class and object- oriented programming techniques These techniques facilitate reusing existing code in new ways without need for retesting and validation –Permits class designers to make a class available to others for additions and extensions without relinquishing control over existing features

32 Inheritance Deriving one class from another class Base class (parent class, or superclass): Initial class used as basis for derived class Derived class (child class, or subclass): A completely new class that incorporates all of the data and member functions of its base class –Usually adds new data and function members –Can override any base class function

33 Inheritance (continued) Inheritance example: Three geometric shapes - circle, cylinder, and sphere –Shapes share common characteristic: radius –Can make circle base type for the other two shapes (Figure 14.1) –By convention, arrows always point from derived class to base class Reformulating these shapes as class types we would make circle base class and derive cylinder and sphere classes from it

34 Inheritance (continued)

35 Access Specifications Until now we have used only private and public access specifiers within class private status ensures that data members can be accessed only by class member functions or friends –Prevents access by any nonclass functions (except friends) –Also precludes access by any derived class functions

36 Access Specifications (continued) protected access: Third access specification permits only member or friend function access –Permits this restriction to be inherited by any derived class –Derived class defines its inheritance subject to base class’s access restrictions Class-access specifier: Listed after colon at start of its declaration section, defines inheritance Table 14.3 lists inherited access restrictions

37 Access Specifications (continued)

38 Access Specifications (continued) Example: Derive Cylinder class from Circle class // BASE class declaration class Circle { protected: double radius; public: Circle(double = 1.0); // constructor double calcval(); }; // class implementation // constructor Circle::Circle(double r) // constructor { radius = r; } // calculate the area of a circle double Circle::calcval() { return(PI * radius * radius); }

39 Access Specifications (continued) Example: Derived Cylinder class // class declaration where // Cylinder is derived from Circle class Cylinder : public Circle { protected: double length; // add one additional data member and public: // two additional function members Cylinder(double r = 1.0, double l = 1.0) : Circle(r), length(l) {} double calcval(); }; // class implementation double Cylinder::calcval() // this calculates a volume { return (length * Circle::calcval()); // note the base // function call }

40 Polymorphism Permits same function name to invoke one response in objects of base class and another response in objects of derived class –Example of polymorphism: Overriding of base member function using an overloaded derived member function, as illustrated by the calcval() function in Program 14.8 Function binding: Determines whether base class or derived class version of function will be used

41 Polymorphism (continued) Static binding: Determination of which function to be called is made at compile time –Used in normal function calls Dynamic binding: Determination of which function to be called is made at run time (via virtual function) Virtual function (Example in Program 14.10): Creates pointer to function to be used –Value of pointer variable is not established until function is actually called –At run time, and on the basis of the object making the call, the appropriate function address is used

42 Common Programming Errors Attempting to redefine an operator’s meaning as it applies to C++’s built-in data types Redefining an overloaded operator to perform a function not indicated by its conventional meaning –Will work but is bad programming practice Attempting to make conversion operator function a friend, rather than a member function

43 Common Programming Errors (continued) Attempting to specify return type for member conversion operator function Attempting to override virtual function without using same type and number of parameters as original function Using the keyword virtual in class implementation section –Functions are declared as virtual only in class declaration section

A First Book of C++: From Here To There, Third Edition 44 Summary User-defined operators can be constructed for classes by using member operator functions User-defined operators may be called as a conventional function with arguments or as an operator expression Subscript operator function, operator[], permits a maximum of one nonclass argument Parentheses operator function, operator(), has no limits on number of arguments

45 Summary (continued) Four categories of data type conversions: –Built-in types to built-in types –Built-in types to class (user-defined) types –Class (user-defined) types to built-in types –Class types to class (user-defined) types Type conversion constructor: Constructor whose first argument is not a member of its class and whose remaining arguments, have default values

46 Summary (continued) Conversion operator function is member operator function that has the name of built-in data type or class Inheritance is capability of deriving one class from another class Base class functions can be overridden by derived class functions with the same name

47 Summary (continued) Polymorphism: Having same function name invoke different responses on basis of the object making function call Static binding: Determination of which function is invoked is made at compile time Dynamic binding: Determination made at run time Virtual function specification designates that dynamic binding should take place