Why do we need abstract classes in C++?

前端 未结 10 2070
暗喜
暗喜 2020-12-14 03:19

I\'ve just learned about polymorphism in my OOP Class and I\'m having a hard time understanding how abstract base classes are useful.

What is the purpose of an abstr

相关标签:
10条回答
  • 2020-12-14 03:34

    Having an abstract class like "Dog" with a virtual method like "bark" allows all classes that inherit from Dog to have their bark code called in the same way, even though the Beagle's bark is implemented way differently than the Collie's.

    Without a common abstract parent (or at least a common parent with a bark virtual method) it'd be difficult to do the following:

    Have a Vector of type Dog that contains Collies, Beagles, German Shepherds etc and make each of them bark. With a Vector of Dogs that contains Collies, Beagles, German Shepherds all you would have to do to make them all bark is to iterate through in a for loop and call bark on each one. Otherwise you'd have to have a separate Vector of Collies, Vector of Beagles etc.

    If the question is "why make Dog abstract when it could be concrete, have a virtual bark defined with a default implementation that can be overriden?", the answer would be that this may be acceptable sometimes -- but, from a design perspective, there really isn't any such thing as a Dog that isn't a Collie or a Beagle or some other breed or mix so although they are all Dogs, there is not one of them in reality that is a Dog but not some other derived class too. Also, since dogs barking is so varied from one breed to another, there is unlikely to be any real acceptable default implementation of bark that would be acceptable for any decent group of Dogs.

    I hope this helps you understand the purpose: yes, you're going to have to implement bark in each subclass anyway, but the common abstract ancestor lets you treat any subclass as a member of a base class and invoke behaviors that may be conceptually similar like bark but in fact have very different implementations.

    0 讨论(0)
  • 2020-12-14 03:36

    I have a dog. Abstract class dog with a method bark. My particular dog makes one bark. Other dogs bark in a different way. So defining a dog in the abstract way is useful.

    0 讨论(0)
  • 2020-12-14 03:40

    An abstract class AbstractClass as a base class is needed when there is functionality that is desired for all objects that have a type deriving from AbstractClass, but cannot sensibly be implemented on the AbstractClass itself.

    The old and somewhat artificial OO example of having a base class Vehicle with derived classes Car, Motorcycle, ... provides a good example here, say you want a method move() - you can implement the way that a Car or a Motorcycle moves, but Vehicles don't move in a generic way, so Vehicle::move() will have to be pure virtual and Vehicle therefore abstract.

    0 讨论(0)
  • 2020-12-14 03:41

    Abstract classes are used to define an interface to be implemented. See some references:

    http://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes

    0 讨论(0)
  • 2020-12-14 03:41
    abstract class dog
    {
    bark();
    }
    
    // function inside another module
    
    dogbarking(dog obj)
    {
       dog.bark(); // function will call depend up on address inside the obj
    }
    
    
    // our class
    ourclass: inherit dog
    {
        bark()
        {
             //body
         }
    }
    
    
    main()
    {
        ourclass obj;
        dogbarking(obj);
    }
    

    we can see that dogbarking is a function written in another module. it knows only the abstract class dog. even though it can call the function bark inside ourclass. in main function we create object of ourclass and pass to function dogbarking where it received using reference object of abstract class dog.

    0 讨论(0)
  • 2020-12-14 03:45

    The purpose of an abstract class is to define a common protocol for a set of concrete subclasses. This is useful when defining objects that share code, abstract ideas, etc.

    Abstract classes have no instances. An abstract class must have at least one deferred method (or function). To accomplish this in C++, a pure virtual member function is declared but not defined in the abstract class:

    class MyClass {
        virtual void pureVirtualFunction() = 0;
    }
    

    Attempts to instantiate an abstract class will always result in a compiler error.

    "What does defining an abstract base class provide that isn't provided by creating each necessary function in each actual class?"

    The main idea here is code reuse and proper partitioning across classes. It makes more sense to define a function once in a parent class rather than defining over and over again in multiple subclasses:

    class A {
       void func1();
       virtual void func2() = 0;
    }
    
    class B : public A {
       // inherits A's func1()
       virtual void func2();   // Function defined in implementation file
    }
    
    class C : public A {
       // inherits A's func1()
       virtual void func2();   // Function defined in implementation file
    }
    
    0 讨论(0)
提交回复
热议问题