Static Member Function in C++
Static Member Function in C++
Programming Techniques
Revision
Constant Member Variable Non-Constant Function Possible
Constant Member Variable Non-Constant Object Possible
Constant Member Function Non-Constant Object Possible
Constant Member Function Constant Object Possible
Constant Member Function Non-Constant Object Possible
Constant Object Constant Member Function Possible
Constant Object Non-Constant Member Function Not Possible
Static Member Variable Non-Constant Object Possible
Static Member Variable Static Member Function Possible
Non-Static Member Variable Static Member Function Not Possible
Revision: Static Member Function Key
Points
• A static member function can only access static member data, static
member functions and data and functions outside the class
• You must take note not to use static member function in the same manner
as non-static member function, as non-static member function can access
all of the above including the static data member
• A non-static member function can be called only after instantiating the
class as an object whereas static member functions can be called even
when a class is not instantiated
3
Static Member Function
• Syntax
static returnType functionName() {}
Function call
className ::functionName()
Output
Array of Object
• The Array of Objects stores objects. An array of a class type is also
known as an array of objects
• Syntax:
ClassName ObjectName[number of objects];
• Example:
Employee e[50];
Array of Object
Output
Array of Object
• Other way for array of object with similar output
Array of Object
Output
Dynamically Allocate Array of Object
Output
Passing Object as an Argument
• To pass an object as an argument we write the object name as the
argument while calling the function the same way we do it for other
variables
Syntax:
• function_name(object_name);
Object as Argument
Output
Dry Run for Output
Passing Object as an Argument
Output
Returning Object from Function
Returning
Object from
Function
Output
Passing and Returning
Object from Function
Output