hi i want ask how things get copied when i assign one object s1 to the another object
s3(s3=s1)of the same class string in the following given example,also tell why constructor 1 needs to be initialized like str[0]='\o' ..thanks in advance

#include<iostream.h>
#include<string.h>
#include<conio.h>
const int max=80;
class string
{
private:
char str[max];
public:

string(){str[0]='\0';}         /*constructor 1*/
string(char s[]){strcpy(str,s);} /*constructor 2*/

void display(){cout<<str;}       /*display*/
void cpycat(string s2 )
{if(strlen(str)+strlen(s2.str)<max)
strcat(str,s2.str);
else
cout<<"string lenght exceeded";
}};
void main()
{
string s1("merry christmas");
string s2=",seasons grettings";
string s3;

cout<<"\ns1 ";s1.display();
cout<<"\ns2 ";s2.display();
cout<<"\ns3 ";s3.display();
s3=s1;                 /*assign*/
cout<<"\ns3 ";s3.display();
s3.cpycat(s2);
cout<<"\ns3 ";s3.display();
}

void main is wrong. main should always return an int as in: int main().

The constructor doesn't need to be initialized like that, but it is a good idea. It basically cause str to be an empty string instead of str being some junk value.

Declare and define both a copy constructor and an assignment operator. You'll want to use strcpy() in both.

The syntax s3(s3 = s1) makes no sense to me. If you want to assign s1 to s3 you would do this:

string s1("happy");
string s3;
s3 = s1;

To use the copy constructor you would do this:

string s1("I'm");
string s3(s1);

One big thing you should do for your programming in general not just for posting on forums is to put white space in your code so you can actually read what is going on. White space does not make your program a bigger executable and instead just makes it take longer to figure out what is going on and where errors could be.

Other than the fact that you are using other header files and you are making a class called string with the iostream library included there are no real errors to this.

For your questions when you make a class they have default constructors for things like = which you can overload if you want. By default the = operator just makes its self an exact copy of the object you have being compared to (of the same class). You can overload the = operator and make it do anything you want that would otherwise be completely illogical, like deleting the contents of your class.

void operator = ( string in )
{
	memset(str, max, 0);
	str[0] = 'p';
	str[1] = 'i';
	str[2] = 'g';
	str[3] = '\0';
}

Here is your code all cleaned up and changed a tiny bit.

#include <iostream> //<iostream.h> is really old do not use it

const int max = 80;

class string
{
	//classes are default private
	char str[max];
	public:

	string() //constructor 1
	{
		str[0] = '\0'; //if not done like said above it gives a junk value '\0' tells it this is the end of the string
	}

	string( char s[] ) //constructor 2
	{
		strcpy( str, s );
	}

	void display() //display
	{
		std::cout << str << std::endl;
	}
	void cpycat( string s2 )
	{
		if( int(strlen(str) + strlen(s2.str)) < max )
			strcat( str, s2.str );
		else
			std::cout << "string length exceeded" << std::endl;
	}
};
int main() //void main is wrong always use int main
{
	string s1("merry christmas");
	string s2 = ",seasons grettings";
	string s3;

	std::cout << "s1 ";
	s1.display();

	std::cout << "s2 ";
	s2.display();

	std::cout << "s3 ";
	s3.display();

	s3 = s1; /*assign*/

	std::cout << "s3 ";
	s3.display();

	s3.cpycat(s2);

	std::cout << "s3 ";
	s3.display();

	getchar(); //makes a pause at the end
	return 0;
}

thanks for guiding , why have you used std::cout << "string length exceeded" << std::endl; instead of cout << "string length exceeded" << endl; this ,and how to write the program into the numbered lines ( which tag to use as i am new here)

If you put using namespace std; at the top you wouldn't have to put std::cout and std::endl because they are in the scope of std, however the C++ string library is included in the iostream library and is in the scope of std as well. So just out of me not really knowing and the fact that the object "string" has already been defined in the std namespace I would either not use std on a global scale or just rename your string class to something else.

If you renamed your class and put using namespace std; then yes you would not have to put std::cout and std::endl.

Actually, you can still resolve std::cout and std::endl globally so that you don't have to explicitly resolve them every time you use them.
To do so, you would use individual "using" statements instead of a generalized using statement like using namespace std; .

#include <cstdlib>
#include <iostream>
#include <string>

//using namespace std; //can't use, could cause conflict
using std::cout;
using std::cin;
using std::endl;

int main() {
  cout << "This now works without explicit resolution\n (i.e. std::cout)" << endl;
  return 0;
}

Yes I forgot about just putting using std::cout and the others that works better than the way I said.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.