Chapter – 9. Pointers

2006 Delhi

1.d. Find the output of the following program:

#include<iostream.h>
#include<string.h>
class state
{
char *state_name;
int size;
public:
state( )
{
size=0; state_name=new char[size+1];
}
state(char *s)
{
size=strlen(s);
state_name=new char[size+1];
strcpy(state_name,s);
}
void display( )
{
cout<<state_name<<endl;
}
void Replace(state &a, state &b)
{
size=a.size+b.size;
delete state_name;
state_name=new char[size+1];
strcpy(state_name,a.state_name);
strcat(state_name,b.state_name);
}
};
void main( )
{
char *temp=”Delhi”; state state1(temp),state2(“Mumbai”),
state3(“Nagpur”),S1,S2;
S1.Replace(state1,state2);
S2.Replace(S1,state3);
S1.display( );
S2.display( );
}

2006 Outside Delhi

1.d) Find the output of the following program:

#include<iostream.h>
#include<string.h>
class student
{
char *name;
int I;
public:
student( )
{
I=0; name=new char[I+1];
}
student(char *s)
{
I=strlen(s);
name=new char[I+1];
strcpy(name,s);
}
void display( )
{
cout<<name<<endl;
}
void manipulate(student &a, student &b)
{ I=a.I+b.I;
delete name;
name=new char[I+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
void main( )
{
char *temp=”Jack”;
student name1(temp),name2(“Jill”),name3(“John”),S1,S2;
S1.manipulate(name1,name2);
S2.manipulate(S1,name3);
S1.display( );
S2.display( );
}