C++ Abstract Class: constructor yes or no?

后端 未结 8 1852
轮回少年
轮回少年 2020-12-04 08:45

A class with one (or more) virtual pure functions is abstract, and it can\'t be used to create a new object, so it doesn\'t have a constructor.

I\'m reading a book t

相关标签:
8条回答
  • 2020-12-04 09:10

    To initialize firstName and lastName. Otherwise you will have to write a code to initilze them in each derived classes' constructors

    0 讨论(0)
  • 2020-12-04 09:13

    The purpose of Abstract class is that you want to extend some functionality by derived classes. Can it have constructor? Yes it can and the purpose is to initialize local variables from the base class. You should avoid using public constructor in Abstract and use protected only.

    The example you have is not a good example. Not sure what book it is but that the bad example. Its like defining int variable with name of variable "iAmString" :).

    int iAmString = 12;

    Cheers

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