confused about classes

So I get the whole class symntex and everything for classes BUT the thing i dont get is the constructor.. i dont get it. I know it initalizes your members I think when you define them when creating the classes in the .h file BUT when your going into the .CPP file for that specific class you gotta out things in the constructor right? like for example after defining the class type you go into your specific .cpp file and look into your construct and it says this

1
2
3
4
5
6
7
8

phone::phone()
{

// defined varabiles right? what about during execution cant really define those 


}




sound like a dumb question but I guess you would define variables in there but what about if you need input from the user while the program is executing? Like if the program asks what kind of phone? what kind of model? when did you get it (date)? hope this makes sense sorry if it doesnt and I will try and explain better if it doesnt.
In C, you need to create things carefully. For example, given:

1
2
3
4
5
struct point_t
  {
  double x, y;
  };
typedef struct point_t point;

It is convenient to have a function that creates a point for you:

1
2
3
4
5
6
7
point Point( double x = 0, double y = 0 )
  {
  point result;
  result.x = x;
  result.y = y;
  return result;
  }

Now I can say useful things like:

1
2
point p1 = Point( 12, -7 );
point p2 = Point();

In C++, we call that convenient function a constructor, and make it part of the struct/class:

1
2
3
4
5
6
7
8
9
struct point
  {
  double x, y;
  point( double x = 0, double y = 0 )
    {
    this->x = x;
    this->y = y;
    }
  };

And, I can use it just as conveniently:

1
2
point p1 = point( 12, -7 );
point p2;  // Note that this is the same as saying: "point p2 = point();" 


PS. Don't forget about other nice C++ syntaxes:

1
2
3
4
5
struct point
  {
  double x, y;
  point( double x = 0, double y = 0 ): x( x ), y( y ) { }
  };
1
2
point p1( 12, -7 );
point p2;

Hope this helps.

[edit] But maybe I misunderstood the question...
Last edited on
You don't actually define your variables in the constructor. You initialize them. Every time you create a new object of your class the constructor is called to give its member variables initial values.
closed account (zb0S216C)
In the context of classes, the constructor is invoked (called) when the class is instantiated. The constructor is used to initialize the members of the class before any function or variable touches them, assuming they have access in the first place. The destructor does the exact opposite of the constructor; it destroys the members within the class as soon as the object (the name given to a class instantiation) goes out of scope. The destructor is the last to be invoked.

Wazzak
Ok so if variables are not defined in the constructor where are the variables suppose to be defined at?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MyClass
{
    int i; // define variable i

public:
    MyClass() // constructor
    {
        i = 9; // initialize i with the value 9 here
    }
};

int main()
{
    MyClass mc; // create an object of type MyClass (the constructor runs now);
}
I see thank you guys really appreciate it
Beginner has a follow-up question.

Taking your code from above, and changing line 15, am I effectively initializing the variable (I) with the value 10 instead of 9? Is this called overloading?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

class MyClass
{
    int i; // define variable i

public:
    MyClass() // constructor
    {
        i = 9; // initialize i with the value 9 here
    }
};

int main()
{
    MyClass mc (10); // create an object of type MyClass (the constructor runs now);
}
Not quite, you are thinking of assigning it, the constructor can take parameters like functions, but you need to tell it what to use:

MyClass(int c)

i = c; // initialize i with the value c here


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MyClass
{
    int i; // define variable i

public:
    MyClass(int c) // constructor
    {
        i = c; // initialize i with the value c here
    }
};

int main()
{
    MyClass mc (10); // create an object of type MyClass (the constructor runs now);
}
Tolerated, look at this constructor:
1
2
3
MyClass(int c){
     i = c;
}


The above constructor requires that you pass it a value, like with
MyClass mc (10);

However, as in the example by Duoas, if you put
1
2
3
MyClass(int c = 0){
     i = c;
}


You can choose to either pass it a value, OR just initialize it with the default value of 0 by using
1
2
MyClass mc (10); // Passing a value to the constructor
MyClass mc; // Allowing the constructor to accept the default value of 0 
Topic archived. No new replies allowed.