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.
#include<iostream>
#include<windows.h>
usingnamespace 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
"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!