Nested Classes


One area we have not yet explored is the concept of nested classes. You can define a class within another class. You can also have a class that, within its functions, instantiates another class. In fact, you will frequently see the latter example of a nested class used in various programs. The first example, that of constructing a class totally within another class, is not as commonly used. That class, defined within another class, can only be used inside of that class. It cannot be executed from outside the class, even if you declare it as public. Let’s take a look at a class declared within another class.

Example 11.5

Step 1: Write the following code in your favorite text editor and save it as 11-05.h.

class baseclass { public:  void basefunca();      class nested      {        public:      void nestedfunca();      }; }; void baseclass::basefunca() {  cout << "This is a function in the base class\n";  nested myclass;  myclass.nestedfunca(); } void baseclass::nested::nestedfunca() {  cout << "This is a function in the nested class \n"; }

Step 2: Write this code into a text editor and save it as 11-05.cpp.

#include "11-05.h" #include <iostream> using namespace std;     int main() {  baseclass myclass;  myclass.basefunca();     return 0; } 

You can test the claim that nested classes cannot be accessed from outside the class. Simply try any method you wish of creating an instance of that class from the main function. You will find that no method you try is effective. The purpose of creating a class within another class is so that the container class can use the methods and properties of the nested class.

As was mentioned earlier in this chapter, it is also possible to have a normal class but to create an instance of it within another class. This is similar to nested classes, with one key difference: with a nested class, it can only be used within the class it is defined. A normal class that is simply instantiated inside another class can still be used in the normal way. The following example illustrates this.

Example 11.6

Step 1: Type the following code into your favorite text editor and save it as 11-06.h.

class classA { public:  void funca(); }; void classA::funca() {  cout << "This is a function in classA \n"; }

click to expand
Figure 11.5: Simple inheritance.

class classB { public:   void funcb(); }; void classB::funcb() {  classA myclass;  myclass.funca(); }

Step 2: Type this code into a text editor and save it as 11-06.cpp.

#include "11-06.h" #include <iostream> using namespace std;   int main() { classB myclass; classA anotherclass; myclass.funcb(); anotherclass.funca();   return 0; }

Step 3: Compile your program and execute it. You should see an image much like what is shown in Figure 11.6.

click to expand
Figure 11.6: Instantiating a class within another class.

You can see in this example that the same function was called from within the containing class, and then again from simply making a direct instance of the contained class. This is the essential difference between instantiating a normal class within another class, and a truly nested class.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net