C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - General Questions (Q.No. 33)
33.
Which of the following statement is correct?
A constructor of a derived class can access any public and protected member of the base class.
Constructor cannot be inherited but the derived class can call them.
A constructor of a derived class cannot access any public and protected member of the base class.
Both A and B.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
4 comments Page 1 of 1.

Sujal sunil dhani said:   1 month ago
Very hard to understand. Thanks all.

Nikhil banerjee said:   8 years ago
Thanks for explaining it.

Daniel Sandor said:   10 years ago
It depends on the standard. The C++03 standard says constructors cannot be inherited, while C++11 standard says they can:

It is valid in C++11 but not in C++03:

struct As
{
As(int){ }
};

struct Bs : As
{
};
Bs b(2);

It is valid in both:

struct As
{
As(int){ }
};

struct Bs : As
{
Bs(int) : As(int) { }
};
Bs b(2);

Bhavna said:   1 decade ago
Constructor can be inherited:

#include<iostream.h>
#include<conio.h>
class base
{
int a;
public:
base(int a1)
{
a=a1;
cout<<"base's single parameter constructor"<<endl;
}
void show()
{
cout<<" \n a="<<a<<endl;
}
};
class derv: public base
{
int b;
public:
derv( int bb, int aa):base(aa)
{
b=bb;
cout<<" \n Derived's two parameter constructor";
}
void show()
{
base:: show();
cout<<"b="<<b<<endl;
}
};
void main()
{
clrscr();
derv d(10,20);
d.show();
d.base::show();
getch();
}

Post your comments here:

Your comments will be displayed after verification.