如何让头文件知道类继承
How to let header file know about class inheritance
>我目前在继承方面遇到了这个问题:
A.hpp
{
class Example;
}
A.cpp
{
class Example : public Base {
//OVERRIDE FUNCTIONS OF CLASS HERE
}
}
B.hpp
{
class DerivedExample;
}
B.cpp
{
class DerivedExample : public Example {
//How to override the functions of class Base here?
}
}
我有一个接收Base
参数的方法:
void doSomething(Base* base) = 0;
问题是,这种doSomething
方法只接受Example
但不接受DerivedExample
。我该怎么做才能让A
的头文件知道类Example
是类Base
的派生类,而无需将所有方法定义移动到那里?对不起,如果听起来模棱两可,我对C++很陌生。谢谢。
在各自的HPP文件中定义类,当你编写函数的主体时,你必须将它们定义为 派生示例::d oSomething() <- 如果这是一个函数, 并且您必须在.cpp文件中包含相应的HPP文件 如果我正确理解了您的 QS。那么他们不会有任何歧义
我正在附加我的示例添加多个文件程序代码。 类似地定义继承类,后跟所谓的函数。
header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
class Addition
{
public :
int sum(int a ,int b);
};
#endif
function.cpp
#include"header.hpp"
int Addition::sum(int a,int b)
{
//int a,b,result;
//result=a+b;
return a+b;
}
main.cpp
#include<iostream>
using namespace std;
#include"header.hpp"
int main()
{
int a,b,result;
Addition add;
cout<<"enter the first number ";
cin>>a;
cout<<"enter the second number";
cin>>b;
result=add.sum(a,b);
cout<<"the sum of the two numbers is "<<result;
return 0;
}
相关文章:
- 头文件-继承c++
- 如何从另一个文件继承私有成员变量和公共函数
- C++两次从文件保存对象读取多重继承
- 在 c++ 中,当一个类继承头文件中的另一个类时出错
- 如何对继承的文件使用前向声明
- C++错误LNK2005和不同项目文件的常规继承
- C 从多个继承类写入二进制文件
- "Attribute is protected within this context"继承和 .h 和.cpp文件
- 在继承类中使用同一文件时,C 链接器错误
- C++继承 - 覆盖功能,包括使用 "::" s、.h 文件和.cpp文件
- 为什么多个头文件和源文件中的C++继承不起作用?
- 从同一文件构建基类和继承类
- 如何让头文件知道类继承
- python永远不能调用包含继承逻辑的c++dll或左右文件
- Pimpl习惯用法、单独的接口/实现文件和多个虚拟继承.如何
- 将继承的类存储到文件的更好方法
- 无法读取继承进程中的文件
- 类继承在多个文件c++中
- 使用#include和Inclusion Guards的独立文件中的C++继承
- C++继承-文件