Hi I'm trying to an array of object of child class in parent class using pointer and trying to initialise the child class variable using functions defined in child class. But it is throwing error that the child object created is not in scope. The code is breaking. Please help me how to do this.
#include<iostream>
using namespace std;
class parent
{
public:
int y=0;
void display(){
cout<<"Value inside parent of y is "<<y;
}
};
class child: public parent
{
public:
int nop;
parent *ptr;
void input()
{
int x,nop=0;
parent p;
*(ptr+nop) = p;
cout<<"Enter number";
cin>>x;
nop++;
p.y=x;
cout<<"Value inside child is "*((ptr+nop)).y;
p.display();
}
};
int main(){
child c;
c.input();
return 0;
}