Reputation: 47
I am beginner,and i am facing problem when trying to copy one pointer to object in array of pointers..
class Publication
{
char title[20];
int idd;
public:
char* getT()
{
return title;
}
virtual void getData()
{
cout<<"Enter Title of Book";
cin>>title;
cout<<"Enter ID of Book";
cin>>idd;
}
virtual void putData()
{
cout<<"Title of Book : "<<title<<"\n";
cout<<"ID of Book : "<<idd;
}
};
class Book:private Publication
{
int pages;
public:
void getData()
{
cout<<"Enter Title of Book";
cin>>getT();
cout<<"Enter Pages ";
cin>>pages;
}
void putData()
{
cout<<"Title of Book : "<<getT()<<"\n";
cout<<"No of Pages : "<<pages;
}
};
Main Function
int main()
{
Publication *ptrpub[2];
Book *ptr;
for(int i=0;i<2;i++)
{
cout<<"Enter Data for Book Object";
ptr=new Book;
ptr->getData();
Now within this loop, i want to move that pointer in an array of pointers to publication i.e ptrpub[],where Book class inherits from Publication Privately
ptrpub[i]=ptr; //'Publication' is an inaccessible base of 'Book'
Is there any way to copy them,so i may use ptrpub[i]->putdata() to show all data entered for book object.. And I must keep that relationship between Book and Publication class.
Upvotes: 0
Views: 101
Reputation: 168988
The problem is that you inherit Book
from Publication
privately:
class Book:private Publication
This means that only Book
's implementation is aware that it inherits from Publication
-- code external to the class does not know this, and so you cannot cast Book
to Publication
except from within members of Book
(or friend members).
You can resolve this by inheriting publicly:
class Book:public Publication
Upvotes: 1