problem of creating objects from another classes

Pages: 123
it is work ,, but i have 2 questions:

1- if all pointers of the type (long *)
why i have to convert the address value each time to long ????

2-see lines 11 and 12 in the newintcont() function,, last version

why
incont->keys[cur_key] = (long )temp;
head=(long *)incont;


why this diference,, one time i don't use * ,, but in the next statment i use * ?????
Dude, I just looked few lines of your code, and I don't feel very good about what you are doing.
your * head if of bptree type. and bptree is a class whose data member is bptree *head; then you have another data member again in bptree class internal_container *incont; And after all this you are typecasting that internal_container *incont to long *. Now I have no idea why are you type casting two totally different pointers to one another. It is compiling until now doesn't mean it does not have bugs or it will not break. You need to revise that code dude.


P.S. I just looked that code for like 1 min. So I might be interpreting it wrong. I am sorry in advance for that.

i know i asked too many questions;;;;

but how can i print all the data in each level in my tree ??

should i create a pointer and give it the head address then retreive the data???
should this pointer be of the type long ??? or what??

any suggestion ???
????????
no help
?????????
I would first of all modify your code such that it is not pointing to pointer of another types. Like it is right now.
Also, don't complain about having no help after only like 4 hours...wait at least a day or two.
thank you mr kevichkin,,
that will be nice ,, i hope that you can build my tree in a better way ..........
Oops I am sorry for misunderstanding, what I wanted to say was If I were you, I would do what I suggested in my previous post. I am sorry to say but we don't complete home works or projects here dude. If you have any problem then post and we'll try our best to help you.

1- if all pointers of the type (long *)
why i have to convert the address value each time to long ????


you dont have to typecast it to long, it will give error. it is of type long* so typecast to long* only. this is correct:
head=(long *)incont;


second thing the difference between:
incont->keys[cur_key] = (long)temp;

and this
head=(long*)incont;
// error correct.??

in the first line you are assigning it to keys which is of type long only
writing it like this:
incont->keys[cur_key] = (long)temp is equal to
*(incont->keys + cur_key) = (long)temp;, now you are dereferencing it so it will make it long, but in the second line you are doing it:

head=(long*)incont; here you are not dereferencing it so head is of type long * and long. thats why typecasting it to long*

lets take an example:
int *ptr = 100;
int temp = 0;

if you write this ptr = temp; //error, ptr is of type int* and temp is int
ptr = &temp; //correct now both are type int *
*ptr = temp; //correct you are dereferencing it, so ptr is of type int and temp is also int.
*ptr = &temp; //error, here ptr is of type int and temp is of type int*

i hope now you can figure out why at some point you take long and sometimes long*.

still if its not clear ask!!! ;)
read the chapter on pointers from:
the c programming language by Dennis Ritchie


and lot many things will get clear.
thank you Mr. writetonsharma
the explanation is so clear......
thanks
great..!! :)
thank you all for your support,,,
Mr.kevinchkin ,,, i did like what you suggest and modify my pointers,,,, i think it is less complicating now....
the code become like this


no changing in ( bptree2.h) but the ( bptree.h) is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#ifndef B_PLUS_TREE

#define B_PLUS_TREE
#include <iostream>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include "bptree2.h"
using namespace std;
class bptree
    {
	public:
		
		int max_key;
		int max_slot;
		int cur_key;
		int cur_leaf_slot;
		int cur_in_slot;
		internal_container * head;
		internal_container *incont;
		leaf_container *leafcont;
		void insert(string , int , int );
		void insert_leaf(string , int , int );
		void insert_internal(int , int );
		void newleafcont();
		void print();
		void print_data(internal_container *);
		void print_data(leaf_container *);
				void newintcont();
			bool f;

		//initilization
		bptree(int mk,int ms)
		    {
			max_key = mk;
			max_slot=ms;
			cur_leaf_slot=0;
			cur_key= 0;
			cur_in_slot=0;
			head=NULL;
			f=false;
incont = new internal_container(max_key, max_slot);
leafcont=new leaf_container(max_slot);
		    }
	};//end class bptree


#endif 


the bptree.cpp become:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <ostream>
#include "bptree.h"
#include "bptree2.h"
using namespace std;


void bptree::print_data(internal_container *d)
{
cout<<"   print_data    ";

// it will print the internal_container data (d->in_node[].st, d->in_node[].et)
};

void bptree::print_data(leaf_container *d)
{
cout<<"   print_data leaf    ";
// it will print the leaf_container data (d->leaf_node[].st,  d->leaf_node[].et and d->leaf_node[].mob_id)

};

void bptree::newintcont()
{
		cur_key=0;
		cur_in_slot=0;
		incont = new internal_container(max_key, max_slot);
		internal_container *temp;
		temp=NULL;
		temp=(internal_container *) head;
		incont->keys[cur_key] =(long) temp;
		head=incont;
cout<<"\n insert new internal container at "<<head<<"   "<<(long) head<<endl;

		};

