Thread: Purpose of Private and Public Access Specifiers, OOP

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    Purpose of Private and Public Access Specifiers, OOP

    I was just wondering in my program, what exactly is the purpose of declaring private data members? What exactly is the concept behind, private and public?

    Code:
    #include<iostream> 
    
    using namespace std; 
      
    class Encapsulation   /*class name is Encapsulation */
    { 
        private:         /*access specifer */
    
    
            int x;      /* x is integer variable that store integer value */
              
        public:          /*access specifer */
           
            void set(int a)     /*function that pass integer value a */
            { 
                x =a;          /* we are assigning content of a to x */
            } 
              
            int get()         /* function that resturn value of x */
            { 
                return x;    /*return result of x */
            } 
    }; 
      
    int main()     /* start program */
    { 
        Encapsulation obj;   //declare object of job */
          
        obj.set(5);          // call function to pass 5
          
        cout<<obj.get();     /* get output of function 
        return 0; 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you have any books on learning C++?

    Data Encapsulation in C++ - Tutorialspoint
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private or Public?
    By Tamim Ad Dari in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2013, 05:09 AM
  2. private/public get/set
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:49 AM
  3. Private or Public
    By gtriarhos in forum C# Programming
    Replies: 3
    Last Post: 10-10-2005, 07:14 PM
  4. Purpose of : public <class name>?
    By jverkoey in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2005, 10:00 PM
  5. public and private
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2002, 11:02 AM

Tags for this Thread