Group: comp.lang.c++
From: "Alf P. Steinbach"
Date: Saturday, April 12, 2008 6:08 PM
Subject: Re: Class prototype vs C function prototype

* 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 // cannot be iostream.h??

, used above, is standard.

is not standard, but existed as a convention before the standard.

Some compilers still provide , some do not.


> #include
> #include
>
>
> #include
> #include
> #include // 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 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

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 // std::cout, std::ostream
#include // operator<<, std::endl
#include // 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

Safety Articles | Usenet Groups | Usenet News | Bluegrass