OOP Lecture 4 - V2
OOP Lecture 4 - V2
L E C T U R E 4 : S TAT I C VA R I A B L E S A N D M E M B E R
FUNCTIONS, INLINE FUNCTIONS, OBJECT
A R R AY
public: cout << "Static member function is called through Object name: \
n" << endl;
static void print() {
b.print();
cout << "The value of the length is: " << length << endl;
cout << "\nStatic member function is called through Class
cout << "The value of the breadth is: " << breadth << endl;
name: \n" << endl;
cout << "The value of the height is: " << height << endl; }};
Box::print();
// initialize the static data members
return 0; }
int Box :: length = 10;
STATIC MEMBER FUNCTION IN C++
Static members belong to the class rather than an individual object. They are shared among all
objects of the class.
Static Data Members
• Declared using static, and memory is allocated only once for the entire class.
• Must be defined outside the class.
Static Member Functions
• Can access only static members.
• Cannot access instance-specific data members.
INLINE FUNCTION
INLINE FUNCTIONS IN C++
• In C++, a function can be specified as inline to reduce the function call overhead.
The whole code of the inline function is inserted or substituted at the point of its
call during the compilation instead of using normal function call mechanism.
• Syntax:
• We just need to add the “inline” keyword before the function prototype:
inline return_type function_name(params)...
INLINE EXAMPLE
inline void displayNum(int num) { // second function call
cout << num << endl; displayNum(8);
}
int main() { // third function call
// first function call displayNum(666);
displayNum(5); return 0;
}
INLINE EXAMPLE
INLINE FUNCTIONS IN C++
Inlining is only a request to the compiler, not a command. The compiler may not
perform inlining in such circumstances as:
1. If a function contains a loop.
2. If a function contains static variables.
3. If a function is recursive.
4. If a function contains a switch statement.
NEED OF INLINE FUNCTIONS
• When a function is called, the CPU stores the return address, copies arguments to the stack, and
transfers control to the function. After execution, the return value is stored, and control is returned
to the caller.
• This overhead can be significant for small, frequently used functions, as their execution time is
less than the time spent on the call and return process.
• This is where the inline functions shine. They remove this overhead by substituting the code of
the function in place of function call.
NEED OF INLINE FUNCTIONS
• One other thing to remember is that it is only useful to make the function inline if the time spent
during a function call is more compared to the function body execution time.
INLINE FUNCTIONS IN CLASS
• All the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline
functions are also applied here. If you need to explicitly declare an inline function in the class,
then just declare the function inside the class and define it outside the class using the inline
keyword.
std::cout << "Name: " << name << ", Age: " << age
<< std::endl;
ADVANTAGES OF ARRAY OF OBJECTS
• The array of objects represent storing multiple objects in a single name.
• In an array of objects, the data can be accessed randomly by using the index
number.
• Reduce the time and memory by storing the data in a single variable.
DISADVANTAGES OF ARRAY OF OBJECTS
• The main disadvantage with arrays of objects is that the constructor runs for each
object. If the default constructor doesn’t do the right thing for multiple objects in
the array, we have to specify the constructor arguments for each element at the
time the array is declared, which we wouldn’t have to do if we allocated an array
of object pointers, and allocated objects as needed.