• No results found

• Accessing data members and member functions

N/A
N/A
Protected

Academic year: 2022

Share "• Accessing data members and member functions"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay

Session: Operator Overloading

(2)

IIT Bombay

• Object-oriented programming with structures and classes

• Accessing data members and member functions

• Constructors and destructors

• Function calls with structures and classes

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Customizing operators for classes

• Operator overloading

• Assignment overloading

Overview of This Lecture

(4)

IIT Bombay

Acknowledgment

• Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++

by Abhiram G. Ranade

McGraw Hill Education 2014

• Examples taken from this book are indicated in slides by the citation AGRBook

(5)

IIT Bombay

Motivating Operator Overloading

class V3 { private:

double x, y, z;

public:

… Constructor, destructor, other member functions … V3 sum (const V3 &b) {

V3 v;

v.x = x + b.x; v.y = y + b.y; v.z = z + b.z; return v;

}

Recall Class V3

(6)

IIT Bombay

Recall Motion Simulator Motivating Operator Overloading

int main() {

V3 vel, acc, pos;

V3 currDispl, currPos;

double t, deltaT, totalT;

… Some code here … while (t <= totalT) {

currDispl = (vel.scale(t)).sum(acc.scale(0.5*t*t));

currPos = currDispl.sum(pos);

t = t + deltaT;

}

… Some code here …

Isn’t that too clumsy?

(7)

IIT Bombay

Motivating Operator Overloading

int main() {

V3 vel, acc, pos;

V3 currDispl, currPos;

double t, deltaT, totalT;

… Some code here … while (t <= totalT) {

currDispl = (vel * t) + 0.5 * (acc * (t*t));

currPos = currDispl + pos;

t = t + deltaT;

}

… Some code here …

Can we write this instead?

(8)

IIT Bombay

Motivating Operator Overloading

• Normally + and * operators in C++ don’t operate on V3 objects as operands

• Can we “overload” their meaning to operate on V3 objects?

Yes, indeed! C++ provides a way of achieving this!!!

(9)

IIT Bombay

Understanding Infix Operators in C++

Suppose @ is an infix operator (e.g. +, -, /, %, …) In C++, the expression

X @ Y

is

equivalent to

X . operator@ (Y)

Written between operands, as in

X @ Y

Call to member function “operator@” of class of X Invoked on receiver object X

Parameter passed is object Y C++ keyword

(10)

IIT Bombay

Defining Custom Operators for Class V3

class V3 {

private: double x, y, z;

public:

… Constructor, destructor, other member functions … V3 operator+ (const V3 &b) {

return V3(x + b.x, y + b.y, z + b.z);

}

V3 operator* (const double factor) {

return V3(x*factor, y*factor, z*factor);

} };

Replaced “sum”

with “operator+”

Replaced “scale”

with “operator*”

(11)

IIT Bombay

Defining Custom Operators for Class V3

class V3 {

private: double x, y, z;

public:

… Constructor, destructor, other member functions … V3 operator+ (const V3 &b) const {

return V3(x + b.x, y + b.y, z + b.z);

}

V3 operator* (const double factor) const { return V3(x*factor, y*factor, z*factor);

} };

Preferable to use const.

Denotes that member function cannot change

receiver object

(12)

IIT Bombay

C++ Program With Overloaded Operators

int main() {

V3 vel, acc, pos;

V3 currDispl, currPos;

double t, deltaT, totalT;

… Some code here … while (t <= totalT) {

currDispl = (vel * t) + 0.5 * (acc * (t*t));

currPos = currDispl + pos;

t = t + deltaT;

}

… Some code here …

Legitimate C++ code

Invoking member function operator*

This appears problematic!

Recall: X@Y and X.operator@(Y)

(13)

IIT Bombay

Another Overloading Technique

• C++ also allows us to define operator@ as an ordinary (non- member) function, and use @ as an infix operator in

expressions

V3 operator* (const double factor, const V3 &b) { return (b * factor);

}

(14)

IIT Bombay

Another Overloading Technique

• C++ also allows us to define operator@ as an ordinary (non- member) function, and use @ as an infix operator in

expressions

V3 operator* (const double factor, const V3 &b) { return (b * factor);

}

Note the order of typed operands.

Allows (factor * b) to be evaluated

Invoking member function.

(15)

IIT Bombay

C++ Program With Overloaded Operators

int main() {

V3 vel, acc, pos;

V3 currDispl, currPos;

double t, deltaT, totalT;

… Some code here … while (t <= totalT) {

currDispl = (vel * t) + 0.5 * (acc * (t*t));

currPos = currDispl + pos;

t = t + deltaT;

}

… Some code here …

Legitimate C++ code

Invoking member function operator*

Invoking non-member function operator*

(16)

IIT Bombay

Operators That Can Be Overloaded

• Almost all operators that you care about

Binary: + - * / % ^ & | < > == != <= >= << >> && ||

= += -= *= /= %= ^= &= |= <<= >>= []

Unary: + - * & ! ~ ++ --

Note the assignment operators

(17)

IIT Bombay

Assignment Operator

• Unlike several other operators, the assignment operator (=) is defined for all classes/structures

V3 a(1.0, 2.0. 3.0);

V3 b;

b = a;

Copy values of all data

members of a to corresponding

data members of b

(18)

IIT Bombay

Assignment Overloading

• We can re-define the assignment operator for a class/struct by defining the member function

operator=

(lhs = rhs) as an assignment expression is equivalent to

lhs.operator=(rhs)

• Definition of member function operator= similar to copy constructor, except that operator= must also return a

value (like all assignment expressions)

(19)

IIT Bombay

Assignment Overloading Example [Ref AGRBook]

class Queue{ private: int front, nWaiting, elements[100];

public:

Queue & operator=(const Queue &rhs) {

front = rhs.front; nWaiting = rhs.nWaiting;

for (int i = front, j = 0; j < nWaiting; j++) {

elements[i] = rhs.elements[i]; i = (i + 1) % 100;

}

return *this;

}

… Other member functions …

Inside a member function,

“this” denotes a pointer to

the receiver object

(20)

IIT Bombay

Summary

• Operator overloading in C++ as a programming convenience

• Assignment overloading as a special case of operator overloading

References

Related documents

Recall: Assuming Lipschitz continuity on gradient ∇ f and convexity of f and assuming bounded iterates and assuming convexity of C (and therefore of I C ) we obtained O(1/k)

• Specification : what the function is supposed to do Typical form: If the arguments satisfy certain properties, then a certain value will be returned, or a certain action will

• A pointer to a variable can be created in the main program and dereferenced during a function call. • This way a function can be allowed to modify variables in the main program,

member function precondition: should be satisfied before the execution of the member function. member function postcondition: should be satisfied after the execution of the

Static member function not invoked on object of class Point. int main

Abstract definition of a class (complete with data members and member functions) with generic data types of members..

Member functions that return values of only those data members that other functions are allowed to read. class

void initialize(struct Account *acc) {acc-&gt;balance=0;} // inlined member function void deposit (struct Account *acc, int amount) { acc-&gt;balance+=amount; }. void