Quantcast
Channel: Encapsulation – Projects India
Viewing all articles
Browse latest Browse all 2

C++ Program for Encapsulation

$
0
0

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;
}

 

The post C++ Program for Encapsulation appeared first on Projects India.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images