Nested Classes in C++ Last Updated : 23 Jul, 2025 Suggest changes Share Like Article Like Report A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed. For example, program 1 compiles without any error and program 2 fails in compilation. Program 1 CPP #include<iostream> using namespace std; /* start of Enclosing class declaration */ class Enclosing { private: int x; /* start of Nested class declaration */ class Nested { int y; void NestedFun(Enclosing *e) { cout<<e->x; // works fine: nested class can access // private members of Enclosing class } }; // declaration Nested class ends here }; // declaration Enclosing class ends here int main() { } Program 2 CPP #include<iostream> using namespace std; /* start of Enclosing class declaration */ class Enclosing { int x; /* start of Nested class declaration */ class Nested { int y; }; // declaration Nested class ends here void EnclosingFun(Nested *n) { cout<<n->y; // Compiler Error: y is private in Nested } }; // declaration Enclosing class ends here int main() { } References: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf K kartik Follow Article Tags : C++ Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++4 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like