Need Programming Assignment Help?

Get Help with Programming Assignment in Java, C/C++, Dot Net, PHP, Database and UML Diagrams

C++ Program for Encapsulation

C++ Program for Encapsulation

Encapsulation is the method of combining the data and functions inside a class. This hides the data from being accessed from outside a class directly.

#include <iostream>
using namespace std;

class Adder{
   public:
   
      Adder(int i = 0)
      {
        total = i;
      }
   
      void addNum(int number)
      {
          total += number;
      }
  
      int getTotal()
      {
          return total;
      };
   private:
     
      int total;
};
int main( )
{
   Adder a;
   
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);

   cout << "Total " << a.getTotal() <<endl;
   return 0;
}