Thread: Inner classes

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    India
    Posts
    14

    Inner classes

    Hi All.
    I just wanted to know that is it possible to have class within a class. If possible....which I think is true...how can we access members of inner class.
    I tried to write this code and tried to access member within inner class. But it gave error in statement f.second.b in code below. Can any one help me ...what is correct way to do it.
    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<iostream.h>
    
    class first{
    	public:
    	int a;
    	first(){a=0;}
    	class second{
    			public:
    			second(){b=0;}
    			~second(){}
    			int b;
    
    		      };
    		};
    
    void main(){
    	first f;
    	clrscr();
    	f.a=5;
    	f.second.b=9;
    	cout<<f.a;
    	cout<<"\n"<<f.second.b;
    	getch();
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    class outtie
    {
    public:
        class innie
        {
        public:
            int nummie;
        };
    
        // Make an instance of innie 
        innie d;
    };
    
    int main( void )
    {
        outtie o;
    
        o.d.nummie = 42;
    
        return 0;
    }

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes this is possible, but you still need an instance of that inner class to work with. In your case, you could do:
    Code:
    first::second mySecond;
    mySecond.b = 5;

    Another common nested class is an iterator. Here's a short example of something you might see:
    Code:
    class List
    {
    public:
      class iterator
      {
      //...
      };
    
      //common member functions  
      iterator begin();
      iterator end();
    
    };
    
    int main()
    {
       List myList;
       //do some insertions into the list
    
      //declare an iterator and initialize it to the beginnning of the list
       List::iterator it = myList.begin();
    }
    A few more things I should point out (and you'll hear these a lot if you keep reading the forums):

    1. You're using non-standard headers. The appropriate headers are:
    Code:
    #include <iostream>
    #include <cstdio>
    There is no standard equivalent of conio.h, so you might want to consider using another method of pausing the program at the end.

    2. void main is not standard. Use int main. Note that you do not have to explicitly return a value from main (it will implicitly return 0), though it might be considered good practice.

    3. Member data of classes should always be private. You might've just made your variables public for simplicity, but I thought I should note it anyway
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by JaWiB View Post
    3. Member data of classes should always be private.
    Maybe you should tell that to the designers of STL? For instance, std:: pair

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Maybe you should tell that to the designers of STL? For instance, std:: pair
    pair is a struct, not a class. Although I agree that always is probably the wrong word there.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > Although I agree that always is probably the wrong word there.

    I probably should've said class data should almost never be public. I'm sure there are exceptions to the rule, and it's definitely not straightforward to say that data members should never even be protected. I apologize for the poor choice of words on my part, especially since I go by the rule of thumb that whenever someone says always or never, there are usually exceptions.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    >> Maybe you should tell that to the designers of STL? For instance, std:: pair
    pair is a struct, not a class. Although I agree that always is probably the wrong word there.
    As far as I know, a struct is just a class with a default access level of public instead of private. So saying "never use public data members" is the same thing as saying "never use structs." Which I think is an odd thing to say...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As far as I know, a struct is just a class with a default access level of public instead of private. So saying "never use public data members" is the same thing as saying "never use structs." Which I think is an odd thing to say...
    Yes, a struct is indeed a class with default public access. On the other hand, the struct keyword has a C heritage, and in this heritage there is no private access, or member functions for that matter. Conceptually, I think JaWiB was just thinking of the encapsulation that can be enforced by private access, and in that sense it is moving a little further away from C++'s C legacy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Common usage of structs and classes in C++ is to use structs when you provide public access to the member data, and when using classes encapsulate that data in the private access space. This is not enforced by the language but is still common practice.

    So, with that in mind, "Member data of classes should always be private," is much different than, "Member data of classes or structs should always be private." I was just pointing out that pair is a bad example because it doesn't apply to the original statement.

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. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM