Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class prototype vs C function prototype

Reply
Thread Tools

Class prototype vs C function prototype

 
 
June Lee
Guest
Posts: n/a
 
      04-12-2008
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;
}


 
Reply With Quote
 
 
 
 
sk_usenet
Guest
Posts: n/a
 
      04-12-2008

"June Lee" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Jim Cobban
Guest
Posts: n/a
 
      04-13-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
function prototype vs function declaration Ravishankar S C Programming 44 12-23-2007 11:40 AM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments