Object-Oriented Programming
(OOP)
Week – 05
Feb 17-21, 2019
Instructor: Basit Ali
Email: [Link]@[Link]
Object-Oriented Programming (OOP)
this Pointer
• The this pointer holds the address of current object, in simple
words you can say that this pointer points to the current
object of the class.
Use?
1. When local variable’s name is same as member’s name
2. To return reference to the calling object
1) When local variable’s name is same as
member’s name
2) To return reference to the calling object
Inline Function
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
} //Output: The cube of 3 is: 27
Inline Function
• Remember, inlining is only a request to the compiler, not a command. Compiler can ignore
the request for inlining. Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement doesn’t exist in
function body.
5) If a function contains switch or goto statement.
Inline functions provide following advantages
1. Function call overhead doesn’t occur.
2. It also saves the overhead of push/pop variables on the stack when function is called.
3. It also saves overhead of a return call from a function.
4. When you inline a function, you may enable compiler to perform context specific
optimization on the body of function. Such optimizations are not possible for normal
function calls. Other optimizations can be obtained by considering the flows of
calling context and the called context.
5. Inline function may be useful (if it is small) for embedded systems because inline
can yield less code than the function call preamble and return.
Array of objects
books book[size] ;
for(int i=0; i<size; i++)
{
Rectangle *ptr_arr[2];
cout<<"Enter details of book "<<(i+1)<<"\n";
ptr_arr[0] = new Rectangle(10,20);
book[i].getdata(); ptr_arr[1] = new Rectangle(15,5);
}
//declaration array of objects
//with parameterized constructor
• Number N[3]={Number(10),Number(20),Number(30)};