Classes' Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newguy194
    New Member
    • May 2007
    • 25

    Classes' Classes

    I am writing a program to familiarize myself better with certain aspects of classes, and was wondering how to properly implement a class inside a class, and make it so only a certain instance of the original class can call a certain instance of the nested class. Basically I'm trying to get an object to have an object on it, and to make the second object exclusive to the first object. I'm confused about how to go about this.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Something like this

    [code=cpp]class Outer
    {
    private:
    class Inner
    {
    private:
    int data;

    public:
    int GetValue() const { return data; }
    void SetValue(int newValue){ data = newValue; }
    Inner(int init = 0) : data(init) {}
    };

    Inner *pInner;

    public:
    Outer(int init = 0) : pInner(new Inner(init)) {}
    ~Outer(){ delete pInner; }

    int GetValue() const { return pInner->GetValue(); }
    void SetValue(int newValue){ pInner->SetValue(newVa lue); }
    };[/code]Although this is a slightly converluted way to store an int :D

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      [CODE=cpp]class yourFirstClass {
      private:
      class yourSecondClass {
      // yourSecondClass definition here.
      }

      // yourFirstClass definition here
      }[/CODE]

      Follow this basic pattern, and you should be OK.

      Comment

      • newguy194
        New Member
        • May 2007
        • 25

        #4
        Originally posted by Ganon11
        [CODE=cpp]class yourFirstClass {
        private:
        class yourSecondClass {
        // yourSecondClass definition here.
        }

        // yourFirstClass definition here
        }[/CODE]

        Follow this basic pattern, and you should be OK.
        I understand the declarations, but I'm confused as to how to implement it in my code, say I want to reference a member of mysecondClass how would I do that?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by newguy194
          I understand the declarations, but I'm confused as to how to implement it in my code, say I want to reference a member of mysecondClass how would I do that?
          Have you read my post?

          Comment

          • newguy194
            New Member
            • May 2007
            • 25

            #6
            Multiple inners

            I have one more problem, how can I have multiple private classes inside my outer class? like Inner1 and Inner2, I do not fully understand how this works.

            Comment

            • newguy194
              New Member
              • May 2007
              • 25

              #7
              Originally posted by newguy194
              I have one more problem, how can I have multiple private classes inside my outer class? like Inner1 and Inner2, I do not fully understand how this works.
              I'm not sure if I was really clear in my above question, so I wrote some code that maybe illustrates better what I am asking

              Code:
              #include <cstdlib>
              #include <iostream>
              
              using namespace std;
              class Food
              {
                    private:
                    class vegetable
                    {
                          private:
                          string vegname;
                          int calories;
                          public:
                          int GetCalories() const { return calories; }
                          void SetCalories(int newValue){ calories = newValue; }
                          vegetable(int init = 0) : calories(init) {}
                                
                    };
                    class fruit
                    {
                          private:
                          string fruitname;
                          int calories;
                          public:
                          int GetCalories() const { return calories; }
                          void SetCalories(int newValue){ calories = newValue; }
                          fruit(int inita = 1) : calories(inita) {}
                    };
                    vegetable *newveg;
                    fruit *newfruit;
                    public:
                    Food(int init = 0) : newveg(new vegetable(init)) {}
                    ~Food(){ delete newveg; }
                    int GetCaloriesveg() const { return newveg->GetCalories(); }
                    void SetCaloriesveg(int newValue){ newveg->SetCalories(newValue); }
              };
              
              
              int main(int argc, char *argv[])
              {
                  Food(carrot);
                  carrot.SetCaloriesveg(50);
                  cout<<carrot.GetCaloriesveg()<<"\n";
                  system("PAUSE");
                  return EXIT_SUCCESS;
              }
              I was wondering how I initialize the second object in this fashion, and similarily create a destructor for it. I'm confused because this is totally different than the way I was attempting to nest classes before.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                [code=cpp]
                Food(int veginit = 0, int fruitinit = 0)
                : newveg(new vegetable(vegin it)),
                newfruit(new fruit(fruitinit ))
                {
                }

                ~Food()
                {
                delete newveg;
                delete newfruit;
                }
                [/code]

                Comment

                • newguy194
                  New Member
                  • May 2007
                  • 25

                  #9
                  Originally posted by Banfa
                  [code=cpp]
                  Food(int veginit = 0, int fruitinit = 0)
                  : newveg(new vegetable(vegin it)),
                  newfruit(new fruit(fruitinit ))
                  {
                  }

                  ~Food()
                  {
                  delete newveg;
                  delete newfruit;
                  }
                  [/code]
                  How would I declare that something should be a new vegetable and not a new fruit, and vice versa.

                  Comment

                  Working...