Calling an iterative class function or variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angelajean
    New Member
    • May 2007
    • 4

    Calling an iterative class function or variable

    Hi all,

    I have a problem here that I am not sure how I should go about solving. I am thinking of creating classes that iteratively calls on other classes such that at the end of the day, I can get something like:

    Criteria[0].Subcriteria[0].Subcriteria[1].Subcriteria[0].value = a;

    So it seems like at first instance, I have a Subcriteria class:

    Code:
    class Subcriteria {
    
    public:
    
      Subcriteria () {
      };
    
      unsigned int value;
    
    };
    And a Criteria class:

    Code:
    class Criteria {
    
    public:
    
      Criteria () {
      };
    
      int value = -1;
    
    };
    I then declare an array of the Criteria class:

    Code:
     Criteria *c;
    And I supposedly have a list of x subcriterias under this criteria (assuming 1 first), and another set of y subcriterias under each of the x subcriteria and finally z subcriteria under each of the y subcriteria. So theoretically I should have something like the following to specify the upperbound of the subcriteria:

    Code:
    Criteria[0].Subcriteria[x-1].Subcriteria[y-1].Subcriteria[z-1].value = a;
    But now it's starting to look cryptic to me and I realise that I am totally lost. So I was wondering if some kind souls can help provide some directions?

    Subsequently, I would need to "move up" from the child to the parent class because the value obtained at the lowest child (i.e. Subcriteria[z]) will be aggregated and stored as the value of the parent (i.e. Subcriteria[y]) and this is repeated until Criteria[0] gets a value from all the children.

    So, is there a way in which Subcriteria can also check if it's parent is a Subcriteria or a Criteria class? Tentatively, I stored a value of -1 in the Criteria.value variable because I was thinking along the lines of

    Code:
    if (parent.value = -1) {
      do some parent-related functions
    else {
      move up to parent subcriteria and aggregate values
    };
    Head's spinning already.... :(

    Cheers,
    Angela
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    I think u are trying to achieve a Binary Tree or a List data structure.
    If u want to achieve the samethen your implementation need some change.
    The SubCriteria class have to have a self referential pointer to itself like
    Code:
    class Subcriteria {
     
    public:
     
      Subcriteria () {
      };
      Subcriteria *Next;
     
      unsigned int value;
     
    };
    Is this what u need ?

    Thanks
    Raghuram

    Comment

    • svlsr2000
      Recognized Expert New Member
      • Feb 2007
      • 181

      #3
      Originally posted by angelajean
      Hi all,

      I have a problem here that I am not sure how I should go about solving. I am thinking of creating classes that iteratively calls on other classes such that at the end of the day, I can get something like:

      Criteria[0].Subcriteria[0].Subcriteria[1].Subcriteria[0].value = a;

      So it seems like at first instance, I have a Subcriteria class:

      Code:
      class Subcriteria {
       
      public:
       
      Subcriteria () {
      };
       
      unsigned int value;
       
      };
      And a Criteria class:

      Code:
      class Criteria {
       
      public:
       
      Criteria () {
      };
       
      int value = -1;
       
      };
      I then declare an array of the Criteria class:

      Code:
       Criteria *c;
      And I supposedly have a list of x subcriterias under this criteria (assuming 1 first), and another set of y subcriterias under each of the x subcriteria and finally z subcriteria under each of the y subcriteria. So theoretically I should have something like the following to specify the upperbound of the subcriteria:

      Code:
      Criteria[0].Subcriteria[x-1].Subcriteria[y-1].Subcriteria[z-1].value = a;
      But now it's starting to look cryptic to me and I realise that I am totally lost. So I was wondering if some kind souls can help provide some directions?

      Subsequently, I would need to "move up" from the child to the parent class because the value obtained at the lowest child (i.e. Subcriteria[z]) will be aggregated and stored as the value of the parent (i.e. Subcriteria[y]) and this is repeated until Criteria[0] gets a value from all the children.

      So, is there a way in which Subcriteria can also check if it's parent is a Subcriteria or a Criteria class? Tentatively, I stored a value of -1 in the Criteria.value variable because I was thinking along the lines of

      Code:
      if (parent.value = -1) {
      do some parent-related functions
      else {
      move up to parent subcriteria and aggregate values
      };
      Head's spinning already.... :(

      Cheers,
      Angela
      Are you looking for something like tree.
      I found one more thing
      Criteria[0].Subcriteria[0].Subcriteria[1].Subcriteria[0].value = a; if i am right you are trying to have object of Subcriteria inside another object of Subriteria this is not allowed in c++

      Comment

      • angelajean
        New Member
        • May 2007
        • 4

        #4
        Originally posted by gpraghuram
        Hi,
        I think u are trying to achieve a Binary Tree or a List data structure.
        If u want to achieve the samethen your implementation need some change.
        The SubCriteria class have to have a self referential pointer to itself like
        Code:
        class Subcriteria {
         
        public:
         
          Subcriteria () {
          };
          Subcriteria *Next;
         
          unsigned int value;
         
        };
        Is this what u need ?

        Thanks
        Raghuram
        Hi,

        Yes. I think I need a tree, not a binary tree though... but I am not familiar with the implementation of a tree. I know that in a binary tree, there will be *Left and *Right.... but I am not sure about a non-binary tree...

        Cheers,
        Angela

        Comment

        • angelajean
          New Member
          • May 2007
          • 4

          #5
          Originally posted by svlsr2000
          Are you looking for something like tree.
          I found one more thing
          Criteria[0].Subcriteria[0].Subcriteria[1].Subcriteria[0].value = a; if i am right you are trying to have object of Subcriteria inside another object of Subriteria this is not allowed in c++
          Oh, actually I just tried something like the following and it seemed to work, although I am not sure if the syntax and method are totally correct:

          Code:
          class Subcriteria {
          
          public:
          
          	Subcriteria () {
          	};
          
          	Subcriteria *SubCriteria;
          
          	int value;
          
          };
          
          
          class Criteria {
          
          public:
          
          	Criteria () {
          		value = 1;
          	};
          
          public:
          
          	int value;
          	Subcriteria *SubCriteria;
          
          };
          
          int main() {
          
          	Criteria *C;
          	C = new Criteria[4];
          	C[0].SubCriteria = new Subcriteria[2];
          	C[1].SubCriteria = new Subcriteria[2];
          	C[2].SubCriteria = new Subcriteria[3];
          	C[3].SubCriteria = new Subcriteria[3];
          
          	C[0].SubCriteria[0].SubCriteria = new Subcriteria[2];
          
          	C[0].SubCriteria[0].value = 3;
          	C[0].SubCriteria[1].value = 6;
          	
          	C[0].SubCriteria[0].SubCriteria[0].value = 10;
          	C[0].SubCriteria[0].SubCriteria[1].value = 20;

          This seemed to compile and run without problems. But *assuming* that this is correct, I have another problem with traversal.

          If for example, I have values at:

          Code:
          	C[0].SubCriteria[0].SubCriteria[0].value = 10;
          	C[0].SubCriteria[0].SubCriteria[1].value = 20;
          And I want to store it at:

          Code:
          	C[0].SubCriteria[0].value = [I]sum of the children nodes, i.e. 10 + 20[/I]
          Is there an iterative way that I could point back to "parent" of children nodes" such that I can do something like (in pseudocode):

          Code:
          for each node starting from the leaves do {
            calculate sum of all children of parent node;
            assign sum to parent.value;
            repeat for all children nodes until root node;
          };
          It's now the "calling the parent" part that I am having problems now. And oh, thanks guys for mentioning the "tree" twice. Sometimes I can get so blinded by the problem that I am not able to see the bigger picture of the abstract data type.

          Cheers,
          Angela

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I think you can avoid this type of coding by using an Observer design pattern.

            Comment

            • angelajean
              New Member
              • May 2007
              • 4

              #7
              Originally posted by weaknessforcats
              I think you can avoid this type of coding by using an Observer design pattern.
              Oh, sorry, but what is an observer design pattern? I'm not too familiar with design paradigms... :(

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Check out Design Patterns ISBN 0-201-63361-2 page 293.

                You use Observer when an action requires one or more other objects to be notified of the change. For example, a change in the address of a Person object may require that the MailingList and VehcileRegistra tion objects be advised of the change so they can report to their users that you have moved.

                Comment

                Working...