Pass parameters to base class constructor though derived class or derived class constructor - Inheritance

Q.  Can we pass parameters to base class constructor though derived class or derived class constructor?
- Published on 19 Oct 15

a. Yes
b. No

ANSWER: Yes
 

    Discussion

  • Raj   -Posted on 19 Oct 15
    Yes, we can pass parameters to base class constructor though derived class or derived class constructor.
    class baseClass {
    protected:
    int i;
    public:
    baseClass(int x)
    {
    i=x;
    cout << "Base class Constructor and i = "<< i <}

    };
    class derivedClass: public baseClass {
    int j;
    public:
    derivedClass(int x, int y): baseClass(y)
    {
    j=x;
    cout << "Derive class Constructor and j = " << j;
    }
    };
    int main()
    {
    derivedClass ob(5,10);
    return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)