Classes and Objects:
Operator Overloading
Chapter 10 of Visual C# How to Program, 6/e
© 2018 Pearson Education, Ltd. All Rights Reserved.
Operator Overloading; Introducing struct
You can create operators that work with objects of your
own types—via a process called operator overloading.
You can overload most operators.
© 2018 Pearson Education, Ltd. All
Rights Reserved.
Creating Value Types with struct
C#’s simple numeric types are value types.
C#’s simple types like int and double are actually aliases for
struct types—an int is defined by the struct
System.Int32, a long by System.Int64, a double by
System.Double and so on.
Operator overloading also can be applied to classes.
© 2018 Pearson Education, Ltd. All
Rights Reserved.
Creating Value Types with struct
When to Declare a struct Type
Microsoft recommends using classes for most new types,
but recommends a struct if:
the type represents a single value (one memeber)
the size of an object is 16 bytes or smaller
© 2018 Pearson Education, Ltd. All
Rights Reserved.
Creating Value Types with struct
Valuetype ComplexNumber (Fig. 10.14) overloads the plus
(+), minus (-) and multiplication (*) operators to enable
programs to add, subtract and multiply instances of class
ComplexNumber using common mathematical notation.
© 2018 Pearson Education, Ltd. All
Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating Value Types with struct
Important Points
Unlike a class, you cannot define a parameterless
constructor for a struct.
The compiler always provides a default constructor that
initializes the struct’s instance variables to their
default values.
© 2018 Pearson Education, Ltd. All
Rights Reserved.
Creating Value Types with struct
Overloading Guide
Keyword operator, followed by an operator symbol (such
as +), indicates that a method overloads the specified
operator.
Overloaded operator methods are required to be public
and static.
Methods that overload binary operators must take two
arguments—the first is the left operand and the second is
the right operand.
© 2018 Pearson Education, Ltd. All
Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
10.13.3 Class ComplexTest
Class
ComplexTest (Fig. 10/15) demonstrates the
overloaded operators for adding, subtracting and
multiplying ComplexNumbers.
© 2018 Pearson Education, Ltd. All
Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Object-Oriented Programming:
Inheritance
Chapter 11 of Visual C# How to Program, 6/e
© 2018 Pearson Education, Ltd. All Rights Reserved.
Introduction
Inheritance allows a new class to absorb an existing class’s
members.
A derived class normally adds its own fields and methods to
represent a more specialized group of objects.
Inheritance saves time by reusing proven and debugged high-quality
software.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Introduction (Cont.)
The direct base class is the base class which the derived class
explicitly inherits.
An indirect base class is any class above the direct base class in the
class hierarchy.
The class hierarchy begins with class object.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Introduction (Cont.)
The is-a relationship represents inheritance.
Don’t forget the has-a relationship represents composition
(previous lecture).
For example, a car is a vehicle, and a truck is a vehicle.
New classes can inherit from thousands of pre-built classes in class
libraries.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Base Classes
and Derived
Classes
several simple examples of base classes and derived
classes.
Note that base classes are “more general,” and
derived classes are “more specific.”
© 2018 Pearson Education, Ltd. All Rights Reserved.
Base Classes and
Derived Classes
(Cont.)
The UML class diagram shows an inheritance
hierarchy representing a university community.
Each arrow represents an is-a relationship.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Base Classes and
Derived Classes
(Cont.)
Now consider the Shape hierarchy in Fig. 11.3.
We can follow the arrows to identify the is-a
relationships.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Base Classes and Derived Classes (Cont.)
Objects of all classes that extend a common base class can be treated
as objects of that base class.
However, base-class objects cannot be treated as objects of their
derived classes.
A derived class can customize methods it inherits from its base class.
In such cases, the derived class can override (redefine) the base-
class method with an appropriate implementation.
© 2018 Pearson Education, Ltd. All Rights Reserved.
protected Members
A base class’s private members are inherited by derived classes,
but are not directly accessible by derived-class methods and
properties.
A base class’s protected members can be accessed by members of
that base class and by members of its derived classes, but not by
clients of the class.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Base Class
الجماهير العريضة Public Members Derived Class
Protected Members
Private Members
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating and Using a CommissionEmployee Class
CommissionEmployee (Fig. 11.4) represents an employee
who is paid a percentage of their sales.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating and Using a CommissionEmployee Class (cont.)
A colon (:) followed a class name at the end of the class declaration header
indicates that the class extends the class to the right of the colon.
Every C# class directly or indirectly inherits object’s methods.
If a class does not specify that it inherits from another class, it implicitly
inherits from object.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating and Using a CommissionEmployee Class (cont.)
Declaring instance variables as private and providing public properties to
manipulate and validate them helps enforce good software engineering.
Constructors are not inherited.
Either explicitly or implicitly, a call to the base-class constructor is made.
Class object’s default (empty) constructor does nothing.
Even if a class does not have constructors, the default constructor will call the
base class’s default or parameterless constructor.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating and Using a CommissionEmployee Class (cont.)
CommissionEmployee Method ToString
Method ToString is special—it’s one of the methods that every class inherits directly
or indirectly from class object.
Method ToString returns a string representing an object.
Class object’s ToString method is primarily a placeholder that typically should be
overridden by a derived class.
To override a base-class method, a derived class must declare a method with keyword
override.
The method must have the same signature (method name, number of parameters and
parameter types) and return type as the base-class method.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating and Using a CommissionEmployee Class (cont.)
Figure 11.5 tests class CommissionEmployee.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Object-Oriented Programming:
More Inheritance
Chapter 11 of Visual C# How to Program, 6/e
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating a BasePlusCommissionEmployee Class
without Using Inheritance
The Bad Programmer !
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating a BasePlusCommissionEmployee Class without Using Inheritance
(cont.)
Note the similarity between this class and class Commission
Employee (Fig. 11.4)—in this example,
Class BasePlusCommissionEmployeeTest
Figure 11.7 tests class BasePlusCommissionEmployee.
we do not yet exploit that similarity.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating a CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy
Now we declare class BasePlusCommissionEmployee
(Fig. 11.8), which extends class CommissionEmployee
(Fig. 11.4).
The Good Programmer !
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Mark Them as Protected
Override The virtual
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating a CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy (cont.)
BasePlusCommissionEmployeeMethod Earnings
The virtual and abstract keywords indicate that a base-
class method can be overridden in derived classes.
The override modifier declares that a derived-class method
overrides a virtual or abstract base-class method.
This modifier also implicitly declares the derived-class
method virtual.
We need to declare CommissionEmployee’s Earnings
method virtual.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Creating a CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy (cont.)
The compiler generates additional errors because base class CommissionEmployee’s
instance variables are private.
The errors can be prevented by using the public properties inherited from class
CommissionEmployee.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Fixing the Errors
Forthe next example, we made class
CommissionEmployee’s instance variables protected.
We also declared the Earnings method virtual so that
BasePlusCommissionEmployee can override the method.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Constructors in Derived Classes
• The derived-class constructor, before performing its own tasks,
invokes its direct base class’s constructor.
• This is done either explicitly or implicitly.
• If the base class is derived from another class, the base-class
constructor invokes the constructor of the next class up in the
hierarchy, and so on.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Software Engineering with Inheritance
Effectivesoftware reuse improves the software-
development process.
Object-oriented programming facilitates software reuse,
potentially shortening development time.
The availability of substantial and useful class libraries
helps deliver the maximum benefits of software reuse
through inheritance.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Class object
All classes inherit directly or indirectly from the object class.
Figure 11.12 summarizes object’s methods.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
© 2018 Pearson Education, Ltd. All Rights Reserved.
Thank U
© 2018 Pearson Education, Ltd. All Rights Reserved.