Class Scope

This is a discussion on Class Scope within the C++ Programming forums, part of the General Programming Boards category; Hi, Can an instantiation of a sub-class call a function member of the class from which it is derived? (that ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Lightbulb Class Scope

    Hi,

    Can an instantiation of a sub-class call a function member of the class from which it is derived? (that sounds weird)

    Basically...
    eg)
    Code:
    class example { // Abstract Class
    
    	protected:
    		int x;
    		
    	public:
    		void setX (int in_x) {x = in_x;}
    		class chair {
    			protected:
    				float y;
    				
    			public:
    				void setY (float in_y) {y = in_y;}
                                                    };
    };
    If I have an object:
    example::chair abc;

    Can I access setX with abc? Perhaps not with this code example but how could this be achieved?

    Thanks
    Last edited by strokebow; 12-17-2008 at 04:44 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    setX has no meaning with abc - because abc is not part of the example class itself - it is just inside the class from namespace perspective, not as a part of example. It just prevents "chair" from being confused with "foo::chair" or "bar::chair" which are completely unrelated [or perhaps related/similar, but yet different] classes.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Smile

    Quote Originally Posted by matsp View Post
    setX has no meaning with abc - because abc is not part of the example class itself - it is just inside the class from namespace perspective, not as a part of example. It just prevents "chair" from being confused with "foo::chair" or "bar::chair" which are completely unrelated [or perhaps related/similar, but yet different] classes.


    --
    Mats
    Thanks for your reply Mats.

    How then can I achieve my aim. I want the chair class to inherit the data members of its parent class.
    How can this be done?

    Thanks

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by strokebow View Post
    Thanks for your reply Mats.

    How then can I achieve my aim. I want the chair class to inherit the data members of its parent class.
    How can this be done?

    Thanks
    Do you actually want to inherit? If so, just inherit from the enclosing class, there's nothign wrong with that. If you just want to ACCESS the members of the enclosing class, just do this normally, provided the access level is public. Or, make the inner class a friend of the outer one.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use inheritance or composition:

    Inheritance:
    Code:
    class example
    {
        ...
    };
    
    class chair : public example
    {
        ... // stuff added to example content
    };
    Composition:
    Code:
    class example
    {
       ...
    };
    
    class chair
    {
        example x;
        ... // other stuff. 
    };
    both of these will allow chair to use setX(), since setX is a public function, and thus allows anything that knows of an example instance to use this function.

    If you make the function private then only the class itself can use it - no manner of inheritance or conposition will change that. If you make it protected, only classes that inherit from the base-class can use the method. Protected is, in my opinion, quite useless - it is just like putting a sign saying "don't walk on the grass" - it will only prevent people who follow rules from doing so - since any other class can just inherit from the class and then use your "protected" content with no repercussion. Private is like putting a fence around the grass - it is a physical barrier for other users to prevent it from being abused [there are ways around fences too - you can climb over or use power-tools to cut through them, and in C++ those tools are pointers and casts - but then we're probably going to get noticed!]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by matsp View Post
    Use inheritance or composition:

    Inheritance:
    Code:
    class example
    {
        ...
    };
    
    class chair : public example
    {
        ... // stuff added to example content
    };
    Composition:
    Code:
    class example
    {
       ...
    };
    
    class chair
    {
        example x;
        ... // other stuff. 
    };
    both of these will allow chair to use setX(), since setX is a public function, and thus allows anything that knows of an example instance to use this function.

    If you make the function private then only the class itself can use it - no manner of inheritance or conposition will change that. If you make it protected, only classes that inherit from the base-class can use the method. Protected is, in my opinion, quite useless - it is just like putting a sign saying "don't walk on the grass" - it will only prevent people who follow rules from doing so - since any other class can just inherit from the class and then use your "protected" content with no repercussion. Private is like putting a fence around the grass - it is a physical barrier for other users to prevent it from being abused [there are ways around fences too - you can climb over or use power-tools to cut through them, and in C++ those tools are pointers and casts - but then we're probably going to get noticed!]

    --
    Mats
    brilliant - thanks! Data member example is really good.

    However, I dont see how the classes for your composition code are linked?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In compositon, example is a data member of chair - just like y is a member of chair in your post, it just happens to be a more complicated member variable than an int or float.

    In the inheritance example, the basic structure of example is extended by the additional members added in chair.

    Which you use depends on whether it is a "IsA" or "HasA" relationship between the two classes.

    For example, if we have a "vehicle" base-class, "car" would be a possible derived class that inherits from "vehicle". "motorcycle" may also inherit from "vehicle". Both of these classes may have an array of "wheel" as a member - but of course they do not have the same type/number of wheels, so we can't put the wheels in the vehicle base-class (and a boat is likely to have "propeller" or "sail", instead of wheel).

    So, car IsA relates to vehicle, car HasA relates to wheel.

    I hopethis makes sense - it's getting late, and coming up with good examples is hard enough without having a tired head and I've got a horrible cold which doesn't help either.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    Thats brilliant mate. Crystal clear.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 02:52 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  3. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 06:11 PM
  4. Class scope variables
    By filler_bunny in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2003, 03:14 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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