0% found this document useful (0 votes)
25 views

Types of Class Member Functions in C++

1. There are different types of member functions in C++ including simple, inline, static, const, friend, and virtual functions. 2. Static member functions work for the entire class rather than individual objects and can be called using the class name rather than an object. They cannot access non-static data or call non-static functions. 3. Static data members are not instantiated for each object but rather shared among all class objects, existing for the lifetime of the program. They can be accessed without an object using the class name.

Uploaded by

Syed nasir mehdi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Types of Class Member Functions in C++

1. There are different types of member functions in C++ including simple, inline, static, const, friend, and virtual functions. 2. Static member functions work for the entire class rather than individual objects and can be called using the class name rather than an object. They cannot access non-static data or call non-static functions. 3. Static data members are not instantiated for each object but rather shared among all class objects, existing for the lifetime of the program. They can be accessed without an object using the class name.

Uploaded by

Syed nasir mehdi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Object Oriented Programming

Lecture 06

Imtiaz Ahmed
Types of Class Member Functions in C++
 We already know what member functions are, what they do, how to define
member functions, and how to call them using class objects. Now let’s learn
about some special member functions that can be defined in C++ classes.
Following are the different types of Member functions:
1. Simple functions (All the general member functions are called Simple Functions)
2. Inline functions (All the member functions defined inside the class definition are by default declared as Inline )

3. Static functions
4. Const functions
5. Friend functions

6. Virtual Functions
Static Member functions in C++
 Static is something that holds its position. Static is a keyword which can be
used with data members as well as the member functions. We will discuss
this in details later. As of now we will discuss its usage with member functions
only.

 A function is made static by using static keyword with function name. These
functions work for the class as whole rather than for a particular object of a
class.

 It can be called using the object and the direct member access . operator.
But, its more typical to call a static member function by itself, using class
name and scope resolution :: operator.
Static Member functions in C++
 A static member function in C++ can be defined by using the static keyword

 A static member function cannot define or access a non-static data member and cannot
call a non-static member function.

 A static member function need not be instantiated and can be called without creating any
object of the class by using the scope resolution operator(::).

 Static member functions can access only static data members and can only call other static
member functions of the class.
Static Data Members in C++
 A static data member in C++ is a data member defined in a class that is not instantiated with each object
created of the class. In C++, each object or instance of a class consists of its own set of data members that
are defined within the class. However, data members that are specified with the static keyword are not
instantiated for each object of the class and the class will have only one instance of that data member.

 Static data members in C++ are not associated with any object and can be accessed even without the
creation of any object. Static data members belong to the static storage class in C++ and as such have a
lifetime equal to the lifetime of the program.

 A static data member in C++ is declared within a class and is defined outside the class.

 A static data member in C++ can be accessed with the help of the scope resolution operator(::) or a static
member function.

 A constant static data member in C++ can be initialized within the same class in which it is defined.
Constant Static Members
 A non-const static data member in C++ cannot be initialized within the class in which it is declared. However,
if a static data member of integral type is declared const it can be initialized inside the class definition as in
the following example:
Things to remember about Static Data Members and Functions
 Persistence: it remains in memory until the end of the program.

 Visibility: if it is defined within a function/block, it's scope is limited to the function/block. It cannot be
accessed outside of the function/block. A variable declared static within the body of a function maintains its
value between invocations of the function.

 Class: static members exist as members of the class rather than as an instance in each object of the class. So,
this keyword is not available in a static member function. Such functions may access only static data
members. There is only a single instance of each static data member for the entire class:

 A static data member : class variable

 A non-static data member : instance variable

 Static member function: it can only access static member data, or other static member functions while non-
static member functions can access all data members of the class: static and non-static.
Example
class A{
public:
static int x;
A(){
x++;
}
};
int A::x = 0;
int main(){
A obj1, obj2, obj3;
cout<<A::x;
return 0;
}
Thank You

You might also like