Programming Help
Okay I've seen many programs that have classes that have variables (wow, right?) Now I've seen that these have like one or two variables that they need, and like maybe one or two that are optional. My question is: How do I make a function (in a class or not (prefferably not)) that has optional parameters?
I'd prefer this in C++ or Python but I don't mind if it's in Perl either.
Thanks, Reaper
P.S: I hope my post made sense, I'm rather tired at the moment and have just woken up so I appologise for any mistakes.
the parameters are optional. For example if you wanted to do something like:
int subtraction (int a, int b) { int r; r=a-b; return (r); }
To use the function later, you'd do something similar to:
z = subtraction (7,2); cout << "The first result is " << z;
Look at function overloading for making making various functions with different signatures. The signature is simply a collection of parameters passed to the function (by reference).
What don't you understand?
I know all about making functions and that you choose which parameters and such. I meant like…. say there's a function that needs a number…. but it's optional to put another number when you're calling the function.
Although, I don't think I've ever seen an examle of this in C++… maybe it's too strict a language for that. But for example… the MsgBox function in VB.Net (poor example, I know) in which you can have MsgBox("Hi") or MsgBox("Hi", "Title")
Hope I made sense Thanks, Reaper
that's what I'm trying to tell you… It's called "Function Overloading".
Take for example, a function. Let's say it takes one number and powers it, but you don't know the data type the user will be passing to it. So, you clone the function with different return types or parameters (bad definition, but it's in lamen's terms).
float power (float x, float y); double power (double x, double y); double power (double x, int y); int power (int x, int y);
Afterwords, you can call them, ie:
cout << power (a, b) << endl; // uses power(float,float); cout << power (2.0, 3.0) << endl; // uses power (double,double); cout << power (2.0, 3) << endl; // uses power (double, int); cout << power (2, 3) << endl; // uses power (int, int);
That's one way to overload the function, another one is more unique in that you can decide NOT to take any input as a parameter, but only call the function. What it does is within it's scope.
For example:
void f(); void f(int i); void f(char c); void f(string & s); void f(char c, int i); void f(int i, char c);
You can call any of those functions later on in your code, but it's still the same function (simply "overloaded", or cloned to respond to calls differently.
Ask more questions if you still don't get it.
Wow, I feel like an idiot now 'cause that was so simple. Sorry about the whole not understanding your first post thing lol
Thanks man!
… Rather than making a new thread, I decided to continue in this one. (I know this is noobish) How would I go about defining my own type in C++? [Edit] I undertand I'd use "typedef" but that's as much as I've found. [/Edit]
Thanks, Reaper
You can use TYPEDEF, or create a new class. For example:
typedef struct {
vector pos;
double mass;
int type;
string name;
} particle;
And to create an array of 3 particles, do:
particle particles [3];
Creating a new class is easier and more efficient, to my opinion. Do you know how to do that, or do you need help with it?
Okay I know I've already asked about this… but I thought I'd figured it out… but I haven't. I've looked around a little for this but I can't find anything. I basically want to make my own type. (C++) An example is that I want to be able to make a colour type. Where my colour, red for example, would be usable in a function.
A simple example I can think of is: I have a function that sets the colour of output. If I'm calling to the function I'd like a parameter to be "blue" or "red" (without quotes)
Like:
.... C++ stuff....
void MyFunction(colour clr)
{
if (clr == red)
{
... set red...
}
... etc...
}
int main()
{
... blah blah blah....
MyFunction(red);
...blah blah blah...
return 0;
}```
I know that's sloppy, it was only an example.
Thanks,
Reaper
You could #DEFINE the colors as constants, or you could use an ENUM, if you are only looking to assign sequential numeric values to the colors. Most likely, constants would work better in your case. I think it would be pointless to use a class for this type of intended use, as you'd only be defining one property: a "color's color".
You could also use an associative array to tie keys to values in an array (such as numerous colors).
I'm not entirely clear on what you're trying to achieve, though…
You could #DEFINE the colors as constants, or you could use an ENUM,
That is correct. But, I think he has already definied his "color" class and wants to make usage of it.
So, yes, reaper, you are in the right track. Unless you want to retract from C++ 's power by using more C (structured programming) than OOP.
I think it would be pointless to use a class for this type of intended use
That's actually erroneous. What if he wanted to make the colors have attributes, such as "LIGHT red" or "DARK blue"?
Then, he'd really be trouble. But if he uses a class (to begin with) he would have more flexibility. That's the power of OOP and C++. Use it to your advantage!
Yet still I'm stuck. Thank you for your posts. I'm probably just reading something wrong or missing something, I appologize for being such a pain.
I don't have the class yet as it was only an example.
I still don't understand what I need to do. If I made the class, what would I need to do to be able to do something like:
c1 = red;```
(imagining "colour" is the name of the class)
Again, I appologize for being such a pain.
Thanks,
Reaper
Well, you didn't design the class yet? Do some work first, then ask questions. I'm not a compiler. lol What you've used above (as code) can work, but with ENUM. You can also use it with classes if you're implementing them as ENUM (correctly).
colors choice; choice = black;
Look at: http://www.cplusplus.com/doc/tutorial/other_data_types.html
I didn't honestly read the whole thread, so if this is off topic… well whatever
in python:
#!/usr/bin/env python
def MyFunction(height, width, colour="Black"):
"""usage: MyFunction(height, width[, colour])"""
#do stuff with variables#
return #stuff that we did with variables
In that example colour will default to "Black" if not explicitly defined.
so MyFunction(10,100) # will work as will MyFunction(10,100,"Green")
Hope thishelped.
In the example you gave, you're looking to create an object with type as that of your class. Also, you're looking to initialize a property of the object. I'm not too fresh on C++, but in other languages, the term you'd be looking for is "constructor". For C++, this would probably help.
If not, please continue to elaborate on your problem, so that we might better understand what you're trying to accomplish.
reaper4334 wrote: Okay I think "enum" is the clostest I've got. Would I be able to link this with a class though? If so, how?
Ermm… put the ENUM inside the class? Someone else can probably give better advice than that, though. Also, I recommend:
http://www.google.com/search?hl=en&q=enum+class+C%2B%2B&btnG=Google+Search
Yeah but then I need to do "classname::enumtitle" everytime I want to declare it. Is there any way to change this?
Thanks, Reaper
My code, if anyone wants to see it (it's only a practice-type-thing):
using namespace std;
class week {
public:
enum Days {Mon, Tues};
};
int main()
{
week one;
week::Days p;
p = week::Tues;
cout << p << "\n";
system("pause");
return 0;
}
reaper4334 wrote: Yeah but then I need to do "classname::enumtitle" everytime I want to declare it. Is there any way to change this?
I'm sure there is. My guess would be a class with a constructor; pass the "day" string into the constructor and run it through a switch statement. The switch statement would basically say "if day says Monday, then pick the Monday object out of the enum", etc.. Also, since enum is a numerically-indexed list, you could probably refer to it by index instead.
Of course, without knowing what you plan to do with the day, I can't really see any other ways of doing it. For that matter, if you have to pass in the day name, then why even use an enum? If you could clarify the purpose behind your desired class, it would help… a lot.