contructors/destructors

Started by
25 comments, last by Xai 18 years, 6 months ago
hey, i almost got the hang of classes lets say i have a class, actually, i'll write this out

class Dog
{

public:

void bark(void);
void urinate(void);
void setWeight(age); //accessor funtion to get to int weight

private:

int age = 2;
int weight= 20;
};


int main(void);
{
      dog max;
      max.weight = 90
};


ok, now iknow this won't work, but how would i change max's weight, i can't do max.weight, cause weight is protected, soi need to use the setweight function, how do i do that? and another thing, how do i d the contructor and destrucotr, what do they do, and how do i work them thanks -Avont
Advertisement
To make the constructor make a method named Dog, to do the deconstructor do a funciton called ~Dog
Hope that helps
DONT LET THEM DISCRIMINATE! BRING BACK THE BLACK!
SetWeight isnt defined is it? well you would just call it and it would change the weight to the param you pass to it I suppose (if thats how you make it)
DONT LET THEM DISCRIMINATE! BRING BACK THE BLACK!
i don't understand constructors/destructos, explain them, they seem pointless to me, i dun't need em'
If you have a class with member functions / data you want to begin with a specific value, you can set that with a constructor. Then all the objects created from that class will have that initial value set. Just think of constructors as one way to initialize parts of your class. The destructors are just the opposite. They will destroy your class objects upon program exit. There is a bit more to it but this is the jist of it. If you want to get into the meat of them do a google search for C++ constructor tutorial.
Hey,

Yeah, you won't be able to initialise more complicated objects with the:
int age = 2;
int weight= 20;

You can only initialise ints in this manner, though I don't get why this is the exception. For a more complicated class, you may want to have something like:

class Dog{public:  Dog();private:  float age;  float weight;};


In which case you'd need a constructor to fill in the default values:
Dog::Dog(){  age=0.0f;  weight=2.0f;}orDog::Dog() : age(0.0f), weight(2.0f){}


And also, you can run other code. Like, if you're wanting to tell some other object that you just made a Dog, you can call that other object's MadeDog() function.

Hope this helps,

--CJM
class Dog
{

public:
Dog();

private:
float age;
float weight;
};


in that example, dog(): is the construcotr, but what is it doing, just creating an instace of the dog class?
Imagine a class that represents a USB device, for instance an FM/AM tuner ...

imagine that this class has functions to change the station and adjust the eq and things like that.

Now, before any of those functions can do what they are supposed to, you need to connect to the device ... one way to design this class is to put that code that handles connecting to the device in the constructor ... so it is guaranteed to be done when they make the object ... they don't have to remember to call things in any certain order ...

then suppose that you need to diconnect from the device at the Operating System level when you are done using it ... put that code in the destructor ... so as long as the program doesn't forget to free the object it creates, you won't keep holding a connection to the device open.

A better example (that you can test yourself) might be a logging class, which writes messages about what the program is doing to a file. You would want to open the log file as soon as they create the log object, and close the file when they destroy the log object ... so you would write that code in the constructor and destructor respectively.

An example with no resource usage (no memory to free or stuff like that):

Suppose you are writing a class which represents an ammo clip in your game. And this ammo clip class has data members that keep track of how many bullets are in the ammo clip. It would make sense that you would tell the ammo clip how many bullets it starts with, when you construct it ... and that perhaps you write a default constructor for the ammo clip that sets the amount of bullets to the maximum capacity ... so if people write a line of code like this:

AmmoClip newClip; // the clip is initialized by the constructor to have a full load;

AmmoClip newClip(7); // this clip only has 7 bullets

in cases like this a custom destructor isn't important, just the constructors are needed ..
you don't have to write ANYTHING to get a constructor and destructor that just "create an instance of the class" ... that is automatic by default.

you manually write a constructor or destructor only when you want to add code that does something different than what the compiler would do naturally ...

class SimpleData
{
public:
int Total() { return x + y; }

private:
int x;
int y;
};

the class above is complete ... and anyone can create or destroy one .. like this:

SimpleData myObject; // creates a SimpleData item on the stack ... that item will be destroyed when myObject goes out of scope (usually the end of the function or block)

or they can do

SimpleData *myObject = new SimpleData();

// use object here

// then delete the object when done
delete myObject;

both of those methods of making objects work, and both call the default constructor and the destructor (if you write one) ... but if you don't write one yourself ... the compiler knows how to make the object anyway .. it just allocates space for the object (which is the size of its 2 int variables ... x and y).

But what is the value of x right after you make the object ... I don't know. Neither do you. Cause you never set it to anything, you never told the compiler that it needed to set x or y to a value, so they might be ANYTHING.

But wouldn't it be nicer to initialize the data object to a predictable set of values ... perhaps (0,0)?

so we write a constructor ... like this:

class SimpleData  {  public:    SimpleData() { x = 0; y = 0; }    int Total() { return x + y; }  private:    int x;    int y;  };


so now, any time a SimpleData object is created, it starts life set to an x and y value of 0 ... that's what the constructor does.
so umm, to have something as a contructor it has to have two brackets at the end like { } right?



hmm, so i just use a constructor , umm, uhh, does something?

i still don't get this

This topic is closed to new replies.

Advertisement