Monday, 30 March 2026

Inline Member Functions in c++

 

Inline Member Functions
The keyword inline is used as a function specifier only in function declarations. The inline specifier is a hint to the compiler that inline substitution of the function body is to be preferred to the usual function call implementation. The advantages of using inline member functions are:
  • the size of the object code is considerably reduced,
  • it increases the execution speed, and
  • the inline member functions are compact function calls.
Example 16.
A program to illustrate an inline member function to read a data member of a class from the keyboard and to display it on the screen.

//inline demonstration
#include <iostream>
using namespace std;
class sample {
private :
int x;
public :
void getdata();
void display();
};
inline void sample:: getdata()
{
cout << “ enter a number ? \n”;
cin >> x;
}
inline void sample :: display()
{
cout << “ entered number is = “ << x ;
cout << endl;
}
int main()
{
sample obj;
obj.getdata();
obj.display();
return 0;
}
Output of the above program
enter a number?
10
entered number is = 10

Example 17.
A program to perform the simple arithmetic operations such as addition, subtraction, multiplication and division using a member function and these functions are defi ned as an inline substitution.

//demonstration of inline member function
#include <iostream>
using namespace std;
class sample {
private :
int x;
int y;
public :
inline void getinfo();
inline void display();
inline int sum();
inline int diff();
inline int mult();
inline oat div();
}; // end of class declaration
inline void sample :: getinfo()
{
cout << “ enter any two numbers ? “ << endl;
cin >> x >> y ;
}
inline void sample :: display()
{
cout << “ x = “ << x << endl;
cout << “ y = “ << y << endl;
cout << “ sum = “ << sum() << endl;
cout << “ dif = “ << diff() << endl;
cout << “ mul = “ << mult() << endl;
cout << “ div = “ << div() << endl;
}
inline int sample :: sum()
{
return(x+y);
}
inline int sample :: diff()
{
return(x-y);
}
inline int sample :: mult()
{
return(x*y);
}
inline oat sample :: div()
{
return(( oat)x / ( oat)y);
}
int main()
{
sample obj1;
obj1.getinfo();
obj1.display();
obj1.sum();
obj1.diff();
obj1.mult();
obj1.div();
return 0;
}
Output of the above program
enter any two numbers?
3 4
x = 3
y = 4
sum = 7
dif = -1
mul = 12
div = 0.75

Example 18.
A program to solve a quadratic equation using an object-oriented programming technique in which the member functions are defi ned as an inline substitution.

//solution of quadratic equation using object oriented programming
// methods are de ned as inline substitution
#include <iostream>
#include <cmath>
using namespace std;
class equation {
private :
oat a;
oat b;
oat c;
public :
inline void getinfo( oat a, oat b, oat c);
inline void display();
inline void equal( oat a, oat b);
inline void imag();
inline void real( oat a, oat b, oat det);
}; // end of class declaration
inline void equation ::getinfo( oat aa, oat bb, oat cc)
{
a = aa;
b = bb;
c = cc;
}
inline void equation :: display()
{
cout << endl;
cout << “ a = “ << a << ‘\t’;
cout << “ b = “ << b << ‘\t’;
cout << “ c = “ << c << endl;
}
inline void equation :: equal( oat a , oat b)
{
oat x;
x = -b/(2*a);
cout << “ roots are equal = “ << x << endl;
}
inline void equation :: imag()
{
cout << “ roots are imaginary \n”;
}
inline void equation :: real( oat a , oat b, oat det)
{
oat x1,x2,temp;
temp = sqrt(det);
x1 = (-b+temp)/(2*a);
x2 = (-b-temp)/(2*a);
cout << “ roots are real \n”;
cout << “ x1 = “ << x1 << endl;
cout << “ x2 = “ << x2 << endl;
}
int main()
{
class equation equ;
oat aa,bb,cc;
cout << “ enter three numbers \n”;
cin >> aa >> bb >> cc;
equ.getinfo(aa,bb,cc);
equ.display();
if ( aa == 0) {
oat temp;
temp = -cc/bb;
cout << “ linear roots = “ << temp << endl;
}
else {
oat det;
det = bb*bb-4*aa*cc;
if (det == 0)
equ.equal(aa,bb);
else if (det < 0)
equ.imag();
else
equ.real(aa,bb,det);
}
return 0;
} // end of main program
Output of the above program
enter three numbers
2 5 2
a = 2 b = 5 c = 2
roots are real
x1 = -0.5
x2 = -2
enter three numbers
0 2 1
a = 0 b = 2 c = 1
linear roots = -0.5

Static Data Member
Static data members are data objects that are common to all the objects of a class. They exist only once in all objects of this class. They are already created before the finite object of the respective class. The static members are used in information that is commonly accessible. This property of static data members, may lead a person to think that static members are basically global variables. This is not true. Static members can be any one of the groups: public, private and protected, but not global data. The normal access rules for class members are also valid for static member data. If static member of a class is public, it can used as a normal variable. The main advantage of using a static member is to declare the global data which should be updated while the program lives in the memory.

A static data member of a class has the following properties;
(1) The access rule of the data member of a class is same for the static data member also. For example, if a static member is declared as a private category of a class, then non-member functions cannot access these members. If a static member is declared as public, then any member of the class can access.

