constructor..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nareshjonnala
    New Member
    • Aug 2006
    • 1

    #1

    constructor..

    hai..this is constructor problem....
    i am creating one and more constructor's, then creating an oblect...that time which type of constructor called.....i want to my problem is creating one paramater constructor,two , three and more parameter's constructor....
    and when am creating an object that time calling all constructor as well as also pass value's to one after anthor conatructor.... .........(chann ing costructor..... .this is in java is suffient using THIS OR SUPER key word's.....)
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The constructor is called when the object is created

    Code:
    class BaseClass {
    public:
        BaseClass()
        {
            member = 0;
        };
        BaseClass(int initialiser)
        {
            member = initialiser;
        };
        int member;
    }
    
    
    BaseClass a;   // Calls default constructor
    
    BaseClass *pB = new BaseClass(6);  // Calls constructor with 1 parameter
    If you whant to write a derived class that calls a specific base cass constructor then you do it like this

    Code:
    class DerivedClass : PublicBaseClass
    {
    public:
        DerivedClass();
        DerivedClass(int initialiser);
    }
    
    DerivedClass::DerivedClass(int initialiser)
                     : BaseClass(initialiser)
    {
    }

    Comment

    • kumarmanoj_pat
      New Member
      • Aug 2006
      • 5

      #3
      Originally posted by nareshjonnala
      hai..this is constructor problem....
      i am creating one and more constructor's, then creating an oblect...that time which type of constructor called.....i want to my problem is creating one paramater constructor,two , three and more parameter's constructor....
      and when am creating an object that time calling all constructor as well as also pass value's to one after anthor conatructor.... .........(chann ing costructor..... .this is in java is suffient using THIS OR SUPER key word's.....)
      class no{
      int number1,number2 ,number3;
      public:
      no(void);
      no(int x)
      { number1=number2 =number3=x; }
      no(int x,int y,int z)
      {
      number1=x;
      number2=y;
      number3=z;
      }
      };

      main()
      {
      no a,b(2),c(1,2,3) ;
      }

      Comment

      Working...