469,347 Members | 19,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. class Subcriteria {
  2.  
  3. public:
  4.  
  5.   Subcriteria () {
  6.   };
  7.  
  8.   unsigned int value;
  9.  
  10. };
And a Criteria class:

Expand|Select|Wrap|Line Numbers
  1. class Criteria {
  2.  
  3. public:
  4.  
  5.   Criteria () {
  6.   };
  7.  
  8.   int value = -1;
  9.  
  10. };
I then declare an array of the Criteria class:

Expand|Select|Wrap|Line Numbers
  1.  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:

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. if (parent.value = -1) {
  2.   do some parent-related functions
  3. else {
  4.   move up to parent subcriteria and aggregate values
  5. };
Head's spinning already.... :(

Cheers,
Angela
May 22 '07 #1
7 1311
gpraghuram
1,275 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. class Subcriteria {
  2.  
  3. public:
  4.  
  5.   Subcriteria () {
  6.   };
  7.   Subcriteria *Next;
  8.  
  9.   unsigned int value;
  10.  
  11. };
  12.  
Is this what u need ?

Thanks
Raghuram
May 22 '07 #2
svlsr2000
181 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. class Subcriteria {
  2.  
  3. public:
  4.  
  5. Subcriteria () {
  6. };
  7.  
  8. unsigned int value;
  9.  
  10. };
And a Criteria class:

Expand|Select|Wrap|Line Numbers
  1. class Criteria {
  2.  
  3. public:
  4.  
  5. Criteria () {
  6. };
  7.  
  8. int value = -1;
  9.  
  10. };
I then declare an array of the Criteria class:

Expand|Select|Wrap|Line Numbers
  1.  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:

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. if (parent.value = -1) {
  2. do some parent-related functions
  3. else {
  4. move up to parent subcriteria and aggregate values
  5. };
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++
May 22 '07 #3
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
Expand|Select|Wrap|Line Numbers
  1. class Subcriteria {
  2.  
  3. public:
  4.  
  5.   Subcriteria () {
  6.   };
  7.   Subcriteria *Next;
  8.  
  9.   unsigned int value;
  10.  
  11. };
  12.  
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
May 22 '07 #4
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:

Expand|Select|Wrap|Line Numbers
  1. class Subcriteria {
  2.  
  3. public:
  4.  
  5.     Subcriteria () {
  6.     };
  7.  
  8.     Subcriteria *SubCriteria;
  9.  
  10.     int value;
  11.  
  12. };
  13.  
  14.  
  15. class Criteria {
  16.  
  17. public:
  18.  
  19.     Criteria () {
  20.         value = 1;
  21.     };
  22.  
  23. public:
  24.  
  25.     int value;
  26.     Subcriteria *SubCriteria;
  27.  
  28. };
  29.  
  30. int main() {
  31.  
  32.     Criteria *C;
  33.     C = new Criteria[4];
  34.     C[0].SubCriteria = new Subcriteria[2];
  35.     C[1].SubCriteria = new Subcriteria[2];
  36.     C[2].SubCriteria = new Subcriteria[3];
  37.     C[3].SubCriteria = new Subcriteria[3];
  38.  
  39.     C[0].SubCriteria[0].SubCriteria = new Subcriteria[2];
  40.  
  41.     C[0].SubCriteria[0].value = 3;
  42.     C[0].SubCriteria[1].value = 6;
  43.  
  44.     C[0].SubCriteria[0].SubCriteria[0].value = 10;
  45.     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:

Expand|Select|Wrap|Line Numbers
  1.     C[0].SubCriteria[0].SubCriteria[0].value = 10;
  2.     C[0].SubCriteria[0].SubCriteria[1].value = 20;
And I want to store it at:

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. for each node starting from the leaves do {
  3.   calculate sum of all children of parent node;
  4.   assign sum to parent.value;
  5.   repeat for all children nodes until root node;
  6. };
  7.  
  8.  
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
May 22 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
I think you can avoid this type of coding by using an Observer design pattern.
May 22 '07 #6
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... :(
May 22 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
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 VehcileRegistration objects be advised of the change so they can report to their users that you have moved.
May 22 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Przemysław Bana¶ | last post: by
2 posts views Thread by satishchandra999 | last post: by
3 posts views Thread by drummond.ian | last post: by
15 posts views Thread by dav3 | last post: by
8 posts views Thread by Jeff | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.