hello all,
in the following code a -> next should print zero, but it is giving the output as 0xbf940000
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Node
{
public:
int data;
Node *next;
Node(int n=1)
{
data=10;
next=0;
}
};
void main()
{
Node *a;
clrscr();
a=new Node();
cout<<a->data<<endl;
cout<<a->next<<endl;
getch();
}
Try passing a number into the constructor, by not giving it a number you may be telling it not to use your own constructor but a do-nothing default constructor..
You should always write a default constructor, since it's used in a surprising number of situations.
i string with printf instead of cout and i got 0
I am not sure which platform or compiler you are using. The use of <iostream.h>, <conio.h>, void main(), etc. would suggest you are on a Windows platform.
The following gives the expected output on modern Linux platforms:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node()
{
data=10;
next=0;
}
};
int main()
{
Node *a;
a = new Node();
cout << a->data << endl;
cout << a->next << endl;
return 0;
}