Class prototype vs C function prototype

Discussion in 'C++' started by June Lee, Apr 12, 2008.

  1. June Lee

    June Lee Guest

    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;
    }
    June Lee, Apr 12, 2008
    #1
    1. Advertising

  2. June Lee

    sk_usenet Guest

    "June Lee" <> wrote in message
    > 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
    --
    http://techytalk.googlepages.com
    sk_usenet, Apr 13, 2008
    #2
    1. Advertising

  3. June Lee

    Jim Cobban Guest

    June Lee wrote:
    > 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.
    Jim Cobban, Apr 13, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Ravishankar S

    function prototype vs function declaration

    Ravishankar S, Dec 18, 2007, in forum: C Programming
    Replies:
    44
    Views:
    1,538
    Flash Gordon
    Dec 23, 2007
  3. Replies:
    9
    Views:
    186
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    264
  5. javascript fish
    Replies:
    0
    Views:
    168
    javascript fish
    Oct 11, 2008
Loading...

Share This Page