Avatar of Rothbard
RothbardFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Function overloading and inheritance in C++

Suppose I have classes Bulldog and Labrador which publically derive from base class Dog, and I have a function, e.g. a method of a different class, called reward, and I want it to produce different results for objects of class Dog and Bulldog, or for objects of class Bulldog and Labrador. What is the best way of doing this for a C++ developer?

I understand that if Dog has any virtual functions, then I can use dynamic_cast for this purpose. Is that something you would recommend? If not, what do you think would be the best approach?

Thanks.
C++

Avatar of undefined
Last Comment
Rothbard
Avatar of ste5an
ste5an
Flag of Germany image

Sounds like the strategy pattern or a factory method.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rothbard
Rothbard
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Many thanks for your your very helpful comments!
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo