1. Nested class
The perimeter class needs to use a nested class object as the underlying implementation, and the nested class is used only for the implementation of the perimeter class, and the underlying implementation can be hidden from the user at the same time.
From a scope perspective, nested classes are hidden in the perimeter class, which can only be used in the perimeter class. If you use the class name in a scope other than the perimeter class, you need to add a name qualifier.
A member function in a nested class can be defined outside its class body.
A member function of a nested class has no access to the private members of the perimeter class, and vice versa.
Nested classes are only syntactically embedded.
2. Local class
A class can also be defined in a function body, and such a class is called a local class (Loacl Class). A local class is visible only within the local domain in which it is defined.
The member functions of the local class must be defined in the class body.
A static member function cannot be in a local class.
In practice, local classes are rarely used.
Here's a piece of code to illustrate:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class Outer
{
Public
Class Inner
{
Public
void Fun ();
};
Public
Inner obj_;
void Fun ()
{
cout<< "Outer::fun ..." <<endl;
Obj_. Fun ();
}
};
void Outer::inner::fun ()
{
cout<< "Inner::fun ..." <<endl;
}
void Fun ()
{
Class Localclass
{
Public
int num_;
void Init (int num)
{
Num_=num;
}
void Display ()
{
cout<< "Num_" <<num_<<endl;
}
};
Localclass LC;
Lc. Init (10);
Lc. Display ();
}
int main ()
{
Outer o;
O.fun ();
Outer::inner i;
I.fun ();
Fun ();
return 0;
}
Run Result: