C ++ basic polymorphism

class Product { ... } class Perishable : public : Product { public: int getday(); } int main() { Product *temp; //due to some coding //temp could point to either Perishable object or Product object that is determine //during runtime cout<< ((Perishable*)temp)->getday() ;// is there other way to achieve this typecasting seems dangerous 

The problem with this code is that if temp points to a Product object, temp-> getday () will be invalid, and I don't know how to prevent this. If, due to some circumstances, I am allowed to have getday () in a perishable but not in a product, how can I check if temp points to a perishable object or a Product object?

Some help would be appreciated /

}

+4
source share
7 answers

"The problem with this code is that if temp points to a Product object, temp-> getday () will be invalid and I don't know how to prevent this."

In the spirit of the question, and if you absolutely do not want to declare / implement getday () in your Product class, as indicated in other answers, you can use dynamic casting to determine the type of runtime of your variable, and only getday () is called if you have There is an instance of Perishable:

  Product* pPerishable = new Perishable; Product* pProduct = new Product; Perishable * pActualPerishable; pActualPerishable= dynamic_cast<Perishable *>(pPerishable ); //pActualPerishable will not be null because it is of type Perishable at run time pActualPerishable = dynamic_cast<Perishable*>(pProduct ); //pActualPerishable will be null because you are trying to cast a runtime base type to a derived type. 

So, try dynamically translating your variable to perishable, and if successful you know you can call getday (). Note that this is no longer polymorphic, but runtime type determination has its uses, especially if you have no control over the interface of the objects you are working on.

+3
source

I think you need the following:

 class Product { public: virtual int getday() = 0; } class Perishable : public : Product { public: virtual int getday(); } 

With this change, you can do this:

 cout << temp->getday(); 
+2
source

If getDay is a virtual function in the Product class, you do not need to throw. You could just write this:

 cout<< temp->getday(); 

If temp points to an object of type Product , then Product:getDay will be called. If temp points to an object of type Perishable , then Perishable::getDay will be called if it is overridden in Perishable , otherwise Product::getDay will be called.

This is how runtime polymorphism works.

0
source

What does this have to do with polymorphism? I assume getDay () is also defined in Product? If so, then this is the whole goal of inheritance and polymorphism. You have to call

TEMP-> getday (); without worrying about the cast. As long as temp is really a Product or its derivatives, and getDate () is defined as virtual in the Product, there is no need for any casting.

Example:

 class Product { public: virtual int getday(); }; class Perishable: public Product { public: virtual int getday(); }; int main() { Product *temp; //= you should be getting the new from some factory method somewhere. //Polymorphism will handle making sure that the right function is called here. cout<< temp->getday(); } 
0
source

There are several ways, for example:

 class Product { int getday() { return ... ; } // -1 for example or any invalid value } class Perishable : public : Product { public: int getday(); } 

or

 class Product { virtual int getday() = 0; } class Perishable : public : Product { public: virtual int getday(); // You must implement this somewhere } 
0
source

Polymorphism requires a virtual base method, and then override the method in subclasses, for example:

 class Product { virtual int getday(); // You could also make this pure abstract } class Perishable : public Product { public: int getday(); } class NonPerishable : public Product { public: int getday(); } int main() { Product *temp; temp = new Perishable(); cout << temp->getday(); // Perishable::getday() temp = new NonPerishable(); cout << temp->getday(); // NonPerishable::getday() } 
0
source
 class Product { ... } class Perishable : public : Product { public: int getday(); } int main() { Product *temp; //due to some coding //temp could point to either Perishable object or Product object that is determine //during runtime Product *pObj = dynamic_cast<Perishable *>(temp); if(!temp) { temp->getday(); //temp is containing Perishable Object. } else { //it is Product Obj; } } 

this concept is known as RTTI. To find the type name of the temp object, you can also use typeid (temp) :: name (), but this will only return the type of the object pointed to by temp .. it will not do typecasting for you use dynamic_cast this way.

0
source

Source: https://habr.com/ru/post/1397366/