Hai the following program is executed some errors are display. Kindly solve this
#include<iostream.h>
Class A
{
private:
int a1;
public:
int a2;
protected:
int a3;
};
class B:public A
{
public:
void func( )
{
int b1,b2,b3;
b1=a1;
b2=a2;
b3=a3;
}
};
void main( )
{
B der;
der.a3=0;
der.fun( );
}
}
#include<iostream.h>
Class A
{
private:
int a1;
public:
int a2;
protected:
int a3;
};
class B:public A
{
public:
void func( )
{
int b1,b2,b3;
b1=a1;
b2=a2;
b3=a3;
}
};
void main( )
{
B der;
der.a3=0;
der.fun( );
}
}
Now, you have a problem. Your class B is inheriting only the public and protected members from your class A. (You cannot inherit the private member).
Thus, you cannot call the void func() (which is in class B), as it tries to read the variable a1, which is private to class A.
@vlad from moscow
That's what I said. The class B inherits class A. By inheriting class A (base class) in class B (derived class), you also inherit all it's functions/variables which are under the public or protected members.
@jumper007
@vlad from moscow
That's what I said. The class B inherits class A. By inheriting class A (base class) in class B (derived class), you also inherit all it's functions/variables which are under the public or protected members.
One more: all members are inherited irrespective of the access control.
The word all includes all public, protected and private members.
I really don't understand what you mean. Apart the one error I mentioned, the program would have the following effect: b2 = a2 and b3 = a3. That's because the inheritance is done correctly, and that gives the derived class the access to both the public and protected members of the base class, giving it the possibility to use the data.
PS: You have repeated the words "all members are inherited irrespective of the access control" more than 2 times. If you are sure that you are right, please explain that in a proper way. Either way, this should be enough: