Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. no private no protected oops only public

no private no protected oops only public

Scheduled Pinned Locked Moved C / C++ / MFC
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pathakr
    wrote on last edited by
    #1

    lately i am finding that I am adding functions in the public space only.. no protected no private.. may be just to access the functions easily.. how may i remove this habbit. tell me true practical explaination that private or protected are must pathak

    _ S L T S 5 Replies Last reply
    0
    • P pathakr

      lately i am finding that I am adding functions in the public space only.. no protected no private.. may be just to access the functions easily.. how may i remove this habbit. tell me true practical explaination that private or protected are must pathak

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      You can do one thing. Create a variable in the public scope that is critical to your application and you don't want the user to modify value of the same. If this value gets modified you are stuck. Don't allow to do this. You will have to think the criticallity of the variable each time you are going to declare them and at the same time you need to determine their scope Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

      1 Reply Last reply
      0
      • P pathakr

        lately i am finding that I am adding functions in the public space only.. no protected no private.. may be just to access the functions easily.. how may i remove this habbit. tell me true practical explaination that private or protected are must pathak

        S Offline
        S Offline
        Sarath C
        wrote on last edited by
        #3

        Practices are making man perfect. so through practice, u will be perfect. Reading is a good option to know more and correct our assumptions and mistake and also it will fill the gaps in our knowledge. If you read Code Complete 2, Effective C++ (or similar books), u will find that what u r doing is absolutly wrong. Define scope for all classes u r creating. Fix the interfaces, limit it to a minimum. The public functions are used to communicate with other modules or object. where are as we are defining private functions for some internal calculation required by other functions in the class. Suppose there is a function in your class which draws an object in your window. you don't need to share this function to others. because it is only required when a paint message comes in your object and draws the required object. it is limited to your class. so scope ur object with minimum interfaces. never put internal functions to public, that is nto a good way of pragramming. if it is generic and can be used for others make it public static and meaningful. Hope u clear. -Sarath

        1 Reply Last reply
        0
        • P pathakr

          lately i am finding that I am adding functions in the public space only.. no protected no private.. may be just to access the functions easily.. how may i remove this habbit. tell me true practical explaination that private or protected are must pathak

          L Offline
          L Offline
          Laxman Auti
          wrote on last edited by
          #4

          If you want that no one can access your method simply make it Private If you want that only immediate derived class can access the methods make it protected and for everyone make it public for e.g if you want that the user must pass the parameter for creation of the instance of the class then make the empty constructer as private

          class test
          {
          public:
          test(int index){index=0};
          private:
          test(){};
          };

          Knock out 'T' from CAN'T , You 'CAN' if you think you 'CAN' :cool:

          P 1 Reply Last reply
          0
          • P pathakr

            lately i am finding that I am adding functions in the public space only.. no protected no private.. may be just to access the functions easily.. how may i remove this habbit. tell me true practical explaination that private or protected are must pathak

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            pathakr wrote:

            tell me true practical explaination that private or protected are must

            when you don't any functionality to leak to outer class, which will corrupt the data if not properly used!

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV

            1 Reply Last reply
            0
            • L Laxman Auti

              If you want that no one can access your method simply make it Private If you want that only immediate derived class can access the methods make it protected and for everyone make it public for e.g if you want that the user must pass the parameter for creation of the instance of the class then make the empty constructer as private

              class test
              {
              public:
              test(int index){index=0};
              private:
              test(){};
              };

              Knock out 'T' from CAN'T , You 'CAN' if you think you 'CAN' :cool:

              P Offline
              P Offline
              ppatel567
              wrote on last edited by
              #6

              A_LaXmanKnock out 'T' from CAN'T ,

              also " ' " then it becomes CAN

              L 1 Reply Last reply
              0
              • P ppatel567

                A_LaXmanKnock out 'T' from CAN'T ,

                also " ' " then it becomes CAN

                L Offline
                L Offline
                Laxman Auti
                wrote on last edited by
                #7

                Can you start thinking from now?;P Knock out 'T' from CAN'T , You 'CAN' if you think you 'CAN' :cool:

                1 Reply Last reply
                0
                • P pathakr

                  lately i am finding that I am adding functions in the public space only.. no protected no private.. may be just to access the functions easily.. how may i remove this habbit. tell me true practical explaination that private or protected are must pathak

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  Just get in the habbit of always adding new functions in the private section first and only "promote" them to protected or lastly to public when required. Steve

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups