Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;对基类的子类访问 我现在正在学习C++,我有个问题。 在继承的概念中,我理解子类可以访问基类的数据和方法。 但是如果基类实现了一个接口,子类能否访问或使用在基类中实现的接口中定义的方法?_C++_Inheritance_Interface - Fatal编程技术网

c++;对基类的子类访问 我现在正在学习C++,我有个问题。 在继承的概念中,我理解子类可以访问基类的数据和方法。 但是如果基类实现了一个接口,子类能否访问或使用在基类中实现的接口中定义的方法?

c++;对基类的子类访问 我现在正在学习C++,我有个问题。 在继承的概念中,我理解子类可以访问基类的数据和方法。 但是如果基类实现了一个接口,子类能否访问或使用在基类中实现的接口中定义的方法?,c++,inheritance,interface,C++,Inheritance,Interface,是的,您可以这样做。假设您有一个基类,如下所示 class Base { public: virtual void someMethod() { //Do your stuff } }; class Derived : public Base { public: void someMethod() override; }; void Derived::someMethod() { // Do Derived Stuffs //Now you may call

是的,您可以这样做。假设您有一个基类,如下所示

class Base {
public:
   virtual void someMethod() {
   //Do your stuff
   }
};
class Derived : public Base {
public:
   void someMethod() override;
};
void Derived::someMethod() {
  // Do Derived Stuffs
  //Now you may call the Base::someMethod by following
  Base::someMethod();
}
和一个派生类,如下所示

class Base {
public:
   virtual void someMethod() {
   //Do your stuff
   }
};
class Derived : public Base {
public:
   void someMethod() override;
};
void Derived::someMethod() {
  // Do Derived Stuffs
  //Now you may call the Base::someMethod by following
  Base::someMethod();
}
派生类中
somethod
的定义如下

class Base {
public:
   virtual void someMethod() {
   //Do your stuff
   }
};
class Derived : public Base {
public:
   void someMethod() override;
};
void Derived::someMethod() {
  // Do Derived Stuffs
  //Now you may call the Base::someMethod by following
  Base::someMethod();
}

您可以这样尝试。

C++不是Java。C++中没有这样的接口,它与类一样,可以有虚拟类,只具有纯虚函数。这在某种程度上类似于java中的接口实现。子类不能自动访问基类数据和方法。但是,如果子类可以访问基类方法,那么该方法是否是接口实现的一部分根本没有区别。因此,尽管你的问题的前提是错误的,我认为答案是肯定的。如果你使用Java,你的解释是完全正确的。接口的概念只是类的规范。