C++ calling base class constructors

后端 未结 6 1900
无人及你
无人及你 2020-12-08 00:23
#include 
#include  
using namespace std;

// Base class
class Shape 
{
   public:
      void setWidth(int w)
      {
         width =         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 01:07

    Imagine it like this: When your sub-class inherits properties from a super-class, they don't magically appear. You still have to construct the object. So, you call the base constructor. Imagine if you class inherits a variable, which your super-class constructor initializes to an important value. If we didn't do this, your code could fail because the variable wasn't initialized.

提交回复
热议问题