lec 4a.pdf
Document Details
Uploaded by FlexibleCarnelian8035
UET Lahore
Full Transcript
MtE-231 Data Structures and Object-Oriented Programming Week 4 Fall 2024 1 Last Lecture const objects const member functions const data members Member initializer lists Member access control Constructors and Destructors Copy constructor Shallow and Dee...
MtE-231 Data Structures and Object-Oriented Programming Week 4 Fall 2024 1 Last Lecture const objects const member functions const data members Member initializer lists Member access control Constructors and Destructors Copy constructor Shallow and Deep Copy Objects in memory and this pointer Memory Models and Abstraction Dynamic Memory Allocation The new and delete operators 2 This Lecture Operator Overloading Friend functions and classes 3 Operator Overloading Use existing operators for new data types or classes Re-define the behavior of operators for new data types or classes through special functions called operator functions. 4 Why Operator Overloading? int i, j, k; // integers The compiler overloads the + operator for built-in float m, n, p; // floats integer and float types by k = i + j; default, producing integer // integer addition and addition with i+j, and assignment floating addition with m+n. p = m + n; // floating addition and assignment Date a,b,c; Operator overloading c = a + b; allows us to achieve the fraction e,f,g; same for the new types e = f+g; that we define. 6 Syntax for Operator Functions The syntax for writing Examples: operator functions is: operator+ operator- operator@(argument-list) operator* operator/ operator is a keyword that specifies that the given function is an operator function. @ is replaced by the operator symbol (+, -, =, etc..) that we want to overload. 7 Implementing Operator Functions There are three ways of doing it: – As a member function – As a non-member function or – As a friend function the operator function may need to be declared as a friend if it requires access to protected or private data Expression obj1@obj2 translates into a function call – obj1.operator@(obj2), if this function is defined within class obj1 – operator@(obj1,obj2), if this function is defined outside the class obj1 8 Member Vs Non Member Function a member function is a function defined within a class that can access the class's private and protected data, while a non- member function is defined outside of any class and can only access public data unless it is declared as a friend. a member function is a function defined within a class that operates on the class's objects and has access to its members, while a non-member function is defined outside any class and does not have direct access to the class's private or protected members. Friend Function Implementing Operator Functions 1. Defined as a member function this function is defined within class class Complex {... public:... c = a+b; Complex operator +(const Complex &op) { double real = _real + op._real, c = a.operator+ (b); imag = _imag + op._imag; return(Complex(real, imag)); }... }; 1 2 Implementing Operator Functions 2. Defined as a non-member function this function is defined outside the class class Complex {... c = a+b; public:... double real() { return _real; } c = operator+ (a, b); //need access functions double imag() { return _imag; }... Complex operator +(Complex &op1, Complex &op2) }; { double real = op1.real() + op2.real(), imag = op1.imag() + op2.imag(); return(Complex(real, imag)); 9 } Implementing Operator Functions 3. Defined as a friend function class Complex {... c = a+b; public:... friend Complex operator +( c = operator+ (a, b); const Complex &, const Complex & ); Complex operator +(Complex &op1, Complex &op2)... { }; double real = op1._real + op2._real, imag = op1._imag + op2._imag; return(Complex(real, imag)); 10 } Overloading Unary Operators Class Counter – Pre-increment int Counter::operator ++(){ } – Post-increment int Counter::operator ++(int){} 15 What is a ‘Friend’? Friend declarations introduce extra coupling between classes – Once an object is declared as a friend of a class, it has access to all the non-public members of that class. Access is unidirectional – If B is designated as a friend of A, then only B can access A’s non-public members; A cannot access B’s.. A friend function of a class is defined outside of the scope of that class. 16 What is a ‘Friend’? Utility of friends – provide more efficient access to data members than accessor functions – accommodate operator functions with easy access to private data members Friends can have access to everything, which defeats data hiding, so use them carefully Friends have permission to change the internal state from outside the class. – Not a good idea. – Always recommend the use of set member functions instead of friends to change the state of an object. 17 That’s it for today. 18