Archived

This topic is now archived and is closed to further replies.

Spartan119

argument lists

Recommended Posts

Well this is my first game ever, no graphics, nothing fancy. Im pretty new to C++. Right now im writing the classes for my game. When I am creating the constructor for it, what is the argumentlist?!? //constructor nameofclass( what belongs in here?!); Im using the book "C++ Programming for the Absolute Beginner" by Dirk Henkemans (just in case anyone was curious or had a page reference). I know that you can specify "string","float", "int", etc, but im just not understanding what it does. I''ve set up the data members, does the constructor create this variable when the class is created? Any advice would be great...and feel free to criticize me if im completely wrong so i can be showed the right direction.

Share this post


Link to post
Share on other sites
The constuctor usually initialises member variables to some desired state or value, but by creating an overloaded constructor which takes arguments then you can construct an object with a whole range of different default values which is not possible without creating multiple editions of the class.

One particular example of using overloaded constructors is if you was to write a Log class for your game - you could either hard-code the filename of the log file into your class, you could create a method to set the filename or you could create an overloaded constructor like CLog(const char *LogFile) which sets the name of the Log file at the same time you create the class oject.

This is more flexible than the first two methods as you could have different Log objects referening different log files simply by using an overloaded constructor.

Share this post


Link to post
Share on other sites
Consturctor is called when you make instance of class (object).

It could be used for example like this:

#include <iostream.h>

class Point
{
public:
Point(int x, int y);
private:
int itsX;
int itsY;
};

Point::Point(int x, int y):
itsX(x),
itsY(y)
{}

int main()
{
// This will make Point1 object that has itsX value 130

// and itsY value 175

Point Point1(130, 175);

return 1;
}


"does the constructor create this variable when the class iscreated?"

No, variables will be made anyway.

"nameofclass( what belongs in here?!);"

If you don´t want to give any variable a value on initialization
stage then you can leave argument list empty.




[edited by - Mkk on March 18, 2004 12:59:41 AM]

Share this post


Link to post
Share on other sites
Argument lists are list of variables that is being referred to another functions. It may be a variable of any type.

example

main()
{
int x,y
get_number(x,y); //this will be your argument to the get_number function's a & b

printf("the 2 numbers are %i and %i, x,y);
return 0;
}
void get_number(int a, b)
{
a=1
b=2
}





Flamers are worst than Newbies

[edited by - makoy on March 19, 2004 4:51:14 AM]

Share this post


Link to post
Share on other sites