Converting base type to class type (char to string), C/C++ Programming

Converting Base Type to Class Type (char to string)

 

class String

{ char *name;

int length;

public:

String(void)

{length =0;

name = new char[length+1];

}

String(char *s)

{length = strlen(s);

name = new char[length+1];//for null terminator strcpy(name,s);

}

void display(void)

{cout<

}

void join(String &a, String &b);

friend String operator+(String &a, String &b);

};

String operator+(String &a, String &b)

{String temp;

delete temp.name;

temp.length = a.length+b.length; temp.name = new char[temp.length+1]; strcpy(temp.name,a.name); strcat(temp.name,b.name);

return temp;

}

 

void String::join(String &a, String &b)

{length = a.length+b.length;

delete name;

name = new char[length+1];

strcpy(name,a.name);

strcat(name,b.name);

};

void main()

{

char *first="Electronic ";

String name1(first); String name2("and Communication ");String name3("Engineering");

String s1,s2; s1.join(name1,name2); s2.join(s1,name3); s1=name1+name2; s2=s1+name3;

name1.display();name2.display();name3.display();

s1.display();

s2.display();

}

 

Posted Date: 9/20/2012 8:19:55 AM | Location : United States







Related Discussions:- Converting base type to class type (char to string), Assignment Help, Ask Question on Converting base type to class type (char to string), Get Answer, Expert's Help, Converting base type to class type (char to string) Discussions

Write discussion on Converting base type to class type (char to string)
Your posts are moderated
Related Questions
Write a function to determine whether a number is prime: it will return true if the input is prime and false otherwise. Use it to see whether -7 is prime.

#questAt a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets wit

What are all of the implicit member functions of the class? Or what are all of the functions that compiler implements for us if we don't describe one? A: 1.      copy ctor

the problem description. The order of the C Program should be as follows: Variables and constants Use #define statements to define the constants. Use arr

What is a newline escape sequence? - A newline escape sequence is signified by the \n character. - It is used to insert a new line whereas displaying the output data. - T

Money is immutable and is associated with a currency. When two money entities are added or subtracted be sure to consider currency conversion. You must create a money object result


Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send


d) a "static destructor" is a static member function of the class which accepts one argument - a pointer to the object of that class to be cracked. It is probably utilized along wi