Teen Programmers Unite  
 

 

Return to forum top

Classes in C++

Posted by PavalDaniel [send private reply] at April 13, 2002, 12:49:36 PM

I find classes a little difficult to understand an I have some questions:
1. What is the role of the constructor (and destructor)?
2. Does the constructor's name have to be the same with the class?
3. If I have a derivated class (subclass) its constructor must include the arguments of the base class?
Thanks.

Posted by metamorphic [send private reply] at April 13, 2002, 01:11:01 PM

1. Ok, when you create a class and then create an instance of that class, it is created in memory, exacally like a normal variable is. When this new instance of a class is created, the constructor is automatically called. Its sort of like (but not exacally) a function. Its normally used to set up member variables with acceptable data. The destructor is the opposite of the constructor. when the program finishes, the destructor automatically deletes the object from memory.

2. yes, and the destructor must be named the same as the class but prefixed with the tilda charactor (~)

3. why would you want to give the constructor arguments?
if you use accessor functions to change class members, you should have no need for passing values to the constructor.

Marc

Posted by CodeRed [send private reply] at April 13, 2002, 01:11:24 PM

1)Constructors initialize instance variables
2)Yes
3)No

class myClass
{
private:
int i,j,k;
public:
myClass(){} //Default constructor (must defined when other constructors are used)
myClass(int i, int k) //Overloaded constructor
myClass(int i, int j, int k) //Another overloaded constructor
}


void main()
{
myClass myObject1(); //using default constructor
myClass myObject2(10,5); //using overloaded constructor 1
myClass myObject3(15,10,5); //using overloaded constructor 2
}

Posted by CodeRed [send private reply] at April 13, 2002, 01:13:36 PM

Ooops, forgot the implementation of the constuctors:

class myClass
{
private:
int i,j,k;
public:
myClass(){} //Default constructor (must defined when other constructors are used)
myClass(int i, int k) //Overloaded constructor
{
this.i = i;
this.k = k;
}
myClass(int i, int j, int k) //Another overloaded constructor
{
this.i = i;
this.j = j;
this.k = k;
}
}


Posted by PavalDaniel [send private reply] at April 14, 2002, 12:41:35 PM

the word "this" is the instance, CodeRed?

Posted by Psion [send private reply] at April 14, 2002, 03:30:49 PM

I think this is a pointer to the current instance, so CodeRed's this.blah's should be this->blah's instead.

Posted by CodeRed [send private reply] at April 14, 2002, 04:01:22 PM

No, it works my way (I'm not saying your way is wrong, but it does work my way)

Posted by CodeRed [send private reply] at April 14, 2002, 04:04:06 PM

-Originally Posted by metamorphic-
"3. why would you want to give the constructor arguments?"

Because that is the ENTIRE purpose of constructors!

Posted by metamorphic [send private reply] at April 14, 2002, 06:15:03 PM

yeah i forgot constructors were ment to be dynamic. I was thinking when the constructor was called that default data was entered. I forgot that default data doesnt just mean '0' :). thanks for reminding me

Posted by Psion [send private reply] at April 14, 2002, 09:41:36 PM

CodeRed, your code doesn't compile in VC++. I'm reasonably close to 100% sure it won't work in any common C++ compiler.

Posted by CodeRed [send private reply] at April 14, 2002, 10:12:25 PM

You're right, I was thinking java, my apologies gian

Posted by gian [send private reply] at April 15, 2002, 02:30:10 AM

I believe you were referring to Psion.

Although, it *is* nice to hear you apologise every once and a while.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.