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

Inline Function

The document discusses four C++ concepts: 1. Inline functions, which are expanded in line when called to avoid function call overhead for small functions. 2. Friend functions, which can access private members of a class and are often used to swap values of objects. 3. Static data members, which have only one copy shared among all class objects and are initialized to zero. 4. Static member functions, which can only access static data members and are called using the class name instead of an object.

Uploaded by

Teerth Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

Inline Function

The document discusses four C++ concepts: 1. Inline functions, which are expanded in line when called to avoid function call overhead for small functions. 2. Friend functions, which can access private members of a class and are often used to swap values of objects. 3. Static data members, which have only one copy shared among all class objects and are initialized to zero. 4. Static member functions, which can only access static data members and are called using the class name instead of an object.

Uploaded by

Teerth Bhardwaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

• Inline functions

• Friend function and classes


•Static data members
•Static member functions

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Inline functions
 Every time a function is called, it takes a lot of
extra time in executing series of instructions.
 Solution for small functions is inline functions
 Inline function: A function that is expanded in line
when it is invoked.
 The compiler replaces the function call with the
corresponding function code.

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Inline functions
 Syntax-
inline function-header
{
function body;
}
 Example:
inline double cube (double a)
{
return(a*a*a);
}
c=cube(3.0); // invoke function

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Inline functions
 inline keyword only sends a request, not a command to
the compiler .
 compiler may ignore this request if the function is too
long and compile it as a normal function.
 inline expansion may not work if:
 a loop, a switch or a goto exists.
 contain static variables.
 functions are recursive.

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Friend function and classes
 A normal function that is declared with the keyword
friend inside the class is called friend function.
 It can be common to two classes.
 A friend function has following characteristics:
 It is invoked like a normal function, not with object of
the class.
 It can access the private members of the class by using
the object of that class.
 It can be declared anywhere in the class.
 Generally, it has objects as arguments.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Friend function and classes
 Example-
class ABC; //forward class declaration
class XYZ
{
int x;
public:
XYZ(int y)
{
x=y;
}
void show_x()
{
cout<<x<<“\n”;
}
friend void swap(XYZ &, ABC &); //swap values of two variables
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Friend function and classes
class ABC
{
int a;
public:
ABC(int b)
{
a=b;
}
void show_a()
{
cout<<a<<“\n”;
}
friend void swap(XYZ &, ABC &); //swap values of two variables
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Friend function and classes
void swap(XYZ & r, ABC & s) //friend
function
{
int t=r.x;
r.x=s.a;
s.a=t;
}

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Friend function and classes
int main()
{
XYZ obj1(5);
ABC obj2(10);
obj1.show_x();
obj2.show_a();
swap(obj1,obj2); //calling friend function
obj1.show_x();
obj2.show_a();
return 0;
}
Output-
5
10
10
5 Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Friend function and classes
 A member function of a class can be friend function of
another class.
 For example
class A
{
void fun(); // member function of A
};
class B
{
- ------
friend void A::fun();
--------
};
 The function fun() is a member function of class A and
friend function of class B.

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Friend function and classes
 If all the member function of one class are declared as
friend functions in another class, then the class is called
friend class.
 For example-
class C
{
- ------
friend class A; //All member functions of class A are friend
to class C.
- ------
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Static data member
 A data member declared with static keyword.
 Characteristics of static data member-
 It is initialized to zero when the first object of its class
is created.
 Only one copy of that member is created for the entire
class and is shared by all the objects of that class.
 It is visible only within the class but its life time is the
entire program.

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Static data member
 Example-
class item
{
static int count; //count is static
int number;
public:
void getdata(int a)
{ number=a;
count++; }
void getcount(void)
{ cout<<”count:” <<count<<“\n”; }
};
int item :: count ; //definition of static data member

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Static data member
int main( )
{
item a,b,c; // count is initialized to zero
a.getcount( ); // display count
b.getcount( );
c.getcount( ):
a.getdata( 10):
b.getdata( 20);
c.getdata( 30);
cout<<“After reading data”<<“\n”;
a.getcount( ); // display count
b.getcount( );
c.getcount( ):
return 0;
} Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Static data member
 Output
count:0
count:0
count:0
After reading data
count: 3
count:3
count:3
 While defining static data member, some initial value can
also be assigned. For example-
int item :: count =10;
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Static member function
 A member function declared with static keyword.
 Properties of static member functions-
 It can access only other static data members and
member functions in the same class.
 They are invoked using the class name.

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Static member function
 Example-
class test
{
int x;
static int y;
public:
void set_xy(int a)
{ x=a; y++; }
void show_x(void)
{ cout<<“x=”<<x<<“\n”; }
static void show_y(void)
{ cout<<“y=”<<y; }
};
int test :: y; //define static data member
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer
Static member function
int main()
{
test t1, t2;
t1.set_xy(10);
t2.set_xy(20);
t1.show_x();
t2.show_x();
test::show_y(); // calling static function
return 0;
}
 Output
x=10
x=20
y=2

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer
Thank You

Prepared by: Anil Kumar Tailor, Assistant Prof.,


Engineering College Ajmer

You might also like