You must Sign In to post a response.
  • Program to explain multiple constructors in a class.


    Want to get program solution for computer programming exam of Diploma? Searching for answers for writing program to explain multiple constructors? Check out this page where our ISC experts have provided program solution.

    Following is a question that was asked in the question paper of Diploma in Computer Technology(Summer 2019)conducted by Maharashtra State Board of Technical Examination, Mumbai for subject Object Oriented Programming(17432)(G Scheme), Semester-IV

    This question is asked for 4 marks.

    Q) Explain multiple constructors in a class with suitable example. [4 Marks]
  • Answers

    2 Answers found.
  • Please include and header files before declaration of class.
    //PROGRAM TO IMPLEMENT MULTIPLE CONSTRUCTOR IN A CLASS.

    #include
    #include
    class example
    {
    private:
    int a,b;
    public:
    example(){} //default constructor
    example(int t)//constructor with one argument
    {
    a=b=t;
    }
    example(int x,int y)//constructor with two argumnts
    {
    a=x;
    b=y;
    }
    example(example &v)//copy consrtuctor
    {
    a=v.a;
    b=v.b;
    }
    void display(void)
    {
    cout<<"value of a is "< }
    };
    void main(void)
    {
    class example obj1,obj2(21),obj3(21,41),obj4=obj3;
    clrscr();
    cout<<"\nDefault Constructor is as follows\n";
    obj1.display();
    cout<<"\nConstructor with one argument is as follows\n";
    obj2.display();
    cout<<"\nConstructor with two arguments is as follows\n";
    obj3.display();
    cout<<"\nCopy Constructor is as follows\n";
    obj4.display();
    getch();
    }

  • Constructor:- In java constructor is basically use for initializing the objects. Constructor never returns any value. A constructor can be called only when the object of the class is created. The very important thing related to the constructor is that the name of the constructor should be matched with the class name.

    Multiple constructors:- A class can have multiple constructors means you have the right to define as many constructors as you need. Only the thing which we need to keep in mind during the defining the constructors that the signature or parameters of all constructors should be different. So when a class has multiple constructors in java with different - different parameters, we can say that this is the constructor overloading. Below I am taking one example to illustrate the function of multiple constructors.
    class ExampleOfConstructor
    {
    ExampleOfConstructor()
    {
    System.out.println("The constructor with no parameters");
    }
    ExampleOfConstructor(int x, int y)
    {
    System.out.println("Sum of x + y = " + (x+y));
    }
    ExampleOfConstructor(int x, int y, int z)
    {
    System.out.println("Sum of x + y + z = " + (x+y+z));
    }
    }

    public class Sample
    {
    public static void main(String args[])
    {
    ExampleOfConstructor obj = new ExampleOfConstructor();
    ExampleOfConstructor obj1 = new ExampleOfConstructor(5, 6);
    ExampleOfConstructor obj2 = new ExampleOfConstructor(5, 6, 7);
    }
    }


  • Sign In to post your comments