C++ local classes

C++ Local classes

classes can be defined and used inside a function or a block classes Example

void test(int a) // function
{
…..
…..
class student // local class
{
……
…… // class definition
};
…..
…..
student s1(a); // create student object
……… // use student object
}

local classes can use global variable(declared above in function) and static variables declared inside the function but cannot use automatic variables. The global variables name(variables) should be used with the (::)scope Resolution operator.

There are some of the restrictions in constructing local classes. They have static data members and member functions must be defined inside the local classes. They can’t have static data members and member functions must be defined inside the local classes. Enclosing function cannot access the private members of the local class.

However we can achieve this by declaring the enclosing function as a friend

An example of a local class is given as follows.

#include< iostream >
using namespace std;
void func()
{
class LocalClass
{
};
}
int main()
{
return 0;
}

In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class. A local class name can only be used in its function and not outside it. Also, the methods of a local class must be defined inside it only. A local class cannot have static data members but it can have static functions.

#include< iostream >
using namespace std;
void func()
{
class LocalClass
{
private:
int num;
public:
void showdata( int n)
{
num = n;
}
void setdata()
{
cout << “The number is “<< num; }
}
};
LocalClass obj;
obj.showdata(7);
obj.setdata();
}
int main()
{
cout<<“Demonstration of a local class”< func();
return 0;
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise