Results 1 to 3 of 3
Thread: Reg: C++ abstract classes
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- May 2009
Reg: C++ abstract classes
But I don;'t know-
- why I have to create a AbstractBaseClass.
- In what situations it can be used.
Can you please anyone help me with an example code base and not like a animal(base) and tiger(derived) relationship example.
Thanks in advance,
We don't normally allow homework questions here, but there may be more to what you're asking than just getting your homework done, so here's some information that might help you.
Abstract base classes are useful mostly to define an interface or to define the way you want the concrete instances of different classes to communicate with other entities within your code model.
You don't have to use abstract classes at all, but it can help you keep interfaces separate to your normal code. Normally multiple inheritance in C++ is a Very Bad Thing, but it's not uncommon for a class to inherit from another class and one or more abstract classes that represent interfaces that the class will present.
I don't think it's appropriate to provide code examples - I think if you create this yourself, you'll understand better what you're doing. If you want to post it here for comment, of course, there may be some that would help.Linux user #126863 - see http://linuxcounter.net/
As Roxoff said, we don't allow homework questions. I am closing this thread.
I will give you a bit of information, though. An abstract class allows you to declare that a set of things has a similar behavior, but the details are unknown. To take your example, you have an Animal abstract class that may have an abstract method called "makeNoise". Each Animal now must provide its own implementation: Tiger might return the string "roar", Dog might return "Woof", and Cat would return "Meow". When you have an Animal object, you don't know what type of Animal it is, but you know that it can make a noise.
If you have a more conceptual question that we can help with, please feel free to start a new thread and ask.