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
Program of function overloading: class vector{                 private :                 int v[3];                   public:                 /*friend istream &

Create a program to print a battleship grid to the console and mark squares as destroyed

Implement the search property from catalogue menu option.  After selecting this option the user should be asked to specify the property using the following sub-menu: 1. Specif

C Program for MAXIMUM NO, MINIMUM NO AND SORTING   void main() {           int i,j,t;           int a[5];           clrscr();           for(i=0;i

Rules of Operator Overloading It is a function defined to an operator with new term or meaning. It cannot produce new operator. It cannot modified the meaning of th



what is a variable

Program is to search for a given character in a string: Program is to search for a given character in a string and print point of match char *stsearch(char *string, char sea

Conversion from Basic to User-Defined variable Consider the following example. class Distance                  {                   public  :