//Addition Function
void cat(char*s);
String operator +(char *s);
};
void String :: cat(char *s)
{
int n = strlen(ptr)+strlen(s);
char *p1 = new char [n+1];
strcpy(p1,ptr);
strcat(p1,s);
delete [] ptr;
ptr = p1;
cout<<"1_0";
}
String String :: operator + (char *s)
{
String new_str(ptr);
new_str.cat(s);
return new_str;
cout<<"1_1";
}
int main(int nNumberofArgs, char* pszArgs[])
{
String a,b,c,d;
a = "\nI ";
b = " am ";
c = " idiot ";
d = a+"ok";
cout<<d;
cout<<"\n";
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
I am trying to learn c++ on my own and got stuck on following exercise. I always find char,char*,string confusing. Will appreciate if someone can guide me!
Exercise 14.3.1. Rewrite the operator+ function as a global friend function, as just
suggested. Review Chapter 13 if you need to do so. (Hint: No real change has to
be made to the code other than the way the function is declared. You can still get
away with having just one operator+ function.)
The real motive here is to be able to print "text" + strings (a,b,c)
I am already able to print string(a,b,c)+"text" with above code.
Exercise 14.3.3. Add a convert-to-integer function that calls the atoi library function
to convert the face value of the digits in the string (if any) to an integer. Also
write a convert-to-double function that calls the atof library function. (Note:
Remember to include the file stdlib.h. Also, these functions should return int
and double, respectively.)
Exercise 14.3.4. Write a String(int n) constructor that initializes the string data
by creating a string made up of n spaces, where n is the integer specified. Write
an operator=(int n) function that does a similar kind of assignment.
char*: A pionter to a place in memory held by a character.
string: A class representing a dynamic array of character pointers.
Other then saying you get the three data types above confused you haven't actually asked a real question. Posting the exercise from the book isn't the same as asking us a specific question since those will be based on the topics you've covered so far, and many of us have VERY different solutions then what the author would expect at this point in the book.
Ok, so for excercise 14.3.1 it says to change the operator+ function to global friend function.
I tried putting a prefix freind onto operator+ function and called it as a global function outside the class. This gives me an error saying thatC:\CPP_Programs\Trial\Chapter 14\example14g.cpp|35|error: 'String operator+(char*)' must have an argument of class or enumerated type|
C:\CPP_Programs\Trial\Chapter 14\example14g.cpp|47|error: 'String operator+(char*)' must have an argument of class or enumerated type|
C:\CPP_Programs\Trial\Chapter 14\example14g.cpp||In function 'int main(int, char**)':|
C:\CPP_Programs\Trial\Chapter 14\example14g.cpp|60|error: no match for 'operator+' in 'a + "ok"'|
||=== Build finished: 3 errors, 0 warnings ===|
All I am trying to do is make it work to do char*s + pointer as opposed to other way around.
Actually I just realised that the tutorial on this site uses the '+' operator in it's example. It will be better at explaining this stuff then I would: http://www.cplusplus.com/doc/tutorial/classes2/