Protected...?

This is a discussion on Protected...? within the C++ Programming forums, part of the General Programming Boards category; Okay...I that it is possible to do the following... Code: class Person { public: int var1; int var1; }; but ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up Protected...?

    Okay...I that it is possible to do the following...
    Code:
    class Person
    { 
       public:
       int var1;
       int var1;
    };
    but what is the point of doing this altered version...
    Code:
    class Person
    {
      protected:
      int var1;
      int var2;
    };
    I know what it does (well, I think that it makes the class members after "protected:" unable to be accesed...correct me if I am wrong), but what is the point of doing it?

    Thanks
    -Chris

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    protected is the same as private (no direct access from outside the class) but protected class members can be accessed by derived classes eg. -

    Code:
    #include <iostream>
    
    using namespace std;
     
    class Person
    {
    protected:
    	int var1;
    	int var2;
    };
    
    class Adult : public Person
    {
    protected:
    	int var3;
    public:
    	void SetVar1(int i){var1=i;} //access Person member variable
    	int GetVar1(){return var1;}
    };
    
    int main()
    {
    	Adult me;
    	me.SetVar1(27);
    	cout << me.GetVar1();
    	return 0;
    }
    zen

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Originally posted by zen
    protected is the same as private (no direct access from outside the class) but protected class members can be accessed by derived classes
    Protected members can also be accessed by friend functions. I don't know if friend functions are used much in the real world, our instructor doesn't like them, but the Deitel text uses them for overloaded << operators on classes.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Protected members can also be accessed by friend functions. I don't know if friend functions are used much in the real world, our instructor doesn't like them, but the Deitel text uses them for overloaded << operators on classes.
    As can private members. Friend functions are necessary when you want to use the member function of an object on the right of a binary expression.

    If you overloaded the << operator for ouput inside the class (rather than as a friend function), you'd have to do something like -

    Code:
    class Adult 
    {
    protected:
    	int var1;
    public:
    	void SetVar1(int i){var1=i;} 
    		ostream& operator<<(ostream& os){return os << var1;} 
    };
    
    int main()
    {
    	Adult me;
    	me.SetVar1(27);
    	me << cout;
    	return 0;
    }
    as the Adult object has to be on the left. If you use a friend function you can put the ostream of the left -

    Code:
    class Adult 
    {
    private:
    	int var1;
    public:
    	void SetVar1(int i){var1=i;} 
    	friend ostream& operator<<(ostream& os,Adult& a); 
    };
    
    ostream& operator <<(ostream& os,Adult& a)
    {
    	return os << a.var1;
    }
    
    int main()
    {
    	Adult me;
    	me.SetVar1(27);
    	cout << me;
    	return 0;
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing protected stuff
    By lord in forum C++ Programming
    Replies: 15
    Last Post: 10-19-2008, 10:43 PM
  2. problem with protected members
    By edwrodrig in forum C++ Programming
    Replies: 9
    Last Post: 01-16-2008, 11:32 AM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  4. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  5. How can I reach Protected Variables?
    By zayzay in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 06:32 AM

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