Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Constructors

2 posters

Go down

Constructors Empty Constructors

Post by Christopher Wed Mar 03, 2010 10:26 am

Code:
#include<iostream>

class Sample
{
   int a;

public:

   Sample(int a)
   {
      this->a=a;
   }

   int getA()
   {
      return a;
   }

};

int main()
{
   Sample s;
}
The above code will produce error because once u have created parameterized constructor, default constructor wont be created automatically.

Code:

#include<iostream>

class Sample
{
   int a;

public:

   int getA()
   {
      return a;
   }

};

int main()
{
   Sample s;
}
But if no constructors are defined, default constructor will be created.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Constructors Empty Re: Constructors

Post by DineshThangaraju Wed Mar 03, 2010 11:58 am

how this works in java ? I guess default constructor will be called first followed by parameterized constructor(if any).. am i right ?

DineshThangaraju

Posts : 18
Points : 19
Join date : 2010-02-26
Age : 35
Location : India

Back to top Go down

Constructors Empty Re: Constructors

Post by Christopher Wed Mar 03, 2010 12:05 pm

no. if u r creating an object, only one constructor will be called depending on arguments.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Constructors Empty Re: Constructors

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum