Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Why is there always a constructor in C++ OOP?

I was told that, when the computer makes an instance in C++, it always makes a constructor. Even when you do not. Why?

2 Answers

Relevance
  • Jacob
    Lv 4
    8 years ago
    Favorite Answer

    The constructor made by the compiler would look like this in normal code:

    myClass::myClass() {}

    As you can see, it is completely blank. Inside the compiler it must be able to create an object so that you can use it in your code, the constructor will set all the operators up and fix them so that you can call it :

    myClass object;

    It also creates a 'new' object and the ability to have arrays:

    myClass * object = new object[5];

    Hope this helps! Good luck.

    Source(s): Programmer
  • Anonymous
    8 years ago

    No it does not, it calls the constructor function, it doesn't make one. To make an instance of an object the object MUST have a Constructor as one of it's methods, this method is called when you create a instance of your object and will initialize your object. Most of the time you are just setting variables to their starting or given values if the Constructor has parameters.

    If you try to make an instance that does not have a constructor either your program will crash or will not function correctly.

Still have questions? Get your answers by asking now.