Originally Posted by
esbo
Firstly I would not want to add and print, I might want to add or print,
but not both.
A google on AddAndPrint returns 70 entries so it doesn't exist does it?
You have to write that first and you left that out.
It returns 1 entry from the UK, a lot of the 70 are not in English.
a=b+c;
printf("%d",a);
printf("%c",a);
printf("%f",a);
printf(%s%s",b,c);
The above covers most of your examples in 5 lines, and less characters than your
example which omits the 90% of the code you will have to write to make your
program work!!
It's like me saying, in C, program to play mp3 file:-
playthismp3file(mp3file);
I think there is a bit more to it than that!!!
are you deliberatly misunderstanding? AddAndPrint was a simple example of a function, it's not a real world function!
I admit there may be some benefits in the compiler or the compiled
prog doing some of the extra work for you (with a slow down in speed) but its all the classes, objects and other garbage I have a major problem with.
and what is your problem with it? in a lot of cases C++ "classes and garbage" can be FASTER than c!
let's take the string class as an example. let's say we want to concatenate strings in a loop
Code:
/* cat.c */
int main()
{
char buffer[1024]; /*again praying we have enough space*/
int numStrings = 0;
char loopString[20]; /* just a short string */
int i =0;
printf("please enter a string to repeat\n");
scanf("%s", loopString);
/* have to initialise our buffer */
memset(buffer, 0, 1024);
/*hopefully it'll be less than 20 chars otherwise we
a) crash, the best result or
b) we get one of those lovey buffer overrun security holes that plague IE */
printf("enter a number of times to loop\n");
scanf("%d", &numStrings);
for (; i < numStrings; i++)
{
strcat(buffer, loopString);
}
printf("%s", buffer);
}
now leaving aside the verbosity, insecurity and general buggyness of such code, let's analyse the performance characteristic of the loop.
Code:
for (; i < numStrings; i++)
{
strcat(buffer, loopString);
}
what exactly is happening here? strcat first searchs for buffer's null terminator (NT), so it loops from 0 to NT. then it copies from loopString. with each loop iteration buffer gets longer and strcat takes longer and longer to execute.
now let's look at the C++ version
Code:
int main()
{
// don't need to initialise or worry about size
std::string buffer;
std::string loopString;
int numStrings = 0;
cout << "please enter a string to repeat"\n;
cin >> loopString;
cout << "enter a number of times to loop\n";
cin >> numStrings;
for (int i = 0; i < numStrings; i++)
{
buffer += loopString;
}
cout << buffer;
}
it's cleaner, terser, more secure and easier to change.
but again, let's analyse the performance characteristic of the main work, the loop.
Code:
for (int i = 0; i < numStrings; i++)
{
buffer += loopString;
}
because buffer is now an object, it internally knows it's size. so when we do += the code generated simply starts copying at the end of the string, there's no searching for the NT, so it's actually FASTER!!
if you still don't understand, there's no hope for you.