CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

virtual double getArea() const = 0; private: string shapeType; Example ... Although overloading && and || is legal, shot circuit evaluation feature may be lost ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 37
Provided by: yanz
Category:

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 6
  • C Operator Overloading

2
Objectives
  • In this lecture, you will learn
  • Abstract methods and classes
  • Slicing and Type Conversion
  • Basics of operator overloading
  • Overloading I/O
  • Global vs. Class Operators
  • What can be overloaded?
  • A Rational Number Class

3
Abstract Methods and Classes
4
Abstract Methods and Classes
  • In C, a method is abstract if
  • It is declared virtual
  • The declaration is followed by 0
  • A class is abstract if it has at least one
    abstract method an abstract class cannot be
    instantiated

5
Example
  • class Shape
  • public
  • Shape(const string s)shapeType(s)
  • virtual Shape()
  • const string getType() const
  • return shapeType
  • virtual double getArea() const 0
  • private
  • string shapeType

6
Example
  • Class Circle public Shape
  • public
  • Circle(double r)Shape(Circle), radius( r)
  • double getArea() const
  • return 3.14 radius radius
  • private
  • double radius

7
Slicing
8
Slicing
  • Circle c(3, 100, 80)
  • Point p(10, 20)
  • P c
  • p.print()

9
Type Conversion
  • Cast down the inheritance hierarchy
  • Point p new Circle(3, 100, 80)
  • ((Circle ) p)-gtradius
  • What happens if p is actually pointing at a Point
    at runtime?

10
Dynamic Cast
  • Runtime checked cast in an inheritance hierarchy
  • Finding the beginning of an object
  • Circle c dynamic_castltCircle gt (p)

11
Static Cast
  • Only allowed for conversions that the compiler
    can check
  • Cannot cast a pointer to non-pointer
  • Cannot cast to remove const-ness
  • Cannot cast from a virtual base class

12
Basics of Operator Overloading
13
Operator Overloading
  • Redefine the existing operators to work with new
    class types
  • For example, overloading
  • ltlt, gtgt

14
Example 1
  • class Person
  • public
  • Person(int s, const string n )ssn(s),
    name(n)
  • const string getName() const
  • return name
  • int getSsn() const
  • return ssn
  • bool equals(const Person rhs) const
  • return ssn rhs.ssn
  • private
  • const int ssn
  • string name

15
Example 2
  • class Person
  • public
  • Person(int s, const string n )ssn(s),
    name(n)
  • const string getName() const
  • return name
  • int getSsn() const
  • return ssn
  • bool equals(const Person rhs) const
  • return ssn rhs.ssn
  • bool operator (const Person rhs) const
  • return equals(rhs)
  • private
  • const int ssn
  • string name

16
Overloading I/O
17
Example 1
  • class Person
  • public
  • Person(int s, const string n )ssn(s),
    name(n)
  • const string getName() const
  • return name
  • int getSsn() const
  • return ssn
  • void print(ostream out cout) const
  • out ltlt ltlt ssn ltlt, ltlt name ltlt
  • private
  • const int ssn
  • string name

18
Overload operatorltlt
  • If defined in the Person class,
  • operatorltlt(ostream out) const
  • Must be invoked as p.operatorltlt(cout) instead of
    cout ltlt p
  • Change the ostream class?
  • Define non-class function operator ltlt

19
Example 2
  • class Person
  • public
  • Person(int s, const string n )ssn(s),
    name(n)
  • const string getName() const
  • return name
  • int getSsn() const
  • return ssn
  • void print (ostream out) const
  • out ltlt ltlt ssn ltlt , ltlt name ltlt
  • private
  • const int ssn
  • string name
  • ostream operatorltlt (ostream out, const Person
    p)
  • p.print(out)
  • return out

20
Global vs. Class Operators
21
Discussion
  • An operator can be overloaded either as a member
    function or as a non-member function
  • If its overloaded as a member function, then the
    first operand (lhs) must be of the class type
    (this is lhs)

22
Discussion
  • Disadvantage of overloading operator as
    non-member
  • Cannot access the private data member of the
    class unless it is declared a friend
  • ostream operator ltlt (ostream out, const
    Person p)
  • out ltlt ltlt p.ssn ltlt , ltlt p.name ltlt
  • return out
  • Will it compile?

23
Discussion
  • Problem with overloading operator as member is
    lhs must match
  • bool operator(const Person rhs) const
  • return ssnrhs.ssn
  • Person(123456789, Bob)
  • cout ltlt (p1 123) ltlt endl //ok?
  • cout ltlt (123 p1) ltlt endl //ok?
  • What about overload operator as non-member
  • bool operator(const Person lhs, const Person
    rhs)
  • return lhs.equals(rhs)

