Results 1 to 4 of 4

Thread: [RESOLVED] Nested Class Question

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Resolved [RESOLVED] Nested Class Question

    I'm playing around with nested classes and I'm getting some rather strange output from what appears to be some rather simple numbers. Take a look at my program below.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class A {
        protected:
            int d;
        public:
            class SubA {
                protected:
                    int a;
                public:
                    SubA() {};
                    ~SubA() {};
                    int getA() { return a; }
                    void setA(int _a) { _a = a; }
            };
            class SubB {
                protected:
                    int b;
                public:
                    SubB() {};
                    ~SubB() {};
                    int getB() { return b; }
                    void setB(int _b) { _b = b; }
            };
            class SubC {
                protected:
                    int c;
                public:
                    SubC() {};
                    ~SubC() {};
                    int getC() { return c; }
                    void setC(int _c) { _c = c; }
            };
            
            A() {};
            ~A() {};
            int getD() { return d; }
            void setD(int _d) { _d = d; }
    };
    
    int main(int argc, char *argv[])
    {
        //instantiate all the classes
        A dummy;
        A::SubA subADummy;
        A::SubB subBDummy;
        A::SubC subCDummy;
        
        //set up some values
        dummy.setD(5);
        subADummy.setA(10);
        subBDummy.setB(15);
        subCDummy.setC(20);
        
        //print them out to the screen
        cout << dummy.getD() << endl;
        cout << subADummy.getA() << endl;
        cout << subBDummy.getB() << endl;
        cout << subCDummy.getC() << endl;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Now I'm expecting that during the output I should see this:

    5
    10
    15
    20

    However there's an entirely different case. This is what the output looks like:

    2013315389
    2013327107
    2013454920
    7863840

    I'm having a slight problem understanding what in the world is going on here. I understand the principals of nested classes but from what I can remember when I first started with these a long time ago, I've never seen this kind of output before. Can someone give me a hand here? Thanks a lot.

    [EDIT] I wonder if those first three output number are phone numbers?
    Last edited by GamerMax5; Jan 10th, 2008 at 02:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width