Encapsulation in C++

Encapsulation:

  • Data encapsulation is used to hide the implementation details from users.
  • If a data member is private it means it can only be accessed within the same class. 
  • No outside class can access private data member (variable) of other class.

Syntax:

  1. class class_name

    {

    private data_members;

    public: methods()

    {

    }

    };

Example program:
  1. #include<iostream.h>

    #include<conio.h>

    class Account {

        private:

         int account_number;

         int account_balance;

        public:

         void show_Data() {

              account_number=13456;

              account_balance=5000;

              cout<<"\nBefore deposit of account XXXXX"<<account_number<<" number of balance :"<<account_balance;

        }

         void deposit(int a) {

              if (a < 0) {

                  cout<<"\nGiven amount is wrong...";

              }

              else{

                     account_balance = account_balance + a;

                     cout<<"\n\nAfter deposit of current balance :"<<account_balance;

              }

        }

    };

    void main()

        {

              clrscr();

              Account ac;

              ac.show_Data();

              ac.deposit(200);

              getch();

        }

Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me