24
Discussion
  • bool operator! (int lhs, const Person rhs)
  • bool Personoperator! (const Person rhs) const

25
What Can Be Overloaded?
26
Discussion
  • In C, only four operators cannot be overloaded
  • Dot .
  • .
  • ?
  • sizeof

27
Discussion
  • Although overloading and is legal, shot
    circuit evaluation feature may be lost
  • Overloaded operator must have the same precedence
    and associativity
  • Avity cannot be changed
  • Only existing operator can be overloaded

28
Rational Number Class
29
Ration Number
  • include Rational.h
  • include ltiostreamgt
  • using namespace std
  • int main()
  • Rational x
  • Rational sum 0
  • int n 0
  • cout ltlt Type in rationals ltlt endl
  • for (sum 0, n 0 cin gtgt x sumx, n)
  • cout ltlt Read ltlt x ltlt endl
  • cout ltlt Read ltlt n ltlt rationals ltlt endl
  • cout ltlt Average is ltlt (sum/n) ltlt endl
  • return 0

Sample input 3 4/5 7/2
30
Declaration
  • ifndef RATIONAL_H
  • define RATIONAL_H
  • include ltiostramgt
  • using namespace std
  • class Rational
  • public
  • Rational (int numerator 0) number
    (numerator), denom(1)
  • Rational(int numerator, int denominator)
    number(numerator), deno(denominator)
  • fixSigns() reduce()

31
Declaration
  • // Assignment Ops
  • const Rational operator (const Rational
    rhs)
  • const Rational operator -(const Rational
    rhs)
  • const Rational operator /(const Rational
    rhs)
  • const Rational operator (const Rational
    rhs)
  • //unary Operators
  • const Ratinal operator()
  • Rational operator(int)
  • const Ratinal operator--()
  • Rational operator--(int)
  • const Ratinal operator() const
  • Rational operator-() const
  • bool operator!() const

32
Declaration
  • // Name member functions
  • double toDouble() const
  • return static_castltdoublegt(numer) /denom
  • int toInt() const
  • return numergt 0? Numer/denom -(-numer/denom)
  • bool isPositive() const return numer gt 0
  • bool isNegative() constreturn numer lt0
  • Bool isZero() const return numer 0
  • void print(ostream out cout) const
  • private
  • int numer
  • int denom
  • void fixSigns() //ensures denom gt 0
  • void reduce() //ensures lowest form

33
Declaration
  • // Math Binary Ops
  • Rational operator(const Rational lhs, const
    Rational rhs)
  • Rational operator-(const Rational lhs, const
    Rational rhs)
  • Rational operator/(const Rational lhs, const
    Rational rhs)
  • Rational operator(const Rational lhs, const
    Rational rhs)
  • //Relational Equality Ops
  • bool operatorlt (const Rational lhs, const
    Rational rhs)
  • bool operatorlt (const Rational lhs, const
    Rational rhs)
  • bool operatorgt (const Rational lhs, const
    Rational rhs)
  • bool operatorgt (const Rational lhs, const
    Rational rhs)
  • bool operator (const Rational lhs, const
    Rational rhs)
  • bool operator! (const Rational lhs, const
    Rational rhs)
  • //I/O
  • ostream operatorltlt (ostream out, const Rational
    value)
  • istream operatorgtgt (istream in, Rational
    value)
  • endif

34
Things to Remember
  • Operator overloading redefines meanings for
    existing operators
  • If an operator is overloaded as a member
    function, the first operand is this and the
    subsequent are parameters. The first operand
    must match the class type
  • If an operator is overloaded as a non-member
    function, all operands are parameters, but the
    overloaded operator cannot access private members
    of any class
  • The most common operators to overload include
    assignment operators, equality and relational
    operators and the input and output stream
    operators.

35
Things to Remember
  • Operatorltlt should be overloaded for all class
    types, typically by providing a companion print
    member function
  • Operator and operator must be overloaded
    separately, and consistently. Typically operator
    simply invokes operator
  • If operator is overloaded, typically both a
    prefix and a postfix version are provided
  • If operator (array operator) is overloaded,
    typically both an accessor and a mutator version
    are provided

36
Possible Quiz Questions
  • Which operators cannot be overloaded?
  • Explain the difference between overloading
    operator as a member function vs. a nonmember
    function.
  • When must an operator be overloaded as a
    nonmember function?
Write a Comment
User Comments (0)
About PowerShow.com