use of an object

If you have a library that only has functions, and no real variable declarations what's the point of creating an object just to be able to call a function? lol
Lots of libraries have only functions and you don't need to create objects to use them. Did you mean "If you have an class that only has functions, and no real variable declarations what's the point of creating an object just to be able to call a function?"

If you did, then it's so that you can call the function. How else would you call the function, unless it's some kind of static function. Is this some kind of trick question? Remember, you can also inherit from it, and such classes are often referred to as "interfaces" or "protocols".
Last edited on
nope not a trick question, just a newb one. I don't know a lot about the language, I'm learning, and picking up on things as I'm coding, watching tutorials, and reading...

I'm wondering because I have a cpp file with a namespace, and class in it, with a function in the class. I had called it like..

1
2
OgreWeaponStrikes::OgreSwordStrikes MyObject;
MyObject.OgreRanSwordStrikesBtl(1,34,23, "the rat");


doesn't it seem a little redundant to create objects every time you want to call a function, or can you call the function over and over again simply by using the name of the object?

I find objects, and stuff like that a little confusing. This deeper stuff is my roadblock to becoming a more successful newb, haha.

I wish I could grasp these concepts a little better.
Last edited on
You do not have to put functions inside objects. If you want to use a function without creating an object, simply do not make the function a class function. For example, the printf function does not require you to make an object to use it.
So it's completely acceptable to create a library of functions that aren't in namespaces, or classes? Have the header file contain the prototypes, and the cpp contain the source?

That would make a lot more sense for the project I'm doing. I'm creating a library, but it's mostly focused on functions more than anything.

could this library still be converted into a dll file?
So it's completely acceptable to create a library of functions that aren't in namespaces, or classes? Have the header file contain the prototypes, and the cpp contain the source?

I give you the C programming language, of which C++ is very nearly a strict superset. If it's legal in C, it's legal in C++. C does not recognise class or namespaces.

could this library still be converted into a dll file?

Yes. If it compiles, you can make a library out of it.
Last edited on
C does not recognize class or namespaces.


There's no classes, or namespaces in c? Is it still object oriented, or would that be impossible without classes?
Last edited on
Is it still object oriented


C is generally considered not to be an object-oriented language. The first C++ compiler worked by writing (horrible) C code which was then compiled, but just because you can create a bastardised, horrifically ugly system of mutant object things in C does not win it the label "object oriented".
hahah, C sounds interesting. I might take a look at it. Do you enjoy it more than C++. I can't say that I don't like C++, lots of times I love it. Still, might take a look into C. Would it be considered as fast? I know the industry standard for things like game programming still seems to be C++.
I might take a look at it.


You already have.

int main() is legal C. int x = 5; is legal C. All of C is contained within C++.
What's missing from it that c++ has...just namespaces, and classes? I find them very difficult. In fact at the moment I don't even know how to fully utilize them to their full potential.
1
2
3
Person me, you;
me.jump(); //this is different then
you.jump();

That is the point of calling a method (member function) on an object class instance.)
The instance is secretly passed as a parameter via the variable *this.
I see. So it's different object making use of the same function. Makes things a tad bit clearer. If you have functions that only deal with parameters passed in to do a simple calculation, and display a message there's no sense in having it in a class eh. That would be a function that would make more sense to stand on it's own...

like a function that takes in enemy health, damage amount, a name...

displays a message base on a simple calculation, and returns enemy remaining health. There's no sense in that being in a class, because there's no class variable involved... Am I right?
There's no sense in that being in a class, because there's no class variable involved... Am I right?


Now we're into the realm of object design. A general rule of thumb is that an object should be responsible for it's own contents, so if you have a situation where an enemy takes damage, and has to change its health value, it makes a great deal of sense for that to be a function of the enemy object.

1
2
3
4
class enemy{
int health;
int takeDamage(int amountOfDamage);
};
Say a function was within a library, a use could implement them function into their own classes, no? I'm just wondering, because I'm more interested in programming a library, than I am actually using it, lol.

basically the function can take in use defined variables, and in theory display a message, amount of damage done, and return enemy health...

so far I've only got the first two working...the return value isn't pulling through, but that could be because of my own code in main while testing the function, I'm not entirely sure, lol..

Except your code there does help a lot. It gives me an idea of something to implement into my library :) an enemy class, and a player class...in case one doesn't want to use their own...

well if I do get the library finished, which I'm hoping I do, I may play around with it, and test some things myself. I'm not all that interested in creating a text game.

this is mostly for learning purposes that I took on the project.
Topic archived. No new replies allowed.