C operator overloading
Download
1 / 14

C++ Operator Overloading - PowerPoint PPT Presentation


  • 174 Views
  • Updated On :

C++ Operator Overloading. Gordon College CPS212. Function overloading. Function overloading enables writing more then one function with the same name but different signature. int my_func(int a, int b) { return(a * b); } int my_func(int a, int b, int c) { return( a * b * c); }

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C++ Operator Overloading' - carissa


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
C operator overloading l.jpg

C++ Operator Overloading

Gordon College

CPS212


Function overloading l.jpg
Function overloading

Function overloading enables writing more then

one function with the same name but different signature.

int my_func(int a, int b) { return(a * b); }

int my_func(int a, int b, int c) { return( a * b * c); }

my_func(2,4);

my_func(2,4,6);


Function overloading3 l.jpg
Function overloading

int Add(int nX, int nY) { return nX + nY; }

double Add(double dX, double dY) { return dX + dY; }

int Add(int nX, int nY, int nZ) { return nX + nY + nZ; }

ERROR (signature must be unique in some way)

int GetRandomValue();

double GetRandomValue();


Operator overloading l.jpg
Operator overloading

  • Some languages allow only function (method) overloading - such as Java.

  • However, on a primitive level all languages use operator overloading to some degree.

    • Consider

      4 + 6

      3.4 + 5.6

      “Cows “ + “can jump”


Operator overloading5 l.jpg
Operator overloading

  • Operator overloading doesn’t add power to code - it essentially enhances the coding experience (efficiency for the programmer)

  • Adds logical depth to ADTs:

    • If there is a new type called “BirthdayList”, shouldn’t it possible to add, substract, compare lists and display using intuitive operators?

      listA == listB

      listA + Jim

      listA - Marvin

      cout << listA


Operator overloading6 l.jpg
Operator overloading

  • The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function).


Operator overloading7 l.jpg
Operator overloading

  • FRIEND (free – yet is associated with class – inline or external function)

    friendconstPointoperator+ (constPoint&, constint&);

    friendostream & operator<<( ostream&, constPoint &);

  • CLASS

    constPoint& operator= (constPoint&);

  • FREE

    constPointoperator- (constPoint&, constPoint&);

Within Class Definition


Operator overloading8 l.jpg
Operator overloading

  • FRIEND (free that is allowed access to private variables of class – Friend not used here)

    constPointoperator+ (constPoint& lhs, constint& v)

    {

    return Point(lhs._x + v, lhs._y + v);

    }

    ostream& operator<<( ostream& o, constPoint &p )

    {

    o << "(" << p.x() << "," << p.y() << ")";

    return o;

    }

Within Source File (not scoped for class)


Operator overloading9 l.jpg
Operator overloading

  • CLASS (use when necessary or more convenient)

    constPoint& Point::operator= (constPoint& rhs)

    {

    this->_x = rhs.x();

    this->_y = rhs.y();

    return *this;

    }

Within Source File (scoped for class)


Operator overloading10 l.jpg
Operator overloading

  • FREE (efficiency issues – must use accessor and mutator functions)

    constPointoperator- (constPoint& lhs, constPoint& rhs)

    {

    return Point(lhs.x() - rhs.x(), lhs.y() - rhs.y());

    }

Within Source File (not scoped for class)


Const l.jpg
Const

  • The five types of const prevent the following from modification:

    • const variable: the variable (local and global variables)

    • const argument: the argument

    • const return type: this only only applied to references to members of a class. Then, const can prevent the original member from being modified.

    • const method: all non-mutable class members

    • const member: the member (class member - once object is constructed - this value can’t change)


Const12 l.jpg
Const

  • For parameters to member function: the parameter value can not be changed.

    • Particularly useful when using the reference qualifier:

      voidaddCow(const Cow& heifer)

      {list_ = list_+heifer;}

      Otherwise - the function would have the ability to change the value of the argument being passed in.


Const13 l.jpg
Const

  • For class usage of a member function - the function can not change the values of any class variables

    voiddisplayBrands() const;


Const14 l.jpg
Const

  • The value of a return type that is declared const cannot be changed. This is especially useful when giving a reference to a class's internals.

    struct Values

    {

    const std::vector<int>& GetValues() const { return mV; }

    private:

    std::vector<int> mV;

    };

    C++ Structure and C++ class are exactly same except default access specifier of their members - in C++ Structure all members are public by default while in Class all are private.

    NOTE: Use const whenever possible.


ad