Nested Class Program in C++

This C++ program illustrates the use of nested classes. The program uses Node class as a member for Stack class where Node class is nested inside Stack class. The Node class is local to Stack class which can be used outside Stack’s context by using scope operator ‘::’.

Here is the source code of the C++ program illustrates the use of nested classes. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Illustrate Nested Classes
  3.  */
  4. #include <iostream>
  5.  
  6. class Stack {
  7.     class Node {
  8.         public:
  9.             int data;
  10.             Node* next;
  11.             Node(int data, Node* next);
  12.             ~Node();
  13.     }* head;
  14.     public:
  15.         Stack();
  16.         Stack(const Stack& s);
  17.         void operator=(const Stack& s);
  18.         ~Stack();
  19.         void push(int data);
  20.         int peek() const;
  21.         int pop();
  22. };
  23.  
  24. Stack::Node::Node(int data, Node* next)
  25. {
  26.     this->data = data;
  27.     this->next = next;
  28. }
  29.  
  30. Stack::Node::~Node() { }
  31.  
  32. Stack::Stack() { head = NULL; }
  33.  
  34. Stack::Stack(const Stack& s)
  35. {
  36.     head = s.head;
  37. }
  38.  
  39. void Stack::operator=(const Stack& s)
  40. {
  41.     head = s.head;
  42. }
  43.  
  44. void Stack::push(int data)
  45. {
  46.     head = new Node(data, head);
  47. }
  48.  
  49. int Stack::peek() const {
  50.     if(head == 0)
  51.     {
  52.          std::cerr << "Stack empty!" << std::endl;
  53.          return -1;
  54.     }
  55.     else
  56.          return head->data;
  57. }
  58.  
  59. int Stack::pop()
  60. {
  61.     if(head == NULL) return -1;
  62.     int result = head->data;
  63.     Node* oldNode = head;
  64.     head = head->next;
  65.     delete oldNode;
  66.     return result;
  67. }
  68.  
  69. Stack::~Stack()
  70. {
  71.     if(head != NULL)
  72.     {
  73.         while(head->next != NULL)
  74.         {
  75.             Node* temp = head;
  76.             head = head->next;
  77.             delete temp;
  78.         }
  79.     }
  80. }
  81.  
  82. int main()
  83. {
  84.     Stack Integers;
  85.     int value, num;
  86.  
  87.     std::cout << "Enter the number of elements ";
  88.     std::cin >> num;
  89.     while(num > 0)
  90.     {
  91.         std::cin >> value;
  92.         Integers.push(value);
  93.         num--;
  94.     }
  95.     while (( value = Integers.pop() ) != -1)
  96.         std::cout << "Top element of stack  " << value << std::endl; 
  97. }

$ a.out
Enter the number of elements 5
1  2  3  4  5
Top element of stack  5
Top element of stack  4
Top element of stack  3
Top element of stack  2
Top element of stack  1

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.