Overloading C++ extends overloading to constructs not usually treated as operators: –Indexing: operator [ ] ( ) –Selection: operator -> ( ) –Function call:

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 12: Support for Object-Oriented Programming
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object Oriented Programming and Object Oriented Design Programming Languages Robert Dewar.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Operator Overloading: indexing Useful to create range-checked structures: class four_vect { double stor[4]; // private member, actual contents of vector.
1 Object Oriented Programming. 2 What’s the Object? Global name space for functions in C Global name space for functions in C Every function is available.
Operator Overloading: bounds-checked indexing Useful to create range-checked structures: class four_vect { double stor[4]; // private member, actual contents.
Chapter 10 Classes Continued
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 12: Adding Functionality to Your Classes.
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.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Types in programming languages1 What are types, and why do we need them?
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Operator Overloading: indexing Useful to create range-checked structures: class four_vect { double stor[4]; // private member, actual contents of vector.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
A First Book of C++ Chapter 12 Extending Your Classes.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Inheritance and Polymorphism
Inheritance and Run time Polymorphism
Chapter 3: Using Methods, Classes, and Objects
12 Data abstraction Packages and encapsulation
Java Programming Language
Dr. Bhargavi Dept of CS CHRIST
CIS 199 Final Review.
Presentation transcript:

Overloading C++ extends overloading to constructs not usually treated as operators: –Indexing: operator [ ] ( ) –Selection: operator -> ( ) –Function call: operator ( ) ( ) –All assignment operators: +=, *=, etc. –new, delete and delete[ ]

Operator Overloading: indexing Useful to create range-checked structures: class four_vect { double stor[4]; // private member, actual contents of vector. … double& operator[ ] (int j) { if (j 3) throw index_error; // defined elsewhere return stor[j]; }; Note: return type is a reference, so can be used on l.h.s

Extending the meaning of subscripting An associative array: class Assoc { // a map from strings to numbers struct Pair { // an inner class char* name; double val; Pair (char* n = “”, double v = 0): name (n), val (v) { }; }; pair * contents; // Assoc is a set of pairs public: Assoc () { }; // default constructor double& operator[ ] (const char *); // map string => number };

Efficiency vs. privacy Linear algebra package: vector class for 4-vectors, matrix class for 4-by-4 matrices vector class redefines [ ] to do check index matrix class redefines [ ] to check both indices want to implement matrix * vector: v1 [j] =  m[j][k] * v [k]; 4 range-checks for every component, all redundant.

Relaxing privacy: friends A friend function can access private members of a class a friend is declared in the class (cannot sneak in) class vector {... friend vector operator* (const matrix&, const vector&); a function can be the friend of several classes class matrix {… friend vector operator*(const matrix&, const vector&); A class can be a friend, so all its function members are. class quaternion { friend class matrix; …} A friend is not a class member

Efficient code class matrix { vector row [4]; public: … }; vector operator * (const matrix&m, const vector v) { vector res; for (int i = 0; i < 4; i ++) { res.stor[i] = 0; // stor is private data in a vector for (int j = 0; j < 4; j++) res.stor[j] += m.row[i].stor[j] * v.stor[j]; // row is private }; return res; };

Packages and private information In Ada, declare both types in same package. Package body has access to full declarations for both: package linear_algebra is type vector is private; type matrix is private; function “*” (m : matrix; v : vector) return vector; private type vector is array (1..4) of long_float; type matrix is array (1..4) of vector; -- code in body can use array representation of both. end linear_algebra;

Inheritance General mechanism for extending existing classes. Specialization: an X is an A A mammal is a vertebrate –a feline is a mammal a cat is a feline –a persian is a cat A persian has all the attributes of a feline, a mammal, and a vertebrate A class hierarchy implements a sequence of Is-A relations

Advantages of inheritance Factorization: Common properties are grouped in a single class code reuse : Descendant class (derived class) is defined incrementally over parent. incremental programming : New derived classes can be added without changing existing parents and siblings. Polymorphism: Can describe collections of diverse objects belonging to a class hierarchy.

Derivation class point { // base class int x, y; public: point () { x = 0; y = 0}; // basic constructor void move (int dx, int dy) { x += dx; y += dy}; } void display ( ) ; // put black dot on the screen. } class color_point: public point { // derived class int R, G, B; // in addition to hidden members x, y public: color_point ( ):point ( ) {R = 50; G = 50; B = 50;}; // call parent constr. void lighten (int w) { R -= w; G -=w; B -= w;}; void display ( ); // put colored dot on the screen // move is inherited, applies to color_points as well };

Substitution rule An object from a derived class can be used wherever an object from the base class is expected. point* p1 = new color_point ( ); works because a descendant has all the attributes of the parent (possibly with different meanings). color_point* wrong = new point ( ); // error: incomplete object but p1 can be coerced (cast) to a color_point, because it is one.

inheritance and privacy Private members of the parent class are not visible in (the methods of) the derived class. int color_point::abcisa ( ) {return x; }; // error Constructor for parent is used to initialize parent data. Derived (…) : Parent (…) { …}; // rest of construction Protected members are visible in descendants, but not outside.

Polymorphism Because of substitution rule, can have collection of various kinds of points: point* figure [100]; To display collection, apply the appropriate display method to each: point::display () color_point::display () blinkling_point::display () Could add discriminant to each object, to recognize its class Best to use virtual methods.

Virtual methods class parent { virtual void change (int x) … class child: public parent { virtual void change (int x) … // overrides parent operation class grandchild: public child { virtual void change (int x)… // overrides child operation parent* someone = … // can be any member of family someone-> change () // the proper one for someone’s class

Dynamic dispatching If M is virtual, the call ptr -> m (..) must determine the actual nature of ptr, and invoke the proper method Each class is described by a table of virtual methods (the vtable) Each object carries a pointer to the vtable. Each derived class inherits the table each overriding modifies an entry in the table Dynamic dispatching is an indirect call through an entry at a known position in the vtable.

Overloading and dynamic dispatching Overloading resolution is a compile-time process that applies to an expression in a context: that = this_one + the_other; // find right numeric type dynamic dispatching is a run-time activity that applies to a reference: some_relative-> display (); // indirect call tommy.display (); // static resolution overloading resolution may fail if call is ambiguous dynamic dispatching succeeds because derived classes have all the virtual methods of ancestors.

The profile of an overriding method class parent { virtual void method (int x); virtual void relation (parent x); class child: public parent { virtual void method (int x); // overrides inherited method virtual void method (double x); // different method Binary operations are not symmetric: virtual void relation (child x); // does not override parent relation virtual void relation (parent x); // overrides

Abstract classes Base class may not have enough attributes to create meaningful object: class shape { point anchor; int color [3]; virtual void display (); // can’t write anything here }; shape blob; // can’t do anything with it If methods are declared abstract, cannot create objects of class: virtual void display ( ) = 0; // must be overridden

A useful pattern: the factory Need to create class hierarchy. Need different versions of hierarchy on different platforms (e.g. Windows vs. Linux) Need to minimize dependence on platform. Solution: encapsulate “constructors” into an object: class maker { virtual class1* build_class1 (int) = 0; virtual class2* build_class2 (int) = 0; };

Everything can be solved with indirection class win_maker: public maker { virtual class1* build_class1 (int){...}; // implementation virtual class2* build_class2 (int){…}; // on win2000 }; class linux_maker: public maker { virtual class1* build_class1 (int){...}; // implementation virtual class2* build_class2 (int){…}; // on RedHat 6.2 }; maker* factory =.. // build the proper one class1* thing1 = maker -> build_class1 (5);

Multiple inheritance Class can inherit from several parents: class abstract_stack { // algebraic definition public: void push (int x) = 0;... }; class vector {…}; // sequential allocation class stack: public abstract_stack, public vector { public: void push (int x) {.. }; // use vector primitives }

Multiple inheritance and overloading class A { … void draw ( ); }; class B { … void draw ( ); }; class C: public A, public B {... void display ( ) { draw ( ); // ambiguous: 2 inherited methods A:: draw ( ); // OK };

Replicated base classes class Root {… // some data members class A: public Root { … // inherits data members class B: public Root { … // inherit data members class hybrid: public A, public B {… // two copies of data members of Root? class C: virtual public Root {… // single copy in children

Other complexities Additional run-time structures for dispatching: –Data members are concatenation of data members of parents –Vtabl is concatenation of Vtabl’s of parents. –Each operation needs to compute offset to find parent data members in descendant object. Multiple inheritance best when it is interface inheritance, or when implementation is inherited from only one parent: Java model: one parent, many interfaces.

Object-Oriented Programming in Ada Type extensions: different syntax, same semantics Single inheritance only No distinguished object: call has a controlling argument All primitive operations are virtual, but call need not be indirect if context is not dispatching The family of types derived from T is the set T’Class

Type extensions type Object is tagged record X_Coord: Float; Y_Coord: Float end record; … type Circle is new Object with record Radius: Float; end record; … Obj : Object := (1.0, 1.0); -- regular aggregate Circ1 : Circle := (Obj with Radius => 4.5); -- extension aggregate

Primitive operations and inheritance All operations declared in the same package, with a parameter or return type that mention T, are primitive operations of T function “=“ (Left, Right : T) return Boolean; function Build (Parm : Integer) return T; function Capacity (Thing : access T) return Integer; procedure Transform (Thing : in out T); Usual inheritance and overriding rules apply: function “=“ (Left, Right: Better_T) return Boolean;

Dynamic Dispatching typically through classwide parameter: type obj_ref is access all Object’class; procedure display_figure (Group: Obj_List) is Ptr : Obj_Ref := First (Group); -- assume some iterator Envelope : Circle; begin Envelope := Find_Boundary (Group); while Ptr /= null loop Display (Ptr.All); -- Object’class: dispatch Display (Envelope); -- Static call: display a circle Ptr := Next (Ptr); end loop; end;

Multiple controlling parameters Dispatching can only depend on one type: cannot have primitive operations in more than one type. procedure Combine (This: Circle; That: Square); -- Illegal procedure Combine (This : Circle; That: figure’class); -- OK, dispatches on This, other parameter is any figure. procedure Combine (This, That : Figure’class); -- OK, no dispatching

Abstract types Same semantics as in C++: introduce root type for a class, with abstract operations: type Root_Object is abstract tagged null record; function Display (It : Root_Object) is abstract; Type extension is abstract if it does not override Display An extension of a concrete type can be abstract: make overriding of some operation abstract.