Title: CS304: Lecture 5
1CS304 Lecture 5
- Operator Overloading, OOP
- Deitel, Chs. 11 12
- http//www.cis.uab.edu/cs304
2Contents
- Introduction
- Unary operator overloading
- Binary operator overloading
- Conversion operator and constructor
- Overloading and --
3What is Operator Overloading?
- Treating the words separately
- Operator A symbol that has some action on
surrounding operands - Overload To allow different meanings to exist
based on context of use - Operator Overloading Defining new meanings for
existing operators in C
4Why would we want Operator Overloading?
- class matrix
-
- int main() matrix a, b, cc.times(a,
b)c.print()
5Why would we want Operator Overloading?
- class matrix
-
- int main() matrix a, b, cc a bcout ltlt
c
6What Operators Can Be Overloaded?
- - /
- ! lt gt -
- / ltlt gtgt gtgt
- ltlt ! lt gt
- -- -gt , -gt ()
- new delete new delete
7How Does Overloading Change Things?
- Short answer Only interpretation changes
- Long answer Precedence, Associativity, and
Number of Operands are not changeable (and you
wouldnt want to).
8What Other Restrictions?
- You cannot create your own operators.
- You cannot overload operators of fundamental
types. Overloading is only intended for classes. - You can, however, create operators that take a
fundamental type and a class - Related operators are not necessarily overloaded
( overloading wont change the meaning of )
9How are these defined?
- There are two ways As class members, and as
global functions. - Class Members
- Certain operators have to be designated within
the class, such as (), , -gt, or any assignment
operator - The left operand must be of the type of the class
for the operator to be put into a class - Global Functions (normally friends of the class)
- Use this if the left operand is not of the class
you want to put it in (example ltlt and gtgt)
10Overloading Unary Operators
- As a global function, one argument
- bool operator! (const String )
-
-
- As a class member, the argument is provided via
this, so it does not require any parameters - bool operator!() const
11Overloading Binary Operators
- Once again, within a class, one fewer parameters
is required. The left operand is the one
indicated by this - Within a class
- bool operatorlt( const obj )
- Ouside of a class
- bool operatorlt(const obj1 , const obj2 )
12ltlt and gtgt example
- operatorgtgt and operatorltlt are the stream
extraction and insertion operators - They take the argument on the right and an
istream or ostream reference on the left, then
return an istream or ostream reference - We cant alter the ostream or istream classes, so
we have to make these into global functions
13Overloading gtgt and ltlt
- Imagine we have a class called phone number,
which inputs and outputs a phone number as
(xxx)xxx-xxxx, but stores the phone number as
areacode, exchange, and line - We want a gtgt operator that takes stuff off the
stream and automatically parses it, and
reconstitutes it in the ltlt operator
14- class PhoneNumber friend ostream
operatorltlt(ostream , const PhoneNumber
)friend istream operatorgtgt(istream ,
PhoneNumber )public string getAreaCode()
return areaCode private string areaCode,
exchange, line
15- ostream operatorltlt(ostream output, const
PhoneNumber number) output ltlt ( ltlt
number.areaCode ltlt ) ltlt number.exchange ltlt
- ltlt number.line return output
16- istream operatorgtgt(istream input, PhoneNumber
number) input.ignore() //skip ( input gtgt
setw(3) gtgt number.areaCode input.ignore(2)
//skip ) and space input gtgt setw(3) gtgt
number.exchange input.ignore() //skip
dash input gtgt setw(4) gtgt number.line return
input
17Conversion Constructors and Operators
- Casting for the masses!
- For class Type
- Type(Anothertype at) AnotherType
gt Type - operator AnotherType () Type gt
AnotherType - Convert a PhoneNumber into a String?
- class PhoneNumber operator string ()
return ( areaCode ) exchange
- line - For converting other things to a string, just
define a conversion operator It works as
intended. - Javas toString() ???
18Problems with Conversion Operators
- The compiler may use the conversion operators you
provide to implicitly cast things - This could become problematic, leading to bugs
that are hard to track down. - Use the explicit keyword to prevent these
troubles. - Dont forget constructors can be used as casting
operators These can be designated as explicit
as well.
19- class Array friend void outputArray(const
Array)public Array( int 10
)private //Array data -
- void outputArray(const Array x)
-
20- class Array friend void outputArray(const
Array)public explicit Array( int 10
)private //Array data -
- void outputArray(const Array x)
-
21What would happen here?
- int main() Array integers1( 7)outputArray(inte
gers1)outputArray(3)outputArray(Array(3))ret
urn 0
22Overloading and --, Prefix and Postfix
- Prefix looks like other unary operators
- Obj operator (Obj ) global function
- Obj operator() class member of Obj
- The post fix is more of a hack. In order to
distinguish it with prefix one, by convention,
the prototype looks like this - Obj operator (Obj , int) global function
- Obj operator (int) class member of Obj
23Chapter 12
- Object Oriented Programming Inheritance
24Inheritance
- Inheritance is a form of software reuse in which
the programmer creates a class that absorbs an
existing classs data and behaviors and enhances
them with new capabilities. - Saves time by preventing rewritten code
- Reduces errors by encouraging reuse of debugged
code
25The Inheritance Relationship
Base Class
Derived Class A
Derived Class B
26Vocabulary
- An existing class that is inherited from is the
base class. - A class that inherits from another class is a
derived class. - A direct base class is the base class from which
a derived class explicitly inherits. - An indirect base class is inherited from two or
more levels up in the class hierarchy.
27Vocabulary (contd)
- Single Inheritance means that a class is derived
from one base class. (This is Javas style of
inheritance) - Multiple Inheritance allows a derived class to
inherit from multiple (possibly unrelated)
classes. C allows both.
28Inheritance
- Inheritance exhibits an is-a relationship.
- This is in contrast to object composition, which
was the has-a relationship - As an example, consider a class rectangle and a
class quadrilateral - What is the relationship between the two?
- Other examples
- Students, Shapes, Employee Etc.
29Types of Inheritance
- Public
- Our main focus
- Private members of a base class are not
accessible directly from a derived class, but
these private members are still inherited - Friends are not inherited
- Public members remain public
- Protected members are still protected
30Types of Inheritance (contd)
- Protected Inheritance
- Public and protected members are protected in
derived class, private remains hidden - Private Inheritance
- Public and protected members are private in
derived class, private remains hidden - Public is the most common type of inheritance
31Protected Members
- Protected is an access modifier (like public and
private) - Intermediate level of protection
- Protected members are unavailable to other
classes (like private), but are accessible by
derived classes
32Inheritance example
- class Petpublic Pet() weight(1), food("Pet
Chow") Pet() void setWeight(int w)
weight w int getWeight() return
weight void setfood(string f) food
f string getFood() return food void
eat() void speak()protected int
weight string food
33- void Peteat() cout ltlt "Eating " ltlt food ltlt
endl -
- void Petspeak()cout ltlt "Growl" ltlt endl
34Public Inheritance Example
- class Shape
-
- class TwoDimensionalShape public Shape
35Derived Class Rat
- class Rat public Pet public Rat() Rat()
void sicken() cout ltlt "Speading Plague"
ltlt endl
36Derived Class Cat
- class Cat public Pet public Cat()
numberToes(5) Cat() void
setNumberToes(int toes) numberToes
toes int getNumberToes() return
numberToesprivate int numberToes
37in action! (Inaction?)
- int main() Rat charles Cat fluffycharles.setW
eight(25)cout ltlt "Charles weighs " ltlt
charles.getWeight() ltlt " lbs. " ltlt
endlcharles.speak() charles.eat()
charles.sicken() fluffy.speak() fluffy.eat()
cout ltlt "Fluffy has " ltlt fluffy.getNumberToes()
ltlt " toes " ltlt endl return 0
38Construction/Destruction
- When constructing a derived class member, the
constructor for the parent class is either
explicitly or implictly called - Explicit base-class member initializer
- Implicit default constructor
- Example If Pet(int weight) were another
constructorRat(int weight) Pet(weight)