why need constuctors

hi everybody my question is simple why do we use them For examle there are two sample below arent they same thing


#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;



class deneme {

int a;
public:
deneme(int x){

a=x;
}
void goster(){
cout<<a;
}

};
int main (){
deneme d(4);
d.goster();
system("pause");
return 0;

}

Kod:




#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;



class deneme {

int a;
public:
int ata (int x){
a=x;
return a;
}

}d;
int main (){
cout<< d.ata(3);
system("pause");
return 0;

}

For examle there are two sample below arent they same thing


No. In the first one, the only way to create an object of type deneme is to pass it an int. In the second, the only way to create an object of type deneme is with the default constructor that takes no parameters. They're clearly different.

If I saw this in real code, I'd guess that in the first case you wanted to make it impossible to create an object of type deneme without having though carefully about what value to give it - you're making it impossible to create a object with unknown internal data. Can you think of cases where that might be useful? I can.

THANKS FRİEND
In the first example you can't change the value of "a", and in the second example you can't get the value of "a" without changing it. More proper to have separate "set" and "get" functions.

A reason for constructors is that it will save coding back in main. If I have a class called Rectangle, which stores the width, height, and area of a rectangle, I don't want to this every time I make a rectangle:
1
2
3
4
Rectangle myRect;
myRect.setWidth(5);
myRect.setHeight(10);
myRect.calcArea();


What if I forget to set the width? Rather, a constructor such as this:
1
2
3
4
5
6
Rectangle::Rectangle(double x, double y)
{
  width = x;
  height = y;
  area = x * y;
}

would allow:
Rectangle myRect(5,10);

The Rectangle is probably a case where you don't want someone making a rectangle without setting the internal data.
Last edited on
> why need constuctors?

Because classes have invariants.

The values of the members and the objects referred to by members are collectively called the state of the object (or simply, its value). A major concern of a class design is to get an object into a well defined state (initialization/construction), to maintain a well defined state as operations are performed, and finally to destroy the object gracefully. The property that makes the state of an object well-defined is called its invariant.

Thus, the purpose of initialization is to put an object into a state for which the invariant holds. Typically, this is done by a constructor. Each operation on a class can assume it will find the invariant true on entry and must leave the invariant true on exit. The destructor finally invalidates the invariant by destroying the object.
- Stroustrup

1
2
3
4
5
6
struct point 
{
    double x ; // any value is ok for x 
    double y ; // any value is ok for y
    // ...
};

point does not 'need' a constructor - though we may write one (or more) for convenience.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct time_of_day
{
    int hour ; // 0-23
    int minute ; // 0-59
    int second ; // 0-59

    inline bool valid() const // does the class invariant hold?
    { return hour>=0 && hour<24 && minute>=0 && minute<60 && second>=0 && second<60 ; }

    time_of_day( /* ... */ )
    {
        // whatever
        assert( valid() ) ; // the constructor must establish the class invariant
    }

    void increment()
    {
        assert( valid() ) ; // the the class invariant holds
        // increment time of day
        assert( valid() ) ; // the the class invariant still holds
    }
    
    // ...
};

time_of_day 'needs' a constructor - it has a non-trivial class invariant.
Topic archived. No new replies allowed.