Linked list

hi guys,i just wanna know that how you can access nested class data members like in my this code.this is my working code with one operation of adding new record.
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
#include<iostream>
#include<windows.h>
using namespace std;

class linklist{
      private:
      class node
{       
    public:
      
        string roll;
        string fname;
        string lname;
        string smester;
        string subject1;
        string subject2;
        string grade1;
        string grade2;  
        
          
      

      node *next;
      
      
};
node *head;
node *last;
public:
       linklist();
       ~linklist();
       void adstart();
      
       
      void destroylist();
      
       void update();
       void delonch();
       void show();
       void showonch();
       };
void linklist::adstart()
       { 
         node *ptr;
         ptr=new node;
         
          system("cls"); 
         cout<<" Enter Roll Number: ";
         cin>>info->roll; //here i just access data members of this class.
         cout<<" Enter First Name: ";     
         cin>>ptr->fname;
         cout<<" Enter Last Name: ";
         cin>>ptr->lname;
         cout<<" Enter 1st Subject: ";
         cin>>ptr->subject1;
         cout<<" Enter 2nd Subject: ";         
         cin>>ptr->subject2;
         cout<<" Enter "<<ptr->subject1<<"'s Grade: ";
         cin>>ptr->grade1;
         cout<<" Enter "<<ptr->subject2<<"'s Grade: ";
         cin>>ptr->grade2;
         cout<<" Enter Semester: ";
         cin>>ptr->smester;
         
         
     ptr->next=NULL;
    if (head==0)
    {
    
     head=ptr;
     last=ptr;
     }else
    
    {
        last->next=ptr;
        last=ptr;
    
    } 
    
   cout<<"\n\n\t\tStudent Successfully Entered!!! \n";
         Sleep(2000);
         }


up code has 2 class one main class name linklist and node class and its working fine.
now i wanna know that if i make a new class name info class in node class.means there are total 3 classes.
Like this
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
#include<iostream>
#include<windows.h>
using namespace std;

class linklist{
      private:
      class node
{       
    public:
       class info
       {
        public:
        string roll;
        string fname;
        string lname;
        string smester;
        string subject1;
        string subject2;
        string grade1;
        string grade2;  }; 
        
          
      

      node *next;
      
      
};
node *head;
node *last;
public:
       linklist();
       ~linklist();
       void adstart();
      
       
      void destroylist();
      
       void update();
       void delonch();
       void show();
       void showonch();
       };
//how i do cin and cout operation here.how it will work.
node *ptr;
ptr=new node;
cin>>ptr.info->roll; //should this work?? i tried but i didnt.
// please help....
"Node" only exists in the context of "linklist", so to access it, you have to specify:
linklist::node *ptr = new linklist::node;

In your second piece of code, you're accessing an object that doesn't exist. You defined an Info class inside the context of Node, but Node doesn't have an Info object.

Just add an Info member to the Node class (right next to node *next, for example).

[edit]
The info object should [b]not[/i] be a pointer in this case!
Last edited on
Topic archived. No new replies allowed.