Thursday, July 9, 2009

Error in Private Inheritance in C++..PLEASE tell me reason..?

See following class


class A


{


public://Member


int a1;





//Constructor


A()


{ cout%26lt;%26lt;"Inside Constructor of class A\n"; }





//Member function of class A


void f1()


{ cout%26lt;%26lt;"Inside f1() of class A\n"; }


};


class B:private A


{


public:


B()


{ cout%26lt;%26lt;"Inside Constructor of class B\n"; }


};


void main()


{


A *a1=new B();//GIVES compile time error...i.e.[can not convert 'B *' to 'A *']


//WHY?????????


}

Error in Private Inheritance in C++..PLEASE tell me reason..?
"A" is an inaccessible base of "B", i.e. while constructing "B" instance, it is impossible to access private "A" constructor. Change inheritance to "protected"!
Reply:Correct. The error is in the inheritence part. Look you are inherting class A into B as private. That's the problem. If you change it as public. It would go smooth...


No comments:

Post a Comment