#include<iostream>
#include<string>
using namespace std;
class string
{    char *a; 
     int b;
public:
       string() {a=0; b=0;}
       string(char *c)
       {
                   b= strlen(c);
                   a= new char[b+1];    //b+1 because one char extra for space
                   strcpy(a,c);
       }
       friend void show(string s);
      friend string operator+(string &s1, string &s2);
      
};
string operator+( string &s1, string &s2)
{
       string s3;
       s3.b= s1.b+s2.b;
       s3.a= new char[s3.b +1];
       strcpy(s3.a, s1.a);
       strcat(s3.a, s2.a);
       return(s3);
}
void show(string s)
{
     cout<<s.b;
}
int main()
{
    string s1("New");
    string s2("Delhi");
    string s3;
    s3= s1+s2;
    system("pause");
}

this is the code of my hw problem.....it does not work in my compiler.....i cant find the error in it...

You want to include <cstring> (which is the old string.h from C that has strcpy, etc) instead of <string>. As you have it now, you are including <string> which has a class named std::string, which is probably causing a name collision.

Edited 6 Years Ago by jonsca: n/a

You want to include <cstring> (which is the old string.h from C that has strcpy, etc) instead of <string>. As you have it now, you are including <string> which has a class named std::string, which is probably causing a name collision.

first of all thanks for the reply...secondly i tried that too bro....it is not working......:(

You have major problems in your code...I tried compiling it and set off many errors and warnings...Try starting with this shell.

#include <iostream>
#include <cstring>

class string
{

public:

	string():s(NULL), itssize(0) {}
	string(const char* const cstring);

	void displayit() const { std::cout << s << std::endl; }
private:

	char *s;
	int itssize;

};

string::string(const char* const cstring)
:itssize(strlen(cstring))
{
	s = new char[itssize + 1];
	for (int i = 0; i < itssize; ++i)
		s[i] = cstring[i];
	s[itssize] = '\0';
}

int main()
{
	string me("my string");

	me.displayit();
	return 0;
}

Edited 6 Years Ago by gerard4143: n/a

Comments
WTG for looking into it more deeply

can anyone please help me with this one...

please help me.... i need some help on how to get the grades of this program and also can u please correct it... cause i think something is not right...

#include<iostream.h>

int A[5];
int ctr;

main()
{
ctr=5;
while(ctr<5);
{
cin>>A[ctr];
ctr=ctr +1;
}
cout<<A[5];

ctr=5;
do
{
cout<<A[ctr];
ctr=ctr +1;
}
while(ctr<=4);

for(ctr=0; ctr<=4; ctr++)
cout<<A[ctr];
}

can u please make it right.. cause i think there's something wronge.... and can u please help me how to add the value of grades...

This question has already been answered. Start a new discussion instead.