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 <iostream> // cannot be iostream.h??
<iostream>, used above, is standard.
<iostream.h> is 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.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
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