Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can functions accept abstract base classes as arguments?

Tags:

Having gotten comfortable with the idea of basic classes and encapsulation, I've launched myself towards understanding polymorphism, but I can't quite figure out how to make it work. Many of the examples I've searched through come across as really, really forced (classes Foo and Bar are just too abstract for me to see the utility), but here's how I understand the basic concept: you write a base class, derive a whole bunch of other things from it that change what the base methods do (but not what they "are"), then you can write general functions to accept and process any of the derived classes because you've somewhat standardized their appearance. With that premise, I've tried to implement the basic Animal->cat/dog hierarchy like so:

class Animal {   public:     virtual void speak() = 0; };  class Dog : public Animal {   public:     void speak() {cout << "Bark bark!" << endl;} };  class Cat : public Animal {   public:     void speak() {cout << "Meow!" << endl;} };  void speakTo(Animal animal) {     animal.speak(); } 

where speakTo can take can take a general kind of animal and make it, well, speak. But as I understand it, this doesn't work because I can't instantiate Animal (specifically in the function argument). I ask, then, do I understand the basic utility of polymorphism, and how can I really do what I've tried to do?

like image 806
Connor Glosser Avatar asked Feb 17 '12 02:02

Connor Glosser


People also ask

Can abstract class be used as a parameter type?

An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion. Pointers and references to an abstract class can be declared.

Can abstract classes have functions?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.

Can you inherit an abstract base class?

NO. Abstract methods(defintion) are overridden by base class' overriding methods.

What is special about abstract base classes?

Abstract base classes separate the interface from the implementation. They make sure that derived classes implement methods and properties dictated in the abstract base class. Abstract base classes separate the interface from the implementation.


2 Answers

You cannot pass an Animal object to the derived class function because you cannot create an object of Animal class et all, it is an Abstract class.
If an class contains atleast one pure virtual function(speak()) then the class becomes an Abstract class and you cannot create any objects of it. However, You can create pointers or references and pass them to it. You can pass an Animal pointer or reference to the method.

void speakTo(Animal* animal)  {     animal->speak(); }  int main() {     Animal *ptr = new Dog();     speakTo(ptr);      delete ptr;   //Don't Forget to do this whenever you use new()     return 0; } 
like image 71
Alok Save Avatar answered Oct 24 '22 04:10

Alok Save


You will need to pass a reference instead of a copy:

void speakTo(Animal& animal) {     animal.speak(); } 
like image 37
Vaughn Cato Avatar answered Oct 24 '22 05:10

Vaughn Cato