Constructors

Go down

Constructors

Post by Christopher on 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.
avatar
Christopher
Admin

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

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Constructors

Post by DineshThangaraju on 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 : 29
Location : India

View user profile

Back to top Go down

Re: Constructors

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

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

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

View user profile http://unixcpp.forumotion.com

Back to top Go down

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