void bptree::newleafcont() 
	{
		
	leafcont= new leaf_container(max_slot);
cout<<"\n newleafcontainer at "<<(long )leafcont<<endl;
	incont->keys[cur_key] = (long )leafcont;
	cur_leaf_slot=0;
	 cur_key++;
cout<< "   cur_key    "<<cur_key<<"    ";

	};


void bptree::insert_internal(int st, int et)
{cout<<"\n insert slot at "<< st  <<"sec"<<endl;
                incont->in_node[cur_in_slot].st=st;
		incont->in_node[cur_in_slot].et=et;
                cur_in_slot++; 
};
void bptree::insert_leaf(string mob_id, int st, int et)

	{cout<<endl<<"  insert at  "<<st<<"   second    ";
		leafcont->leaf_node[cur_leaf_slot].st=st;
		leafcont->leaf_node[cur_leaf_slot].et=et;
		leafcont->leaf_node[cur_leaf_slot].mob_id=mob_id;

		cur_leaf_slot++;
cout<< "   cur_key after insert leaf    "<<cur_key<<"    ";		 
	};
void bptree::insert(string mob_id, int st, int et)
 {

	if(head==NULL)
            {cout<<endl<<"NULL head"<<endl;
		insert_internal(st, et);
		insert_leaf(mob_id, st, et);
      		head=incont;
		incont->keys[cur_key] = (long )leafcont;
		cur_key++;
		cout<<" head   "<<head<<"    "  <<(long)head<<endl;
						
	    }//end if
	else//head!=NULL
	   {	
			if(cur_leaf_slot<max_slot) // LEAF

		 	      {
				insert_leaf(mob_id, st, et);
				if (incont->in_node[cur_in_slot-1].et<et)
					incont->in_node[cur_in_slot-1].et=et;
				
		                }//end if(cur_leaf_slot<max_slot)

			else 
			{
		cout<< "   \n cur_key    "<<cur_key<<"    maxkey"<<max_key<<endl;
			if(cur_key<max_key)
		       {
			
			cout<<"  ora    ";
			newleafcont();
			insert_leaf(mob_id, st, et);
			if(cur_in_slot<max_slot)
			insert_internal(st, et);
	          	}//end (cur_key<max_key)

			else 
			{
				
				newintcont();	
				newleafcont();
				insert_internal(st, et);
				insert_leaf(mob_id, st, et);	
			}//end last else
                       }
	}//end  head !=null

 };  //end insert




	void bptree::print()
	  {
		cout<<"\n print"<<endl;
		internal_container *icp;
		leaf_container *lcp;
		icp=head;
print_data((icp->keys[0]));
	  }//end print


int main()          
{       
bptree tree(3,2);
tree.insert("m4", 1, 7);
tree.insert("m2", 2, 4);
 tree.insert("m1", 3, 12);
 tree.insert("m6", 4, 12);
 tree.insert("m0", 5, 12);
 tree.insert("m6", 6, 3);
 tree.insert("m9", 7, 20);
tree.insert("m6", 8, 12);
 tree.insert("m0", 9, 12);
 tree.insert("m6", 10, 3);
 tree.insert("m9",11, 20);
tree.insert("m6", 12, 12);
 tree.insert("m0", 13, 12);
 tree.insert("m6", 14, 3);
 tree.insert("m9",15, 20);
tree.print();


return 0; 
}///end main 



the problem now

i whant to print the tree,,, i have to start from the root, so first i take the head address , the head is of the type internal_container,,, then i want to print the data inside this container and print children of this container,, these children may be leaf_container OR internal_container ,,, so i make 2 function for printing the data acoording to the passed parameter,,, the problem is ,, how can i know the parameter type that the key[ ] pointed to ????

thanks in advance :)
Last edited on
Well, you certainly can't know it automatically. So why not have a another data member in your class as Type. In that type you can give it a value 0 if it is incont and 1 otherwise.
Then before printing just check this value and call the appropriate print function.

Hope this helps !
actually i think of this solution, but i thought maybe another solution,, iam looking for type cheking or something like that ,,,, i think i will weit for another solution ...........
? sugesstion ?
so what exactly you are now trying to do??
i write my problem in my last reply,,,,,
whatch the last code,,, and this is the problem...............

i whant to print the tree,,, i have to start from the root, so first i take the head address , the head is of the type internal_container,,, then i want to print the data inside this container and print children of this container,, these children may be leaf_container OR internal_container ,,, so i make 2 function for printing the data acoording to the passed parameter,,, the problem is ,, how can i know the parameter type that the key[ ] pointed to ????
you mean you want to know if this of type leaf_container or internal_container??
correct??

but key is storing the address only, and i think only from address you cant guess that.. you need to store something else for that.. am i understanding your problem?
yes,, u r right

i think maybe there is a way for type checker???
Pages: 123