Click to See Complete Forum and Search --> : class and functions
lord loh
February 17th, 2003, 12:18 AM
What is the difference if I make a class with functions
Or
If I make a file with only functions and no class defn. and just include the file when I need?
I generally do not use all functions of a class. So are there any memory/speed related advandages?
Is there any other advantage of a class other than grouping functions ?
Manish Malik
February 18th, 2003, 09:04 AM
I assume you are talking C++, but you can translate the concepts to other OO languages too. I suggest you take a look at what a 'namespace' is. If your only requirement is to keep a bunch of functions that do not require to operate on some objects (depends on your program design), then you should be using a namespace to wrap them up. e.g.
-- myincludefile.h
#pragma once
// replace #pragma once with include guards if you are using compilers other than VC++ and gcc that do no support this directive
namespace myABClibrary
{
void myfunction1() { }
void myfunction2() { }
};
-- main.cpp
#include "myincludefile.h"
int main()
{
myABClibrary::myfunction1();
return 0;
}
or even
-- main.cpp
#include "myincludefile.h"
int main()
{
using myABClibrary;
myfunction1();
return 0;
}
Classes are used when you have designed your program (or a part of it) in terms of objects, that have members and methods that operator on them. Not suited for the job when all you require is to keep a library of few functions. Though if the size of the library grows more than that, you are better off redesigning a bit to effectively utilize a combination of namespaces and classes.
lord loh
February 19th, 2003, 03:26 AM
Can you point a link toward knowing more about name space.(I was not talking about c++ though!)
And I also wanted to know about speed/memory usage of class. Is it better to include a function and use it as and when required (at run time)?
Thank You!
bfarley
March 4th, 2003, 12:55 PM
If you were not taliking about C++, the point is moot. No functions can exist outside a class in C#.
Bill F
lord loh
March 5th, 2003, 08:20 AM
huh! :rolleyes:
I was not talking about C# either. It was php I was thinking of...
ergas
March 10th, 2003, 05:58 AM
If you can use a function, there is no reason not to use it concerning speed/memory, specially if you run your app on some PC.
Here is a sample, why to use class member function:
Suppose you have drawing function that takes CShape argument:
Draw( CShape *shape)
{
if( shape == "Circle" ) DrawCircle( shape );
else if( shape == "square" ) DrawSquare( shape );
}
In the sample above it is reasonable to make virtual DrawMe() member of CShape class.
Sometimes it may be better to use a function to perform some simple task.
lord loh
March 10th, 2003, 11:55 PM
Yes! I agree that functions are very very useful....but why put it in a class? it could be a function outside a class...if the language constructs permit so...(bfarley pointed out about the C#)...
I am really trying to understand "why use classes ?" when simple function do...
ergas
March 12th, 2003, 05:55 AM
In my little sample above there is pointed out, why to use class.
1. Use a class-member:
If some function have to check the object type, and then to take different actions, that is a classic example to use a class-member function.
2. Use a function
If the function is object-type independant, then there may be no reason to make it as a member of some class.
3. There is a lot more, like protection-issues. If a function is used only for one class, it is a good practice to make it a private member of this class.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.