Home | | Computer Science 11th std | C++: Nested class

Example Programs in C++ - C++: Nested class | 11th Computer Science : Chapter 14 : Classes and objects

Chapter: 11th Computer Science : Chapter 14 : Classes and objects

C++: Nested class

When one class become the member of another class then it is called Nested class and the relationship is called containership.

Nested class

 

When one class become the member of another class then it is called Nested class and the relationship is called containership.

 

Classes can be nested in two ways.

(1) By defining a class within another class

(2) By declaring an object of a class as a member to another class


Defining a class with in another

When a class is declared with in another class, the inner class is called as Nested class (ie the inner class) and the outer class is known as Enclosing class. Nested class can be defined in private as well as in the public section of the Enclosing class.

 

Illustration 14.12 C++ program to illustrate the nested class

#include<iostream>

using namespace std;

class enclose

{

private:

      int x;

      class nest

      {

private :

      int y;

public:

      int z;

void prn()

{

      y=3;z=2;

      cout<<"\n The product of"<<y<<'*'<<z<<"= "<<y*z<<"\n";

}

}; //inner class definition over

nest n1;

public:

nest n2;

void square()

{

n2.prn(); //inner class member function is called by its object x=2;

      n2.z=4;

      cout<<"\n The product of " <<n2.z<<'*'<<n2.z<<"= "<<n2.z*n2.z<<"\n";

      cout<<"\n The product of " <<x<<'*'<<x<<"= "<<x*x; }

};   //outer class definition over

int main()

{

enclose e;

      e.square();  //outer class member function is called

}

Output:

The product of 3*2=6

The product of 4*4=16

The product of 2*2=4

 

In the above program the inner class nest is defined inside the outer class enclose. nest is accessed by enclose by creating an object of nest

 

Containership in C++

 

Whenever an object of a class is declared as a member of another class it is known as a container class. In the container-ship the object of one class is declared in another class.

 

Illustration 14.13 C++ program to illustrate the containership

#include<iostream>

using namespace std;

class outer

{

      int data;

      public:

      void get();

};

class inner

{

      int value;

      outer ot; // object ot of class outer is declared in class inner

      public:

      void getdata();

};

void outer :: get()

{

      cout<<"\nEnter a value";

      cin>>data;

      cout<<"\nThe given value is "<<data;

}

void inner :: getdata()

{

      cout<<"\nEnter a value";

      cin>>value;

      cout<<"\nThe given value is "<<value;

      ot.get();  //calling of get() of class outer in getdata() of class inner

}

int main()

{

      inner in;

      in.getdata();

      return 0;

}

Output:

Enter a value10

The given value is 10

Enter a value 20

The given value is 20

 

In the above program class outer and inner are defined separately. But both the classes are connected by the object 'ot' which is a member of class inner

 

Tags : Example Programs in C++ , 11th Computer Science : Chapter 14 : Classes and objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Nested class | Example Programs in C++

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.