Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; ok, if you were to put a function inside of a protected class, you can't access it outside of that ...

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    Classes

    ok, if you were to put a function inside of a protected class, you can't access it outside of that class, right?
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    err.. wrong forum by any chance?!

    [edit]thats better
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    If you mean when you put a function in the protected section of a class who can access it then the answer is that class and its derived classes. The function may not be accessed from outside the class because it is protected and not public.
    Code:
    class AClass
    {
       public: // Our classes interface. These can be called from anywhere.
       protected: // This is like private except is visible in derived classes.
       private: // This is our class internals. Accessible by the class only.
    };
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    ok, but in the tutorial for this site, they have as an example:

    Computer();

    ~Computer();

    void setspeed(int p);

    int readspeed();

    the class

    protected:

    int processorspeed;

    };

    Computer::Computer()

    {
    processorspeed = 0;

    }



    shouldn't the variable processorspeed not be able to be accessed in computer::computer()?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    of course it can as Computer::Computer is a member of the class. Its the constructor. Be a bit silly having a constructor that couldnt construct the class object wouldnt it?! so of course it has full access to all of the class.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    Originally posted by Stoned_Coder
    of course it can as Computer::Computer is a member of the class. Its the constructor. Be a bit silly having a constructor that couldnt construct the class object wouldnt it?! so of course it has full access to all of the class.

    Oh, because computer is a function in computer, I didn't realize that. Thanks! by the way, I am just learning classes as we speak.
    This has been a public service announcement from GOD.

    111 1111

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    if you understand private members, I can tell you that protected members are the same exept for derived classes, derived classes can access protected members, and cann't access private ones.
    none...

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by sentienttoaster
    Oh, because computer is a function in computer, I didn't realize that. Thanks! by the way, I am just learning classes as we speak.
    I don't know if I would really call a constructor a function. it may be, correct me if I'm wrong please. It kindof seems like a function, but to be technical, a constructor and a member function of the class are two different things. it doesn't seem to be a function because constructors don't return anything.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Constructors do return values....

    Think about one of the scenaries in which a copy constructor is automatically invoked: When you are returning a class type object from a function.

    Code:
    myClass  myClass::myFunction()
    {
    
            // blahblahblah
    
    
            return (myClass(/* What goes here */));   //  the constructor returns a value of type myClass
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21