473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Class prototype vs C function prototype

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
3 2931
* 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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Da Costa Gomez | last post by:
Hi, I was wondering whether someone could shed some light on the following. Using inheritance in Java one can override a function f() (or is it overload?) in the child and then do: public f() {...
3
by: Pavils Jurjans | last post by:
Hallo, Is there some decent way how to get the object class name in a string format? Currently I use this: function getClassName(obj) { if (typeof obj != "object" || obj === null) return...
8
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the...
1
by: Jing You | last post by:
hi every one, I have got some confused problem when I try to write some custom object by javascript. Look at the example code here: <BODY> <script language="jscript">
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
4
by: Spam Catcher | last post by:
When declaring class properties, what's the difference between: function EventBroker() { this.Register = function(eventName, handler) { } //AND
2
by: Alberto | last post by:
Here is a DHTML sample file. Note that response of autocomplete.php should be a table like this: <table> <tr><th>Field 1</th><th>Field 2</th><th>Field 3</th></tr> <tr...
10
by: Ugo | last post by:
Hi guys, until now I make so: <div id="my_div"><!-- content --></div> MyClass.prototype.doSomething = function( ) {}; MyClass.prototype.assignFun = function( )
6
by: Xu, Qian | last post by:
Hello All, is there any handy tool to generate class diagrams for javascript? I have tried JS/UML, but it generates always an empty diagram. -- Xu, Qian (stanleyxu)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.