-3

I was recently giving an online quiz on c++ and a question came that had similar syntax as

class className
{
     public:
     constructor()
     {
         print("ABC");
     }
}

int main()
{
    className ABC();
    return 0;
}

I thought it would not compile but rather it compiled and ran without having any effect, I am interested as to what feature this is and in which case do we use this?

1 Answers1

1

Your posted code won't compile. Ignoring that there are 2 reasons it doesn't do anything.

  1. className ABC(); declares a function. To call the default constructor of a class simply omit the brackets: className ABC;
  2. className has a method called constructor but no contructor so doesn't print anything when it is constructed.
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60