setters and getters?

ok so my cs 202 class is in full sail and i gotta learn about oop. So i need to get rid of my structs and start using classes. My professor told us that we need to use setter and getter functions to set the next pointer and get it back in a node class . Node is a node in my linked list. My node class has 2 char data members and a next pointer.
So how exactally do i pass in the value into the right node and how do i implement the set and get functions? iam really confused about this stuff. Any help will be greatly appreciated. Thanks :)


PS: Heres my .h file

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
class node 
{
      public :
              int add_no_chain( char * temp_name , char * temp_type );
              int add_chain( char * temp_name , char * temp_type );
              int traverse ( node * temp , char * temp_name ,char * temp_type) ;
      private :
              char * name ;
              char * type ; 
              node * next ;
};


struct tree 
{
       node anode;
       int cost ;
       tree * left ;
       tree * right ;
};


class client 
{
      public:
             client();
             ~client();
             
             int add_ini();
             int add(tree * current, char * name , int  temp_cost , char * type);
             int display(); 
             int display_bal();
      private :
              tree * root ;
              int balance ;
};

http://www.cplusplus.com/forum/beginner/6415/
Hope this helps.

[edit] Er, give a sec for a better answer...

A tree is composed of nodes... but to be simple, a tree [i]is[/b] a node. That is, your "tree" and "node" classes should be a single class. The only thing you need to track is the root of the tree (or the root node) -- which you look to be doing on line 34.

I am not sure what exactly all the extra information you have got is for, so that's the best I can help for now (without more info).
Last edited on
my assignment says that i need to make a binary search tree of linked lists, i read that thread and i understand the reason behind it but i dont understand the syntax of it, i understand the setters, they basically take in data and set the data in the private section but how do i do this for a pointers. For example i need to set the next node to NULL after ive added data to it how would i do that using setters and getters ?
Heres my code , my program does this weird thing...any idea why ?

http://img815.imageshack.us/i/81693135.jpg/


Heres my code :



File 1
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
#include<iostream>
using namespace std ;
#include "Hfile.h" 





base::base()
{
            root = NULL ;
            balance = 0 ;
}


int base::add_leaf_ini() 
{    
     cout<<"name please \n ";
     char name[100];
     cin.get( name , 100 ) ;
     cin.ignore( 100, '\n' );
    
     cout<<"type please \n ";
     char type[100];
      cin.get( type , 100 ) ;
     cin.ignore( 100, '\n' );
     
     cout<<"cost please \n ";
     int cost ;
     cin>>cost ;
     cin.ignore ( 100  ) ;
      
     //comfirm loop
     cout<<"Test";
     balance = cost + balance ;                                                     
     base::add_leaf( root , name , cost , type );

	 return 1;
    
}

int base::add_leaf( tree *& root , char * name , int temp_cost , char * type )
{
    if(!root)                  
    {    
             root = new tree ;
             
             root -> head = new node ;
             root -> head -> add_no_chain ( name , type );
             root -> head -> set_next_to_null( ); 
             
             root -> cost = temp_cost ;
             root -> left = root -> right = NULL;      
             
    }
    else  
    {    
         if ( temp_cost == root -> cost )
         {
              root -> head -> add_chain(  name , type );
         }
         else
         {
              if( root->cost > temp_cost )
              {
                   add_leaf( root -> right , name , temp_cost , type );     
              }
              else
              {
                   add_leaf( root -> left , name , temp_cost , type ) ;
              }
         }    
    }
	 return 1;
}



int base::display()
{
    root -> head -> display();
	
	 return 1;
}

base::~base()
{
             delete root ;
             root = NULL ;
}





File2

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

#include<iostream>
using namespace std ;
#include "Hfile.h" 
#include<cstring>




node::node()
{
}

int node::add_no_chain( char * temp_name , char * temp_type )
{
    name = new char [ strlen ( temp_name ) + 1 ];
    strcpy_s ( name ,10, temp_name ) ;
    
    type = new char [ strlen ( temp_type ) + 1 ];
    strcpy_s ( type ,10, temp_type ) ;
	
	 return 1;
    
}

int node::display()
{
    cout<<name<<type;
	
	 return 1;
}

int node::set_next_to_null(  )
{
     next = NULL ;
	 
	 return 1;
}

int node::add_chain(  char * temp_name , char * temp_type )
{
    node * current ;
    current = this ;
    
    while ( current -> next )
    {
          current = current -> next ;
    }
    
    current -> name = new char [ strlen ( temp_name ) + 1 ];
    strcpy_s ( current -> name ,10, temp_name );
    
    current -> type = new char [ strlen ( temp_type ) + 1 ];
    strcpy_s ( current -> type ,10, temp_type );
    
	
	 return 1;
}

node::~node()
{
             delete next ;
             next = NULL ;
}





File3
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

#include<iostream>
using namespace std ;
#include "Hfile.h" 

int main()
{
    base abase ;              
    char choice;                                        
    do{                                                 
            cout<<"a to add , d to display , and   q to quit ";
            cin>>choice;
            cin.ignore( 100, '\n' );
            cout<<"\n";
        
            if(choice == 'a')
            {
                abase.add_leaf_ini();
            }
            else if(choice == 'd')
            {
                abase.display();
            }
            
            else if(choice == 'q')
            {
                 cout<<"See ya ";
                 return(0);
            }
            else                               //This happens if the user enters an invalid choice 
            {
                cout<<"Bad choice ";
            }
       }while(choice != 'q');
}



Hfile

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

class node 
{
      public :
              node();
              
              int add_no_chain( char * temp_name , char * temp_type );
              int add_chain( char * temp_name , char * temp_type );
              
              int display();
              int set_next_to_null(  );
              
              ~node();
      private :
              char * name ;
              char * type ; 
              
              node * next ;
};


struct tree 
{
       node * head ;
       
       int cost ;
       
       tree * left ;
       tree * right ;
};

class base 
{
      public:
             base();
             
             int add_leaf_ini ();
             int add_leaf (tree *& root , char * name , int cost , char * type  );
             
             int display();
        /*     
             int display_all_tree_ini();
             int display_all_tree(tree * current);
             
             int search_ini( char * key );
             int search( tree *& root , char * key );
             
          */   
             ~base();
             
      private:
              int balance ;
              tree * root ;
};























I'll use the node class as an example, it's fairly simple, the only complication is that the node class owns the memory for the name and type (and thus demonstrates nicely the need for a setter function, as opposed to raw access), so you'll have to free that in setter function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void node::set_name( const char * new_name ) //const param, you're not going to edit it
{
    delete name; //free up the old memory name was in
    name = new char [ strlen ( new_name ) + 1 ];
    strcpy_s ( name ,10, new_name ) ; //just copied how you did in the constructor, 10 chars
}

/*
should be a const method, it's not editing the class's members
user shouldn't edit the returned memory (it belongs to the node), hence the const return value
*/
const char * node::get_name() const 
{
    return name; //all you need to do here is return the name
}

Another note, it looks like "node::~node()" is responsible for name and type's memory and should be delete'ing those fields, but it's not. That needs to be fixed.
Last edited on
Topic archived. No new replies allowed.