Jozef Goetz contribution, 2016 1 Credits go to  2014, 2011, 2009 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
Inheritance Inheritance Reserved word protected Reserved word super
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
 2002 Prentice Hall. All rights reserved. Some slides modified by LL 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Inheritance, Polymorphism, and Virtual Functions
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Derived-Class-Object.
Operator Overloading in C++
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Inheritance using Java
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 1: Course overview.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Programming Pillars Introduction to Object- Oriented Programming.
1 Advanced Programming Lecture 5 Inheritance. 2 static Class Members Every object of a class has its own copy of all instance variables Sometimes it is.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Derived-Class-Object.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
 2002 Prentice Hall. All rights reserved. 1 Chapter 8 – Object-Based Programming Outline 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with.
 2002 Prentice Hall. All rights reserved. 1 Introduction Polymorphism allows programmers to write: –Programs that handle a wide variety of related classes.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Derived-Class-Object.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Advanced Programming Inheritance (2). 2 Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing.
 2002 Prentice Hall. All rights reserved. 1 Week 4: OOP Questions from last lecture: –Single-subscript arrays –Multiple-subscript arrays –Homework/Practical.
 2002 Prentice Hall. All rights reserved. 1 Chapter 10 – Object-Oriented Programming: Part 2 Outline 10.1Introduction 10.2 Derived-Class-Object to Base-Class-Object.
