-1

I am trying to understand what is a constructor and could you tell me why I am still able to run the program without error?

As you can see I am not doing it correctly but it still works!! why?? and when I tried to put LL=(object name ) after my class DELL it is not working? but when I retrieved the LL it works why??? If I am not clear do not hesitate to tell me and I am sorry in advance for this mess

#include <iostream>
#include <string>
using namespace std;


class DELL{
public:
    DELL(){
        cout <<" bla bl bla\n";
    }
    void setname(string x){
        name = x;
}

        string getname()
        {
            return name;}
private:
string name;


};

int main(){

    DELL (); // Variant 1
    DELL LL(); // Variant 2
    return 0;

}
SergeyA
  • 61,605
  • 5
  • 78
  • 137

3 Answers3

1

First of all, let's enhance your DELL class to add destructor to it. This will help us better understand the lifecycle of your object. Here is the code:

class DELL {
public:
    DELL() {
        std::cout << "DELL constructor called\n";
    }
    ~DELL() {
        std::cout << "DELL destructor called\n";
    }
};

Here are 3 possible ways you can play with this:

DELL(); - This does construct a temporary object of type DELL(). It is deleted immediately after this, and since it is not used, optimized compilers might simply drop this line altogether. However, if you disable optimization and compile/link/run your code, you should see a constructor call, immediately followed by destructor call. Example code below will produce constructor, destructor, checkpoint printouts in this order.

DELL();
std::cout << "Checkpoint.\n";

DELL LL; this creates an object of type DELL. This object will live until the end of the scope where it will be declared. If you put this into your main, and than output something after this, like following

{
DELL LL;
std::cout << "Checkpoint.\n";
}

You will see constructor call, "Checkpoint" and than destructor call as your output (again, provided no optimization is taking place).

DELL LL(); - this creates no objects. Instead, it declares a function named LL, which takes no arguments and return object of type DELL. When you run this code, you will see no output from constructor nor destructor, because no object was created.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

You've run afoul of a quirk of C++ grammar known as the Vexing Parse. In short:

DELL LL();

declares a function named LL which has no parameters and returns a DELL object. Compare the syntax of DELL LL(); and int main();.

This code:

DELL();

creates a temporary object of type DELL and initialises it with the default constructor.

This code:

DELL LL;

creates an object named LL, of type DELL, and default-initialises it. For a class, that means calling the default constructor.

Starting with C++11, you can use curly braces instead of parentheses for initialisation, which avoids the vexing parse:

DELL LL{};

This creates an object LL, of type DELL, and value-initialises it. For a class, that again means calling the default constructor.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

You are not doing it 'wrong'. You are simply not doing it 'right'.

You declare a class called DELL, and it has only one constructor that requires no additional parameters.

public:
    DELL(){
        cout <<" bla bl bla\n";
    }

Now, in your main() you call that constructor but you don't create an object of DELL class;

Proper way to create an object of DELL would be:

DELL LL;
 //and then you could maybe do
LL.setname("name of object");
cout<<LL.getname();

When you would run this program the output would be:

 bla bla bla
name of object

Also, you could create a pointer to object of DELL by doing>

int main(){

    DELL *ll;
    ll = new DELL();

    ll->setname("name of object");
    cout<<ll->getname();

    return 0;
}

And the output would be the same as the one above.

Rorschach
  • 734
  • 2
  • 7
  • 22
  • By the way I know that you do not use a constructor for this kind of situation but on the other hand i thought i constructor from my research was a kind special function which would give the same things as your second example. but it was not just another way to write faster the code instead to use--------------------------------------------------------------------------------- DELL LL; LL.setname("name of object"); cout< – Juststarted26092015 Oct 01 '15 at 15:09
  • I'm not sure I quite understand what you are saying... What I think you want is to set `name` in a constructor, and you can do that, but then you need a constructor with a `string` parameter... DELL(string str) { name = str; } – Rorschach Oct 01 '15 at 15:20
  • i was trying to do the same things as in this video https://www.youtube.com/watch?v=_b7odUc7lg0..... yes that is the part 2 in the videos the guy show another example using ........................class buckysclass{ public: buckysclass(string z){ setname(z); – Juststarted26092015 Oct 01 '15 at 15:23