can anyone debug this cpp prog pls

Status
Not open for further replies.

bharat_r

In the zone
I have my Object Oriented Programming lab exam tomorrow. Hence I'm trying out some programs.
This program is to concatenate Strings using dynamic memory allocation.
I don't get a compiler error but when I run it it doesn't give output.
Please anyone find out the error & tell me asap before tomorrow morning..

Code:
#include<iostream.h>
#include<string.h>
class stringcon
{
int length;
char *name;
public:
stringcon()
{
length=0;
name=new char[length+1];
}
stringcon(char *s)
{
length=strlen(s);
name=new char[length+1];
}
void display(void)
{
cout<<name<<endl;
}
void join(stringcon &a, stringcon &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
int main()
{
stringcon name1("New"),name2("Delhi"),s1;
name1.display();
name2.display();
s1.join(name1,name2);
s1.display();
return 0;
}

thanks:)

pls help guys..
 
Last edited:
have u made this prog urself ..........coz i think its logic is nt correct ............
i cant point out the statement in which u have given a CHAR STRING value to the pointer NAME ..................srry i dnt have much experience bout c++
 
OP
bharat_r

bharat_r

In the zone
This was the program given in the C++ book by Balagurusamy. I just changed the function names & the variable names...
 

ayush_chh

Ambassador of Buzz
u missed out one statement i think.....here it goes.

Code:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class stringcon
{
int length;
char *name;
public:
stringcon()
{
length=0;
name=new char[length+1];
}
stringcon(char *s)
{
length=strlen(s);
name=new char[length+1];
[B]strcpy(name,s)[/B];
}
void display()
{
cout<<name<<endl;
}
void join(stringcon &a, stringcon &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
int main()
{
clrscr();
stringcon name1("New"),name2("Delhi"),s1;
name1.display();
name2.display();
s1.join(name1,name2);
s1.display();
getch();
return 0;
}
 
Status
Not open for further replies.
Top Bottom