Jozef Goetz contribution,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2002 Prentice Hall. All rights reserved. 1 Chapter 11 – [Object-Oriented Programming] Polymorphism, Interfaces & Operator Overloading Section [order.
Jozef Goetz contribution,  2011, 2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2009 Prentice Hall. All rights reserved. 1 Chapter 12 Polymorphism, Interfaces & Operator Overloading [optional] Many slides modified by Prof. L. Lilien.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapters 9, 10 – Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Questions from last lesson
Chapter 9 – Object-Oriented Programming: Inheritance
 2002 Prentice Hall. All rights reserved.
Polymorphism, Interfaces &
CIS 199 Final Review.
Presentation transcript:

Jozef Goetz contribution, Credits go to  2014, 2011, 2009 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.

Jozef Goetz contribution, sealed Classes and Methods  sealed is a keyword in C# ( final methods and classes in Java)  goal: prevent overriding and inheritance  sealed methods cannot be overridden in a derived class  Methods that are declared static and private, are implicitly sealed  keyword sealed is used for methods that have been overridden, but that we don’t want to be overridden in derived classes  sealed classes (opposite to abstract class) cannot have any derived-classes  cannot be a base class  all methods in sealed class are sealed implicitly  can be instantiated  Creating sealed classes can allow some runtime optimizations  e.g., the compiler can decide inline a sealed method call

Jozef Goetz contribution, Case Study: Creating and Using Interfaces  Interfaces specify the public services (methods and properties) that classes must implement  no default implementation to inherit  Interfaces provide:  no instance variables and  no default method implementations  vs. abstract classes which may provide some default implementations  An abstract class is best used for providing data and services in a hierarchical relationship.  Interfaces are used to “bring together” disparate objects  that relate to one another only through the interface

Jozef Goetz contribution, Case Study: Creating and Using Interfaces  Interfaces are defined using keyword interface public interface InterfaceName{ }  Use inheritance notation to specify a class implements an interface ClassName : InterfaceName { }  Classes implement an interface (its methods)  Classes may implement more than 1 interface (a comma separated list of interfaces)  Classes that implement an interface, must provide implementations for every method and property in the interface definition  Example: interface IAge that returns information about an object’s age  Can be used by classes they have at least one common characteristic for people, cars, trees (all have an age)

 2002 Prentice Hall. All rights reserved. Outline 5 IAge.cs 1 // Fig ed1: IAge.cs 2 // Interface IAge declares property for setting and getting age. 3 4 public interface IAge 5 { 6 int Age { get; } // no implementation 7 string Name { get; } // no implementation 8 } Definition of interface IAgeClasses implementing this interface will have to define read-only properties Age and Name, because of “get”

 2002 Prentice Hall. All rights reserved. Outline 6 Person.cs 1 // Fig ed1: Person.cs 2 // Class Person has a birthday. 3 using System; 4 5 public class Person : IAge 6 { 7 private string firstName; 8 private string lastName; 9 private int yearBorn; // constructor 12 public Person( string firstNameValue, string lastNameValue, 13 int yearBornValue ) 14 { 15 firstName = firstNameValue; 16 lastName = lastNameValue; if ( yearBornValue > 0 && yearBornValue <= DateTime.Now.Year ) 19 yearBorn = yearBornValue; 20 else 21 yearBorn = DateTime.Now.Year; 22 } // property Age implementation of interface IAge - must 25 public int Age 26 { 27 get 28 { 29 return DateTime.Now.Year - yearBorn; 30 } 31 } 32 Class Person implements the IAge interface Definition of Age property (required)

 2002 Prentice Hall. All rights reserved. Outline 7 Person.cs 33 // property Name implementation of interface IAge- must 34 public string Name 35 { 36 get 37 { 38 return firstName + " " + lastName; 39 } 40 } } // end class Person Definition of Name property (required)

 2002 Prentice Hall. All rights reserved. Outline 8 Tree.cs 1 // Fig ed1: Tree.cs 2 // Class Tree contains number of rings corresponding to its age. 3 using System; 4 5 public class Tree : IAge 6 { 7 private int rings; // number of rings in tree trunk 8 9 // constructor 10 public Tree( int yearPlanted ) 11 { 12 // count number of rings in Tree 13 rings = DateTime.Now.Year - yearPlanted; 14 } // increment rings 17 public void AddRing() 18 { 19 rings++; 20 } // property Age implementation of interface IAge - must 23 public int Age 24 { 25 get 26 { 27 return rings; 28 } 29 } Class Tree implements the IAge interfaceImplementation of Age property ( required)

 2002 Prentice Hall. All rights reserved. Outline 9 Tree.cs // property Name implementation of interface IAge - must 32 public string Name 33 { 34 get 35 { 36 return "Tree"; 37 } 38 } } // end class Tree Definition of Name property (required)

 2002 Prentice Hall. All rights reserved. Outline 10 InterfacesTest.c s 1 // Fig ed1: InterfacesTest.cs 2 // Demonstrating polymorphism with interfaces. 3 using System.Windows.Forms; 4 5 public class InterfacesTest 6 { 7 public static void Main( string[] args ) 8 { 9 Tree tree = new Tree( 1978 ); 10 Person person = new Person( "Bob", "Jones", 1971 ); // create array of IAge references 13 IAge[] iAgeArray = new IAge[ 2 ]; // iAgeArray[ 0 ] refers to Tree object polymorphically 16 iAgeArray[ 0 ] = tree; // iAgeArray[ 1 ] refers to Person object polymorphically 19 iAgeArray[ 1 ] = person; // display tree information 22 string output = tree + ": " + tree.Name + "\nAge is " + 23 tree.Age + "\n\n"; // display person information 26 output += person + ": " + person.Name + "\nAge is: " 27 + person.Age + "\n\n"; 28 Create array of IAge referencesAssign an IAge reference to reference a Tree object Assign an IAge reference to reference a Person object

 2002 Prentice Hall. All rights reserved. Outline // display name and age for each IAge object in iAgeArray 30 foreach ( IAge ageReference in iAgeArray ) 31 { 32 output += ageReference.Name + ": Age is " + 33 ageReference.Age + "\n"; 34 } MessageBox.Show( output, "Demonstrating Polymorphism" ); } // end method Main } // end class InterfacesTest Use foreach loop to access each element of the array To verify all previous outputs use polymorphism to call the property of the appropriate class Software Engineering Observation 12.5 The is-a relationship that exists between base classes and derived classes, and between interfaces and the classes that implement them, holds when passing an object to a method. When a method parameters receives an argument of a base class or interface type, the method polymorhically process the object received as an argument.

Jozef Goetz contribution, Software Engineering Observations Software Engineering Observation 12.4 Inheritance and interfaces are similar in their implementation of the is-a relationship. An object of a class that implements an interface may be thought of as an object of that interface type. Software Engineering Observation 12.6 All methods of class object can be called by using a reference of an interface type—the reference refers to an object, and all objects inherit the methods of class object.

Jozef Goetz contribution, Case Study: Creating and Using Interfaces  An interface can inherit from one or more base interfaces.  In the following example, the interface IMyInterface inherits from two interfaces, Ifase1 and Ifase2: public interface IMyInterface: Ifase1, Ifase2 { void MethodA(); void MethodB(); }  interfaces can be implemented by classes. The identifier of the implemented interface appears in the class base list. public class Class1: Iface1, Iface2 { // class members }  When a class base list contains a base class and interfaces, the base class comes first in the list. class ClassA: BaseClass, Iface1, Iface2 { // class members }

 2002 Prentice Hall. All rights reserved. Outline 14 IShape.cs 1 // Fig ed1: IShape.cs 2 // Interface IShape for Point, Circle, Cylinder Hierarchy. 3 4 public interface IShape 5 { 6 // classes that implement IShape must implement these methods 7 // and this property 8 double Area(); 9 double Volume(); 10 string Name { get; } 11 } Definition of IShape interfaceClasses implementing the interface must define methods Area and Volume (each of which take no arguments and return a double) and a read-only string property Name (since “get”)

 2002 Prentice Hall. All rights reserved. Outline 15 Point3.cs 1 // Fig ed1: Point3.cs 2 // Point3 implements interface IShape and represents 3 // an x-y coordinate pair. 4 using System; 5 6 // Point3 implements IShape 7 public class Point3 : IShape // object Point3 is an IShape 8 { 9 private int x, y; // Point3 coordinates // default constructor 12 public Point3() 13 { 14 // implicit call to Object constructor occurs here 15 } // constructor 18 public Point3( int xValue, int yValue ) 19 { 20 X = xValue; 21 Y = yValue; 22 } // property X 25 public int X 26 { 27 get 28 { 29 return x; 30 } 31 Class Point3 implements the IShape interface

 2002 Prentice Hall. All rights reserved. Outline 16 Point3.cs 32 set 33 { 34 x = value; 35 } 36 } // property Y 39 public int Y 40 { 41 get 42 { 43 return y; 44 } set 47 { 48 y = value; 49 } 50 } // return string representation of Point3 object 53 public override string ToString() 54 { 55 return "[" + X + ", " + Y + "]"; 56 } // must implement interface IShape method Area 59 public virtual double Area() 60 { 61 return 0; 62 } 63 Implementation of IShape method Area (required), declared virtual to allow deriving classes to override

 2002 Prentice Hall. All rights reserved. Outline 17 Point3.cs 64 // must implement interface IShape method Volume 65 public virtual double Volume() 66 { 67 return 0; 68 } // must implement property Name of IShape 71 public virtual string Name 72 { 73 get 74 { 75 return "Point3"; 76 } 77 } } // end class Point3 Implementation of IShape method Volume (required), declared virtual to allow deriving classes to override Implementation of IShape property Name (required), declared virtual to allow deriving classes to override

 2002 Prentice Hall. All rights reserved. Outline 18 Circle3.cs 1 // Fig ed1: Circle3.cs 2// Circle3 inherits from class Point3 and overrides key members: // Area, Name 3 using System; // and implements interface IShape implicitly 4 5 // Circle3 inherits from class Point3 6 public class Circle3 : Point3 7 { 8 private double radius; // Circle3 radius 9 10 // default constructor 11 public Circle3() 12 { 13 // implicit call to Point3 constructor occurs here 14 } // constructor 17 public Circle3( int xValue, int yValue, double radiusValue ) 18 : base( xValue, yValue ) 19 { 20 Radius = radiusValue; 21 } // property Radius 24 public double Radius 25 { 26 get 27 { 28 return radius; 29 } 30 Definition of class Circle3 which inherits from Point3

 2002 Prentice Hall. All rights reserved. Outline 19 Circle3.cs 31 set 32 { 33 // ensure non-negative Radius value 34 if ( value >= 0 ) 35 radius = value; 36 } 37 } // calculate Circle3 diameter 40 public double Diameter() 41 { 42 return Radius * 2; 43 } // calculate Circle3 circumference 46 public double Circumference() 47 { 48 return Math.PI * Diameter(); 49 } // calculate Circle3 area 52 public override double Area() 53 { 54 return Math.PI * Math.Pow( Radius, 2 ); 55 } // return string representation of Circle3 object 58 public override string ToString() 59 { 60 return "Center = " + base.ToString() + 61 "; Radius = " + Radius; 62 } 63 Override the Point3 implementation of Area

 2002 Prentice Hall. All rights reserved. Outline 20 Circle3.cs 64 // override property Name from class Point3 65 public override string Name 66 { 67 get 68 { 69 return "Circle3"; 70 } 71 } } // end class Circle3 Override the Point3 implementation of Name

 2002 Prentice Hall. All rights reserved. Outline 21 Cylinder3.cs 1 // Fig ed1: Cylinder3.cs 2 // Cylinder3 inherits from class Circle2 and overrides key members. 3 using System; // and implements interface IShape implicitly 4 5 // Cylinder3 inherits from class Circle3 6 public class Cylinder3 : Circle3 7 { 8 private double height; // Cylinder3 height 9 10 // default constructor 11 public Cylinder3() 12 { 13 // implicit call to Circle3 constructor occurs here 14 } // constructor 17 public Cylinder3( int xValue, int yValue, double radiusValue, 18 double heightValue ) : base( xValue, yValue, radiusValue ) 19 { 20 Height = heightValue; 21 } // property Height 24 public double Height 25 { 26 get 27 { 28 return height; 29 } 30 Declaration of class Cylinder3 which inherits from class Circle3

 2002 Prentice Hall. All rights reserved. Outline 22 Cylinder3.cs 31 set 32 { 33 // ensure non-negative Height value 34 if ( value >= 0 ) 35 height = value; 36 } 37 } // calculate Cylinder3 area 40 public override double Area() 41 { 42 return 2 * base.Area() + base.Circumference() * Height; 43 } // calculate Cylinder3 volume 46 public override double Volume() 47 { 48 return base.Area() * Height; 49 } // return string representation of Cylinder3 object 52 public override string ToString() 53 { 54 return "Center = " + base.ToString() + 55 "; Height = " + Height; 56 } 57 Override the Circle3 implementation of Area Override the Point3 implementation of Volume

 2002 Prentice Hall. All rights reserved. Outline 23 Cylinder3.cs 58 // override property Name from class Circle3 59 public override string Name 60 { 61 get 62 { 63 return "Cylinder3"; 64 } 65 } } // end class Cylinder3 Override the Circle3 implementation of Name

 2002 Prentice Hall. All rights reserved. Outline 24 Interfaces2Test. cs 1 // Fig ed1: Interfaces2Test.cs 2 // Demonstrating polymorphism with interfaces in 3 // Point-Circle-Cylinder hierarchy. 4 5 using System.Windows.Forms; 6 7 public class Interfaces2Test 8 { 9 public static void Main( string[] args ) 10 { 11 // instantiate Point3, Circle3 and Cylinder3 objects 12 Point3 point = new Point3( 7, 11 ); 13 Circle3 circle = new Circle3( 22, 8, 3.5 ); 14 Cylinder3 cylinder = new Cylinder3( 10, 10, 3.3, 10 ); // create array of IShape interface references 17 IShape[] arrayOfShapes = new IShape[ 3 ]; // arrayOfShapes[ 0 ] references Point3 object 20 arrayOfShapes[ 0 ] = point; // arrayOfShapes[ 1 ] references Circle3 object 23 arrayOfShapes[ 1 ] = circle; // arrayOfShapes[ 2 ] references Cylinder3 object 26 arrayOfShapes[ 2 ] = cylinder; string output = point.Name + ": " + point + "\n" + 29 circle.Name + ": " + circle + "\n" + 30 cylinder.Name + ": " + cylinder; 31 Create an array of IShape references Assign an IShape reference to reference a Point3 object Assign an IShape reference to reference a Circle3 object Assign an IShape reference to reference a Cylinder3 object

 2002 Prentice Hall. All rights reserved. Outline 25 Interfaces2Test. cs Program Output 32 foreach ( IShape shape in arrayOfShapes ) 33 { 34 output += "\n\n" + shape.Name + ":\nArea = " + 35 shape.Area() + "\nVolume = " + shape.Volume(); 36 } MessageBox.Show( output, "Demonstrating Polymorphism" ); 39 } 40 } Use polymorphism to call the appropriate class’s method or property

Jozef Goetz contribution, Fig | Common interfaces of the.Net Framework Class Library. (Part 1 of 2)

Jozef Goetz contribution, Fig | Common interfaces of the.Net Framework Class Library. (Part 2 of 2)

Jozef Goetz contribution, Operator Overloading  C# contains many operators (such as + and - ) that are defined for some primitive types  It is often useful to use operators with user- defined types (e.g., a complex number class)  Operator notation may often be more intuitive then method calls  C# allows programmers to overload operators to make them sensitive to the context in which they are used

Jozef Goetz contribution, Operator Overloading  Methods define the actions to be taken for the overloaded operator  They are in the form: public static ReturnType operator operator-to-be-overloaded (arguments) Example: public static ComplexNumber operator + (ComplexNumber x, ComplexNumber y )  These methods must be declared public and static  The Return type is the type returned as the result of evaluating the operation  The keyword operator follows the return type to specify that this method defines an operator overload  The last piece of information is the operator to be overloaded (such as +, -, *, etc.)  If the operator is unary, one argument must be specified, if the operator is binary, then two, etc.

Jozef Goetz contribution, 2016

© by Pearson Education, Inc. All Rights Reserved.

Jozef Goetz contribution, 2016

 2002 Prentice Hall. All rights reserved. Outline 35 ComplexNumber.cs 1 // Fig ed1: or Fig.12.7 ed3. ComplexNumber.cs 2 // Class that overloads operators for adding, subtracting 3 // and multiplying complex numbers. 4 5 public class ComplexNumber 6 { 7 private int real; 8 private int imaginary; 9 10 // default constructor 11 public ComplexNumber() {} // constructor 14 public ComplexNumber( int a, int b ) 15 { 16 Real = a; 17 Imaginary = b; 18 } // return string representation of ComplexNumber 21 public override string ToString() 22 { 23 return "( " + real + 24 ( imaginary < 0 ? " - " + ( imaginary * -1 ) : 25 " + " + imaginary ) + "i )"; 26 } // property Real 29 public int Real 30 { 31 get 32 { 33 return real; 34 } 35 Class ComplexNumber definition Property Real provides access to the real part of the complex number You may ignore the previous edition solution:

 2002 Prentice Hall. All rights reserved. Outline 36 ComplexNumber.cs 36 set 37 { 38 real = value; 39 } } // end property Real // property Imaginary 44 public int Imaginary 45 { 46 get 47 { 48 return imaginary; 49 } set 52 { 53 imaginary = value; 54 } } // end property Imaginary // overload the addition operator 59 public static ComplexNumber operator + ( 60 ComplexNumber x, ComplexNumber y ) 61 { 62 return new ComplexNumber( 63 x.Real + y.Real, x.Imaginary + y.Imaginary ); 64 } 65 Property Imaginary provides access to the imaginary part of a complex number Overload the addition (+) operator for ComplexNumbers.

 2002 Prentice Hall. All rights reserved. Outline 37 ComplexNumber.cs 66 // provide alternative to overloaded + operator for addition 67 // for.NET languages that don’t support operator overloading 68 public static ComplexNumber Add( ComplexNumber x, 69 ComplexNumber y ) 70 { 71 return x + y; 72 } // overload the subtraction operator 75 public static ComplexNumber operator - ( 76 ComplexNumber x, ComplexNumber y ) 77 { 78 return new ComplexNumber( 79 x.Real - y.Real, x.Imaginary - y.Imaginary ); 80 } // provide alternative to overloaded – operator for subtraction 83 // for.NET languages that don’t support operator overloading 84 public static ComplexNumber Subtract( ComplexNumber x, 85 ComplexNumber y ) 86 { 87 return x - y; 88 } // overload the multiplication operator 91 public static ComplexNumber operator * ( 92 ComplexNumber x, ComplexNumber y ) 93 { 94 return new ComplexNumber( 95 x.Real * y.Real - x.Imaginary * y.Imaginary, 96 x.Real * y.Imaginary + y.Real * x.Imaginary ); 97 } Method Add – provides an alternative to the addition operator Overload the subtraction (-) operator for ComplexNumbers Method Subtract – provides an alternative to the subtraction operator Overloads the multiplication (*) operator for ComplexNumbers

 2002 Prentice Hall. All rights reserved. Outline 38 ComplexNumber.cs // provide alternative to overloaded * operator for multiplication 100 // for.NET languages that don’t support operator overloading 101 public static ComplexNumber Multiply( ComplexNumber x, 102 ComplexNumber y ) 103 { 104 return x * y; 105 } } // end class ComplexNumber Method Multiply – provides an alternative to the multiplication operator

 2002 Prentice Hall. All rights reserved. Outline 39 OperatorOverload ing.cs 1 // Fig ed1: OperatorOverloading.cs 2 // An example that uses operator overloading 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; public class ComplexTest : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.Label realLabel; 14 private System.Windows.Forms.Label imaginaryLabel; 15 private System.Windows.Forms.Label statusLabel; private System.Windows.Forms.TextBox realTextBox; 18 private System.Windows.Forms.TextBox imaginaryTextBox; private System.Windows.Forms.Button firstButton; 21 private System.Windows.Forms.Button secondButton; 22 private System.Windows.Forms.Button addButton; 23 private System.Windows.Forms.Button subtractButton; 24 private System.Windows.Forms.Button multiplyButton; private ComplexNumber x = new ComplexNumber(); 27 private ComplexNumber y = new ComplexNumber(); [STAThread] 30 static void Main() 31 { 32 Application.Run( new ComplexTest() ); 33 } 34

 2002 Prentice Hall. All rights reserved. Outline 40 OperatorOverload ing.cs 35 private void firstButton_Click( 36 object sender, System.EventArgs e ) 37 { 38 x.Real = Int32.Parse( realTextBox.Text ); 39 x.Imaginary = Int32.Parse( imaginaryTextBox.Text ); 40 realTextBox.Clear(); 41 imaginaryTextBox.Clear(); 42 statusLabel.Text = "First Complex Number is: " + x; 43 } private void secondButton_Click( 46 object sender, System.EventArgs e ) 47 { 48 y.Real = Int32.Parse( realTextBox.Text ); 49 y.Imaginary = Int32.Parse( imaginaryTextBox.Text ); 50 realTextBox.Clear(); 51 imaginaryTextBox.Clear(); 52 statusLabel.Text = "Second Complex Number is: " + y; 53 } // add complex numbers 56 private void addButton_Click( object sender, System.EventArgs e ) 57 { 58 statusLabel.Text = x + " + " + y + " = " + ( x + y ); 59 } // subtract complex numbers 62 private void subtractButton_Click( 63 object sender, System.EventArgs e ) 64 { 65 statusLabel.Text = x + " - " + y + " = " + ( x - y ); 66 } 67 Use addition operator to add two ComplexNumbers Use subtraction operator to subtract two ComplexNumbers

 2002 Prentice Hall. All rights reserved. Outline 41 OperatorOverload ing.cs Program Output 68 // multiply complex numbers 69 private void multiplyButton_Click( 70 object sender, System.EventArgs e ) 71 { 72 statusLabel.Text = x + " * " + y + " = " + ( x * y ); 73 } } // end class ComplexTest Use multiplication operator to multiply two ComplexNumbers

Jozef Goetz contribution, 2016 Summary  Derived-class object “is a” base-class object through inheritance  Use base-class reference to refer to derived-class object  Abstract classes  Cannot be instantiated  Too generic to define real objects  Used as base classes from which other classes may inherit (concrete classes)  Incomplete  derived-classes fill in "missing pieces“  Concrete classes  Classes from which objects are instantiated  Even though abstract classes cannot be instantiated, we can use abstract class references to refer to instances of any concrete class derived from the abstract class 42

Jozef Goetz contribution, 2016 Summary  Sealed classes (opposite to abstract class) cannot have any derived-classes  cannot be a base class  all methods in sealed class are sealed implicitly  can be instantiated 43  Interfaces specify the public services (methods and properties) that classes must implement  no default implementation to inherit  Interfaces provide:  no instance variables and  no default method implementations  vs. abstract classes which may provide some default implementations  Classes that implement an interface, must provide implementations for every method and property in the interface definitio n

Jozef Goetz contribution, 2016 Summary Inheritance and interfaces are similar in their implementation of the is-a relationship. An object of a class that implements an interface may be thought of as an object of that interface type.  When a class base list contains a base class and interfaces, the base class comes first in the list. class ClassA: BaseClass, Iface1, Iface2 { // class members } 44