By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Class prototype vs C function prototype

P: n/a
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.hfunction
}

// 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;
}
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* June Lee:
Is that for Class/Object function prototype, I must define the
function in header file or .cpp file.
Not sure what the question is, but yes, a function that is (potentially) called
when the program is run, must be defined somewhere.

And since a C++ program mainly consists of header files and implementation
files, the definition will necessarily, in practice, be in either a header
files, or in an implementation file.

>
MyClass::functionA();
MyClass::functionB();
Those are invalid declarations. A function must have a result type.

but for C function prototype, I don't have to define if it's put
before the main() function the following is not needed -
Also in C++ it's a good idea to define functions -- and anything else --
before the place of first use.

void stradd (char *s1, char *s2);
void stradd (char *s1, int i);
In C++ preferentially use std::string instead of char*.

Note that std::string provides the first operation directly, as a '+=' operator.

Also, when not using std::string you should focus on constness to communicate to
programmers, and have the compiler check, what can be modified and what can not
be modified -- and it's also good idea to use self-describing names:

void stradd( char* destination, char const* source );

=========
#include <iostream// cannot be iostream.h??
<iostream>, used above, is standard.

<iostream.his not standard, but existed as a convention before the standard.

Some compilers still provide <iostream.h>, some do not.

#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??
You should place this directive as locally as possible, i.e., for this program,
in function 'main'.

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

// 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
Uh, C++, except the preprocessor, has free format.

You can split those lines anyway you want.

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;
}
A C++ program that does conceptually the same both internally and in terms of
outer effect:

#include <iostream // std::cout, std::ostream
#include <ostream // operator<<, std::endl
#include <sstream // std::ostringstream

std::string asDecimal( int x )
{
std::ostringstream stream;
stream << x;
return stream.str();
}

int main()
{
using namespace std;

string str;

str = "Hello ";
str += "there";
cout << str << "\n";

str += asDecimal( 100 );
cout << str << "\n";

str += "hihi";
cout << str << "\n";
}

On difference is that this program will still be correct if the length of the
string exceeds 80 characters.

Also, much simpler when you get used to the notation.

Cheers, & hth.,

- Alf

Jun 27 '08 #2

P: n/a

"June Lee" <ii****@yahoo.comwrote 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.hfunction
}

// 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
Jun 27 '08 #3

P: n/a
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.hfunction
}
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.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.