polymorphism c++, trying to access member functions of classes that are derived from abstract base class

Squanchinator

so I have 5 classes,

  1. cds
  2. dvds
  3. books
  4. Item
  5. Inventory

My Item class is an abstract base class and cds,dvds, and books are derived from it. My Inventory class has an array of pointers of the Items type that point towards objects of cd,dvd, and books.

Here is the array:

Item* ptrItems[100];
int totalItems = 0`

Here is how assign an object to the array inside my Inventory class:

ptrItems[totalItems++] =  new books(1000 + totalItems, quantity, tempTitle,tempAuthor, tempDesc, cost);

//I use total items as a counter to keep track of the index

The problem that i have is that ptrItems[totalItems] can not access the member functions of the derived classes only the member functions of the Items abstract class.

For example:

ptrItems[i]->getQuantity() //works fine because getQuanty is a member function of abstract base class Item.

however,

ptrItems[i]->getBookTile() // won't work
error C2039: 'getBookTile' : is not a member of 'Item'  

Correct me if I'm wrong but isn't this polymorphism? It should have access to the derived classes public member functions, right?

n. 'pronouns' m.

No, what you are trying to do is not polymorphism. It is actually the exact opposite of polymorphism.

Polymorphism is using only your base class interface to manipulate any of your derived classes, including those that are not written yet.

For example, if you want your items to have titles, you define a virtual (possibly pure) method getTitle() in your base class, and implement it in your derived classes. This way you can get a title of any item in your collection without knowing or caring about its exact type.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access another Base Classes member from Abstract Base Class

From Dev

How to use polymorphism to access derived class vector member from base class?

From Dev

How to use polymorphism to access derived class vector member from base class?

From Dev

Accessing base member functions in class derived from template class

From Dev

Call a function from base class that uses the member functions of derived class

From Dev

Access member of derived class from pointer of base class

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well

From Dev

Extending polymorphism in derived class from base class

From Dev

Extending polymorphism in derived class from base class

From Dev

C++: Generic base member functions with specific types in derived class

From Dev

How to access member in derived classes by a pointer of type base but pointing to the derived class

From Dev

c# runtime polymorphism with abstract base class

From Dev

New derived class from abstract classes

From Dev

C++: cannot access protected member from derived class

From Dev

Why I must declare field as "public" in abstract class to access it from derived classes?

From Dev

Member functions of a derived class

From Dev

Polymorphism: accessing derived class elements from a base class array

From Dev

Trying to make derived class, compiler expects default constructor for abstract base

From Dev

Why do abstract derived classes need to initialize a virtual base class?

From Dev

Why do abstract derived classes need to initialize a virtual base class?

From Dev

Why "Do not access a static member that is defined in a base class from a derived class."

From Dev

Access to atributes from derived class in the base class

From Dev

Access base class object from derived class

From Dev

Casting from abstract base class pointer to derived class

From Dev

Invoke unique functions of derived classes from a vector of pointers to base class objects

From Dev

How to access the address of base class member in inherited class member functions?

From Dev

how to access derived class object from abstract class in boost python

From Dev

How can I retrieve the derived class from a list of abstract classes?

Related Related

  1. 1

    Access another Base Classes member from Abstract Base Class

  2. 2

    How to use polymorphism to access derived class vector member from base class?

  3. 3

    How to use polymorphism to access derived class vector member from base class?

  4. 4

    Accessing base member functions in class derived from template class

  5. 5

    Call a function from base class that uses the member functions of derived class

  6. 6

    Access member of derived class from pointer of base class

  7. 7

    C++ public method inherited from base class can not access private member variable in derived class

  8. 8

    C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well

  9. 9

    Extending polymorphism in derived class from base class

  10. 10

    Extending polymorphism in derived class from base class

  11. 11

    C++: Generic base member functions with specific types in derived class

  12. 12

    How to access member in derived classes by a pointer of type base but pointing to the derived class

  13. 13

    c# runtime polymorphism with abstract base class

  14. 14

    New derived class from abstract classes

  15. 15

    C++: cannot access protected member from derived class

  16. 16

    Why I must declare field as "public" in abstract class to access it from derived classes?

  17. 17

    Member functions of a derived class

  18. 18

    Polymorphism: accessing derived class elements from a base class array

  19. 19

    Trying to make derived class, compiler expects default constructor for abstract base

  20. 20

    Why do abstract derived classes need to initialize a virtual base class?

  21. 21

    Why do abstract derived classes need to initialize a virtual base class?

  22. 22

    Why "Do not access a static member that is defined in a base class from a derived class."

  23. 23

    Access to atributes from derived class in the base class

  24. 24

    Access base class object from derived class

  25. 25

    Casting from abstract base class pointer to derived class

  26. 26

    Invoke unique functions of derived classes from a vector of pointers to base class objects

  27. 27

    How to access the address of base class member in inherited class member functions?

  28. 28

    how to access derived class object from abstract class in boost python

  29. 29

    How can I retrieve the derived class from a list of abstract classes?

HotTag

Archive