Inline Functions, Static Class Members, Scope Resolution Operator, Nested Classes and Local Classes
Inline Functions,
Static Class Members, Scope Resolution Operator, Nested Classes and Local
Classes in C++
1.
Inline Functions
Definition
An inline
function is a function in which the compiler is requested to replace the
function call with the actual function code at the point of the call. This can
reduce the overhead associated with a normal function call, particularly for small
and frequently called functions.
The inline
keyword is used to request inline expansion.
Syntax
inline
return_type function_name(parameters)
{
// function body
}
Example
#include <iostream>
using namespace std;
inline int square(int n)
{
return n * n;
}
int main()
{
cout <<
"Square = " << square(5);
return 0;
}
Output
Square =
25
Important
Points
- The inline keyword requests
the compiler to perform inline expansion.
- It is generally suitable for small
functions.
- Inline expansion can reduce
function-call overhead.
- The compiler may ignore the
inline request when it considers expansion unsuitable.
- A function defined inside a
class definition is implicitly inline.
- Large, recursive, or complex
functions are generally not good candidates for inline expansion.
- inline does not guarantee
that the function will actually be expanded inline.
Example
of an Inline Member Function
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
};
Here,
add() is implicitly an inline member function because it is defined
inside the class.
2.
Static Class Members
A static
class member belongs to the class rather than to individual objects.
There are
two commonly discussed types:
- Static data member
- Static member function
2.1
Static Data Member
Definition
A static
data member has only one shared copy for the entire class, regardless of
how many objects of that class are created.
Syntax
class ClassName
{
static data_type
variable;
};
The static
data member is normally defined outside the class:
data_type
ClassName::variable = value;
Example
#include <iostream>
using namespace std;
class Student
{
public:
static int count;
Student()
{
count++;
}
};
int Student::count = 0;
int main()
{
Student s1;
Student s2;
Student s3;
cout <<
"Number of objects = " << Student::count;
return 0;
}
Output
Number of
objects = 3
Explanation
Here:
static int
count;
is shared
by all objects of the Student class.
When three
objects are created, the same count variable is incremented three times.
Important
Points
- Only one copy of a
static data member exists for the class.
- It is shared by all objects.
- It can be accessed using the
class name:
Student::count
- Traditionally, a static data
member requires an out-of-class definition when odr-used; modern C++ also
provides inline static data members, which can be defined directly inside
the class.
2.2
Static Member Function
Definition
A static
member function belongs to the class rather than to a particular object.
Syntax
class ClassName
{
public:
static return_type
functionName()
{
// function body
}
};
Example
#include <iostream>
using namespace std;
class Math
{
public:
static int square(int
n)
{
return n * n;
}
};
int main()
{
cout <<
Math::square(6);
return 0;
}
Output
36
Important
Points
A static
member function:
- Can be called using the class
name.
- Does not require an object for
invocation.
- Does not have a this pointer.
- Can directly access static
members of the class.
- Cannot directly access
non-static data members.
3.
Scope Resolution Operator (::)
Definition
The scope
resolution operator (::) is used to specify the scope to which an
identifier belongs.
It is one
of the important operators in C++ and is particularly useful for defining class
member functions outside the class.
Syntax
ClassName::memberName
Example
1: Defining a Member Function Outside the Class
#include <iostream>
using namespace std;
class Student
{
private:
int rollNo;
public:
void setRollNo(int r);
void display();
};
void Student::setRollNo(int r)
{
rollNo = r;
}
void Student::display()
{
cout <<
"Roll No: " << rollNo;
}
int main()
{
Student s;
s.setRollNo(101);
s.display();
return 0;
}
Output
Roll No:
101
Here:
void
Student::display()
means that
display() belongs to the Student class.
Major
Uses of ::
|
Use |
Example |
|
Define class member outside class |
void Student::display() |
|
Access static class member |
Student::count |
|
Access namespace member |
std::cout |
|
Access global variable when
hidden by local variable |
::x |
|
Access nested class/type |
Outer::Inner |
Example:
Global Variable
#include <iostream>
using namespace std;
int x = 100;
int main()
{
int x = 50;
cout <<
"Local x: " << x << endl;
cout <<
"Global x: " << ::x;
return 0;
}
Output
Local x:
50
Global x:
100
4.
Nested Classes
Definition
A nested
class is a class that is declared inside another class.
The class
containing the nested class is called the outer class, and the class
declared inside it is called the nested or inner class.
Syntax
class Outer
{
public:
class Inner
{
// members of
Inner
};
};
Example
#include <iostream>
using namespace std;
class Outer
{
public:
class Inner
{
public:
void display()
{
cout <<
"This is a nested class.";
}
};
};
int main()
{
Outer::Inner obj;
obj.display();
return 0;
}
Output
This is a
nested class.
Explanation
The nested
class is accessed using the scope resolution operator:
Outer::Inner
and an
object is created as:
Outer::Inner
obj;
Important
Points
- A nested class is defined
inside another class.
- It helps logically group a
class that is strongly related to its enclosing class.
- The nested class name is
within the scope of the outer class.
- The scope resolution operator
:: is used to refer to the nested class from outside.
- A nested class does not
automatically have access to a particular object of the outer class.
- A nested class can have its
own data members and member functions.
Real-Life
Example
A
University class could contain a nested Department class:
class University
{
public:
class Department
{
//
Department-related members
};
};
This
represents a logical relationship:
University
→ Department
5.
Local Classes
Definition
A local
class is a class that is declared inside a function or a local block.
Its scope
is limited to the block in which it is declared.
Syntax
void function()
{
class LocalClass
{
// members
};
LocalClass obj;
}
Example
#include <iostream>
using namespace std;
void display()
{
class Number
{
public:
void show()
{
cout <<
"This is a local class.";
}
};
Number n;
n.show();
}
int main()
{
display();
return 0;
}
Output
This is a
local class.
Important
Points
- A local class is declared
inside a function or block.
- Its scope is restricted to
that function/block.
- Its objects can normally be
created only within that scope.
- It is useful when a class is
required only for a particular function.
- A local class cannot have
static data members in the traditional sense; modern C++ has some nuanced
exceptions around static/inline members, but this is generally not needed
at introductory level.
- A local class cannot have
member templates.
- It cannot directly access
automatic local variables of the enclosing function unless those values
are passed to it or otherwise made available through permitted mechanisms.
Quick
Revision Table
|
Topic |
Meaning |
Main Keyword/Operator |
Main Feature |
|
Inline Function |
Small function requested for
inline expansion |
inline |
Can reduce function-call overhead |
|
Static Data Member |
Class-level shared variable |
static |
One shared copy for the class |
|
Static Member Function |
Class-level function |
static |
Can be called without an object |
|
Scope Resolution Operator |
Specifies scope of an identifier |
:: |
Defines/accesses class, namespace
and global members |
|
Nested Class |
Class declared inside another
class |
class + :: |
Provides logical grouping |
|
Local Class |
Class declared inside a
function/block |
class |
Scope limited to the
function/block |
Very
Short Exam Definitions
Inline
Function:
An inline function is a function for which the compiler is requested to
substitute the function body at the point of the function call.
Static
Class Member:
A static class member belongs to the class as a whole and is shared by all
objects of that class.
Scope
Resolution Operator:
The scope resolution operator :: is used to specify and access the scope of an
identifier.
Nested
Class:
A nested class is a class declared within another class.
Local
Class:
A local class is a class declared inside a function or block, whose scope is
restricted to that function or block.