473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::functi onA();
MyClass::functi onB();

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.hfuncti on
}

// 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 2950
* 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::functi onA();
MyClass::functi onB();
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.hfuncti on
}

// 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::ostringstr eam

std::string asDecimal( int x )
{
std::ostringstr eam 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.c omwrote in message
Is that for Class/Object function prototype, I must define the
function in header file or .cpp file.

MyClass::functi onA();
MyClass::functi onB();
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.hfuncti on
}

// 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.hfuncti on
}
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
6533
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() { super.f(); ... } in the child to first execute the parent stuff to be followed by the
3
59242
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 false; return /(\w+)\(/.exec(obj.constructor.toString());
8
1578
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 class of "obj"?
1
3530
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
12964
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 needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
4
2180
by: Spam Catcher | last post by:
When declaring class properties, what's the difference between: function EventBroker() { this.Register = function(eventName, handler) { } //AND
2
251
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 class="autocompleteFila"><td>XXXX</td><td><span class="autocompleteInput">YYYY</span><span class="autocompleteRet">ZZZZZ</span></td><td>AAAA</td></tr> <tr class="autocompleteFila"><td>XXXX</td><td><span class="autocompleteInput">YYYY</span><span
10
1792
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
3211
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) http://stanleyxu2005.blogspot.com
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.