
I have a program as bellow:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
A()
{
x=10;
}
};
class B:public A
{
protected:
int y;
public:
B()
{
y=30;
}
};
void main()
{
A obj1;
B obj2;
obj1=obj2;
cout<<obj1.x;
cout<<obj1.y;
getch();
}
The error message is "y is not member of A"
Can any one solve this problem?

There is nothing to solve here. The error message displays correct that "y is not member of A".
When you assign a derived class's instance into the base class only that part, which are present in the base class are copied, rest are not copied. this is called Object Sliced. because here obj2 get sliced and stored in obj1.
The main reson for this is, it is not possible (or meaningful) in any statically typed language for a superclass instance to contain subclass member variables.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.