Class prototype vs C function prototype

J

June Lee

Is that for Class/Object function prototype, I must define the
function in header file or .cpp file.

MyClass::functionA();
MyClass::functionB();

but for C function prototype, I don't have to define if it's put
before the main() function the following is not needed -

void stradd (char *s1, char *s2);
void stradd (char *s1, int i);

=========
#include <iostream> // cannot be iostream.h??
#include <stdio.h>
#include <string.h>


#include <comdef.h>
#include <conio.h>
#include <windows.h> // must need for SYSTEMTIME

//must need C/C++ > General > Debug Information Format to debug
working

using namespace std; // for cout must have??

// concatenate two strings
void stradd (char *s1, char *s2)
{
strcat (s1, s2); // CRT <string.h> function
}

// concatenate a string with a "stringized" integer
void stradd (char *s1, int i)
{
char temp[80];

sprintf (temp, "%d", i);
strcat (s1, temp);
}



int main()
{
//SYSTEMTIME st = {0,0,0,0,0,0,0,0}; // cannot divide into 2
lines - must init all in one line
SYSTEMTIME st = {0}; // OK too

char str[80];
//char* str; // not OK will crash program

strcpy (str, "Hello ");
stradd (str, "there");
cout << str << "\n";

stradd (str, 100);
cout << str << "\n";

stradd (str, "hihi");
cout << str << "\n";

return 0;
}
 
S

sk_usenet

June Lee said:
Is that for Class/Object function prototype, I must define the
function in header file or .cpp file.

MyClass::functionA();
MyClass::functionB();

These are not definitions.
but for C function prototype, I don't have to define if it's put
before the main() function the following is not needed -

void stradd (char *s1, char *s2);
void stradd (char *s1, int i);

These are not definitions either. These are declarations. Compiler must know
about your function before you call them (through an appropriate
declaration/definition).
=========
#include <iostream> // cannot be iostream.h??

Yes, no iostream.h. That's non standard.
#include <stdio.h>
#include <string.h>


#include <comdef.h>
#include <conio.h>
#include <windows.h> // must need for SYSTEMTIME

Non standard.
//must need C/C++ > General > Debug Information Format to debug
working

using namespace std; // for cout must have??

Yes. Else reference cout as std::cout.
// concatenate two strings
void stradd (char *s1, char *s2)
{
strcat (s1, s2); // CRT <string.h> function
}

// concatenate a string with a "stringized" integer
void stradd (char *s1, int i)
{
char temp[80];

sprintf (temp, "%d", i);
strcat (s1, temp);
}
Do some error checking in your code. Like what happens when s1 is not big
enough to accomodate temp?
int main()
{
//SYSTEMTIME st = {0,0,0,0,0,0,0,0}; // cannot divide into 2
lines - must init all in one line
SYSTEMTIME st = {0}; // OK too

char str[80];
//char* str; // not OK will crash program

Right. Modifying a string literal is undefined behavior.
strcpy (str, "Hello ");
stradd (str, "there");
cout << str << "\n";

stradd (str, 100);
cout << str << "\n";

stradd (str, "hihi");
cout << str << "\n";

return 0;
}

HTH
 
J

Jim Cobban

June said:
but for C function prototype, I don't have to define if it's put
before the main() function the following is not needed -

void stradd (char *s1, char *s2);
void stradd (char *s1, int i);
If you are writing C++ write C++. C++, for migration purposes,
incorporates 99% of C as a subset, but the primary purpose of C++ is to
permit the compiler to catch implementation errors. If you write C
style code the ability of C++ to catch implementation errors is
extremely limited. In particular the reason that C++ includes all of
those containers, such as std::string and std::vector is because C style
pointers are DEADLY. They create an exposure for memory corruption, for
example if invoking any of your functions accidentally resulted in more
a string that is more than 80 characters long. They also create an
exposure for memory leakage, since any storage assigned to a pointer
must be explicitly deleted by you, whereas C++ containers will
automatically clean up when they go out of scope.
using namespace std; // for cout must have??

This is dangerous because it means that every single symbol declared in
any of the C++ headers you included will be brought into your program.
You should code:

using std::cout;
// concatenate two strings
void stradd (char *s1, char *s2)
{
strcat (s1, s2); // CRT <string.h> function
}

Others have explained that you should use the std::string methods and
operators for this.
int main()
{
//SYSTEMTIME st = {0,0,0,0,0,0,0,0}; // cannot divide into 2
lines - must init all in one line
SYSTEMTIME st = {0}; // OK too

char str[80];
//char* str; // not OK will crash program

It is useful to understand that C++, and its predecessor C, do not
actually implement arrays the way you find them in FORTRAN, or BASIC.
All that C++ has done is to define an operator [] (const unsigned index)
that applies to all pointer types:

pointer[index] is a synonym for *(pointer+index)

When you tried char * str, thinking that char * was a "string" type you
failed to initialize it. Therefore the pointer had whatever bit pattern
happened to be in the location in memory when the program started. In
this case, since the program had just started, the location probably
contained 0, which in most implementations is the null pointer. You
therefore attempted to reference the storage at location zero, which you
are not authorized to modify because it contains control information
used by the operating system.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top