Why do we need abstract classes in C++?

前端 未结 10 2096
暗喜
暗喜 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:47

    The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error. (because vtable entry is not filled with memory location for virtual function we mentioned in Abstract Class)

    Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.

    Example:

    class mobileinternet
    {
    public:
    virtual enableinternet()=0;//defines as virtual so that each class can overwrite
    };
    
    
    class 2gplan : public mobileinternet
    
    {
        private:
    
             int providelowspeedinternet(); //logic to give less speed.
    
        public:
    
             void enableinternet(int) {
                                         // implement logic
                                     }
    
    };
    
    //similarly
    
    class 3gplan : public enableinternet
    {
       private: high speed logic (different then both of the above)
    
       public: 
              /*    */
    }
    

    here in this example, you can understand.

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

    Imagine you have two methods for displaying a string:

    DisplayDialog(string s);
    PrintToConsole(string s);
    

    And you want to write some code that can be switched between these two methods:

    void foo(bool useDialogs) {
        if (useDialogs) {
            DisplayDialog("Hello, World!");
        } else {
            PrintToConsole("Hello, World!");
        }
    
        if (useDialogs) {
            DisplayDialog("The result of 2 * 3 is ");
        } else {
            PrintToConsole("The result of 2 * 3 is ");
        }
    
        int i = 2 * 3;
        string s = to_string(i);
    
        if (useDialogs) {
            DisplayDialog(s);
        } else {
            PrintToConsole(s);
        }        
    }
    

    This code is tightly coupled to the specific methods used for displaying the string. Adding an additional method, changing how the method is selected, etc. will affect every piece of code that uses this. This code is tightly coupled to the set of methods we use to display strings.

    Abstract base classes are a way of decoupling code that uses some functionality from the code that implements that functionality. It does this by defining a common interface to all the various ways of doing the task.

    class AbstractStringDisplayer {
    public:
        virtual display(string s) = 0;
    
        virtual ~AbstractStringDisplayer();
    };
    
    void foo(AbstractStringDisplayer *asd) {
        asd->display("Hello, World!");
        asd->display("The result of 2 * 3 is ");
    
        int i = 2 * 3;
        string s = to_string(i);
    
        asd->display(s);
    }
    
    int main() {
        AbstractStringDisplayer *asd = getStringDisplayerBasedOnUserPreferencesOrWhatever();
    
        foo(asd);
    }
    

    Using the interface defined by AbstractStringDisplayer we can create and use as many new ways of displaying strings as we want, and code that uses the abstract interface won't need to be changed.

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

    Abstract classes allow for compile time protocol enforcement. These protocols define what it means to be a part of a class family.

    Another way to think of it is that a abstract class is a contract that your implementing classes must fulfill. If they do not fulfill this contract they cannot be part of the class family and they must be modified to conform to the contract. The provided contract may provide default functionality, but it also leaves it up to the sub-class to define more specific or different functionality while still remaining within the scope of the contract.

    For small projects this may not seem useful but for large projects it provides conformity and structure as it provides documentation through the abstract class contract. This makes for more maintainable code and makes for the sub-classes to each have the same protocol making using and developing new sub-classes easier.

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

    why don't we create each necessary function in each class ? (C++)

    You have to create each necessary function marked as abstract in each derived class.

    If you question is, why to create abstract function in abstract class?

    It allows strict run time polymorphism.

    Also read Interface vs Abstract Class (general OO)

    0 讨论(0)
提交回复
热议问题