(2) Whenever a static data member is declared and it has only a single copy, it will be shared by all the instance of class. That is, the static member becomes global instances of the class.

(3) The static data member should be created and initialised before the main () function control block begins.

Example 19.
A program to demonstrate how an automatic initialisation of a static member is carried out and the contents of the variable displayed.

//automatic initialization of a static member
#include <iostream>
using namespace std;
class sample {
private:
static int counter;
public:
inline void display();
};
int sample :: counter;
inline void sample:: display()
{
cout << “ content of the static data member = “ << counter;
cout << endl;
}
int main()
{
sample obj;
obj.display();
return 0;
}
Output of the above program
content of the static data member = 0

Example 20.
A program to defi ne a static data member which has the initial value of 100 and to fi nd out the sum of the following series:
sum = 1+2+3...10
The summing of series is to be repeated five times. This program is to demonstrate how the compiler reserves the special storage allocation as a global space so that the content of a variable lives from the start of the program to the end.

//static data member
#include <iostream>
using namespace std;
class sample {
private :
static int counter;
public :
void display();
};
int sample::counter = 100;
void sample:: display()
{
int i;
for (i = 0; i <= 10; ++i) {
counter = counter+i;
}
cout << “ sum of the counter value = “ << counter;
cout << endl;
}
int main()
{
sample obj;
int i;
for ( i = 0; i < 5; ++i) {
cout << “ count = “ << i+1 << endl;
obj.display();
cout << endl;
}
return 0;
}
Output of the above program
count = 1
sum of the counter value = 155
count = 2
sum of the counter value = 210
count = 3
sum of the counter value = 265
count = 4
sum of the counter value = 320
count = 5
sum of the counter value = 375

Example 21.
A program to display how many instantiation of a class object has been created using static data member declaration. This program is to demonstrate how the static data member is used to keep track of the number of instantiation of a class created. Whenever a new object of the class is made, the static counter updates automatically.

//static data member
#include <iostream>
using namespace std;
class sample {
private :
static int count; // static data member declaration
public :
sample();
void display();
};
//static data de nition
int sample :: count = 0;
sample :: sample ()
{
++count;
}
void sample :: display()
{
cout << “ counter value = “ << count << endl;
}
int main()
{
sample obj1,obj2,obj3,obj4;
obj4.display();
return 0;
}
Output of the above program
counter value = 4

Static Member Functions
    The keyword static is used to precede the member function to make a member function static. The static function is a member function of class and the static member function can manipulate only on static data member of the class. The static member function acts as global for members of its class without affecting the rest of the program.
    The purpose of static member is to reduce the need for global variables by providing alternatives that are local to a class. A static member function is not part of objects of a class. Static members of a global class have external linkage. A static member function does not have a this pointer so it can access nonstatic members of its class only by using . or ->.
    The static member function cannot be a virtual function. A static or nonstatic member function cannot have the same name and the same arguments type. And further, it cannot be declared with the keyword const. The static member function is instance dependent, it can be called directly by using the class name and the scope resolution operator. If it is declared and defi ned in a class, the keyword static should be used only on the declaration part.

Example 22.
A program to check how many instances of the class object are created using the static member function.

//both static data member and static function member
#include <iostream>
using namespace std;
class sample {
private :
static int count; // static data member declaration
public :
sample();
static void display(); // static member function
};
//static data de nition
int sample :: count = 0;
sample :: sample ()
{
++count;
}
void sample :: display()
{
cout << “ counter value = “ << count << endl;
}
int main()
{
cout << “ before instantiation of the object “ << endl;
sample::display();
sample obj1,obj2,obj3,obj4;
cout << “ after instantiation of the object “ << endl;
sample::display();
return 0;
}
Output of the above program
before instantiation of the object
counter value = 0
after instantiation of the object
counter value = 4

Example 23.
A program to check how many instances of the class object are created using the static member function, where static member function is defi ned with inline code substitution.

//using inline member function
#include <iostream>
using namespace std;
class sample {
private :
static int count; // static data member declaration
public :
sample();
static void display(); // static member function
};
//static data de nition
int sample :: count = 0;
inline sample :: sample ()
{
++count;
}
inline void sample :: display()
{
cout << “ counter value = “ << count << endl;
}
int main()
{
cout << “ before instantiation of the object\n”;
sample::display();
sample obj1,obj2,obj3,obj4;
cout << “ after instantiation of the object\n”;
sample::display();
return 0;
}
Output of the above program
before instantiation of the object
counter value = 0
after instantiation of the object
counter value = 4

Example 24.
A program to demonstrate how a static data is accessed by a static member function.

//accessing static member function
#include <iostream>
using namespace std;
class alpha {
private:
static int x;
public :
alpha();
static void display() // static member function
{
cout << “ content of x “;
cout << “ after incrementd by one = “ << x << endl;
}
};
class beta {
private:
int y;
public :
void getdata()
{
cout << “ enter a value for y \n”;
cin >> y;
}
void display() //member function
{
cout << “ content of y = “ << this->y << endl;
}
};
int alpha:: x = 10;
alpha :: alpha()
{
++x;
}
int main()
{
alpha objx;
beta objy;
objy.getdata();
alpha::display();
objy.display();
return 0;
}
Output of the above program
enter a value for y
10
content of x after incremented by one = 11
content of y = 10

0 comments

Post a Comment