Why do we need abstract classes in C++?

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

提交回复
热议问题