473,698 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which is the better way to define methods?

My c++ text tells me that I should define methods this way:

class Stack
{
int method(double t);
Stack(int s);
...
}

int Stack::method(d ouble t)
{
/* behavior */
}

but my experience with Java tells me that this way is better:

class Stack
{
int method(double t)
{
/* behavior */
}

Stack(int s)
{
/* behavior */
}
}

Which way do you use? Should I use the first or the second? I'd
rather develop good habits as I teach myself C++ rather than have to
go back and fix bad ones later. Thank you in advance for the help.
Jul 22 '05
14 1651
JKop <NU**@NULL.NULL > wrote in message news:<C1******* **********@news .indigo.ie>...
It's purely cosmetic.

[about putting function implementation in the class def or not]

It's largely cosmetic, but not purely.

- It can increase compile times on many platforms.
- It can change dependancies requiring more files to be recompiled.
- It exposes implementation details in header files un-necessarily.
Socks
Jul 22 '05 #11
> JKop wrote:
It's purely cosmetic. [about putting function implementation in the class def or not]

It's largely cosmetic, but not purely.

- It can increase compile times on many platforms.
- It can change dependancies requiring more files to be recompiled.
- It exposes implementation details in header files un-necessarily.

Socks


One might first question the practice of instantly putting every class, as
you create it, into a new .h file named after it.

Suppose method A::Q needs new class B. Write it as close to Q as possible.
Migrate B to a .h file if and only if someone else needs it.

If nobody needs it without A, migrate it to A's .h file.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #12
"Ivan Vecerina" <NO************ *************** *******@vecerin a.com> wrote in
message news:cc******** **@newshispeed. ch...
"Blue Ocean" <ag***********@ gmail.com> wrote in message
news:af******** *************** ***@posting.goo gle.com...
If that is right, let me extend the question a little. Should I
declare private methods in the .h file? Or should I declare them in
the .c file?


C and C++ are abit archaic in that they use this subdivision between a
header and a source file, which is an artefact from the way the compilers
work. Java avoids this problem because the equivalent of a header is
generated by the compiler automatically, in a binary form.


A side note: Java doesn't have the equivalent of a header, generated by the
compiler or otherwise. It loads and uses the class file for referenced
classes to resolve the static references at compile time. You may have
thought Java was "generating " the class file because it automatically
compiles it when needed if it can't find a precompiled class file, IF it can
find the source file with the same name. [This may sound a little confusing,
but I can't go into all the details on a C++ group -- see
comp.lang.java. programmer for further information if needed.]
--
Gary
Jul 22 '05 #13
"Gary Labowitz" <gl*******@comc ast.net> wrote in message
news:Xc******** ************@co mcast.com...
"Ivan Vecerina" <NO************ *************** *******@vecerin a.com> wrote in message news:cc******** **@newshispeed. ch...
C and C++ are abit archaic in that they use this subdivision between a
header and a source file, which is an artefact from the way the compilers work. Java avoids this problem because the equivalent of a header is
generated by the compiler automatically, in a binary form.
A side note: Java doesn't have the equivalent of a header, generated by

the compiler or otherwise. It loads and uses the class file for referenced
classes to resolve the static references at compile time. You may have
thought Java was "generating " the class file because it automatically
compiles it when needed if it can't find a precompiled class file, IF it can find the source file with the same name.

I'm aware of how Java functions. For the purpose of this discussion, the
compiled class file is for me the "binary form" equivalent of a C/C++
header,
in that this is where the compiler looks up information it needs for static
linking.

Some compilers already leverage 'precompiled headers', although they are
functionally limited (i.e. typically required to be the first header
included).

The (rarely available) implementation of template export also requires
something
similar to a header in compiled form.

Maybe someday, it will be common for C++ implementations to at least store
pre-compiled binary versions of headers. Or maybe some form of "class
export"
could make the 'pimpl' idiom be history, by avoiding contamination
with indirect header dependencies...
.... just thinkin' aloud ...
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #14

"Blue Ocean" <bl*********@ho tmail.com> wrote in message
news:33******** *************** ***@posting.goo gle.com...
My c++ text tells me that I should define methods this way:

class Stack
{
int method(double t);
Stack(int s);
...
}

int Stack::method(d ouble t)
{
/* behavior */
}

but my experience with Java tells me that this way is better:

class Stack
{
int method(double t)
{
/* behavior */
}

Stack(int s)
{
/* behavior */
}
}

Which way do you use? Should I use the first or the second?


Sorry, but your question makes no sense. I could try to speculate as to
what you mean, but you're better off proofreading that and taking another
shot. You defined a constructor in the second example, but not the first.
Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2141
by: Dan | last post by:
I have seen differing ways to pass values to a class: $db=new mysqlclass('localhost','user','passwd','database'); ..... OR $db=new mysqlclass() $db->host='localhost'; $db->user='user'; $db->passwd='passwd';
17
2552
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this with a link to old information that suggests use of "navigator": http://developer.irt.org/script/43.htm
52
2291
by: Darklight | last post by:
Question: Write a function named sumarrays() that accepts two arrays that are the same size. The function should add each element in the array together and place the values in the thrid array. Which answer is the better practice; /*EX9.C*/ #include<stdio.h> #define MAX 5
18
2046
by: Sean Kirkpatrick | last post by:
I have a very ugly problem and I need some sincere guidance. My legacy VB6 application depends heavily on DAO, especially as it relates to custom properties on database objects. These custom properties are, as I understand it, not avabilable with SQL Server, which we need to migrate to in the not too distant future. My boss, the owner of the company, requires that we provide for a transition plan that minimizes (he really wants none)...
2
2435
by: steve | last post by:
Hi, I'm trying to use a protocol class as a template parameter. The protocol class defines its own types and methods for working with them. The template class uses the types defined by the protocol and various methods, however I get errors when trying to compile this in Xcode while in Codewarrior it works fine. I'm looking for a way to either fix this in Xcode, or find another design solution that meets the needs of the problem.
14
9863
by: Derek Basch | last post by:
This one has always bugged me. Is it better to just slap a "self" in front of any variable that will be used by more than one class method or should I pass around variable between the methods? Flamewar........NOW! jk, I really do want other opinions. Thanks, Derek
20
1538
by: Parag | last post by:
Hi, I am trying to figure out best testing tool for my project. I have narrowed down my requirements to two tools NUNIT and VSTS unit. But I have used neither and I have to use only one of them. Hence can someone who has used them before share his/her experience on them so that I can get a better idea and make a proper choice ? Any link or suggestions are also welcome. Thanks and Regards, Parag
2
2119
by: Bill Jackson | last post by:
For example, class A: def __init__(self,a): self.a = a def __eq__(self, other): return self.a == other.a class B: def __init__(self,b):
8
2357
by: =?Utf-8?B?eWRibg==?= | last post by:
I need to write a program validate a text file in CSV format. So I will have a class DataType and a lot of of derived class for various type, e.g. IntType, StringType, FloatType, MoneyType, ... etc. For each column of a type, it may accept null/empty value. or not. It may have various max length for StringType, IntType,... etc.
0
9166
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
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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
7737
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...
0
5861
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
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2333
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.