C++ 为什么这个纯虚拟方法没有编译?
我使用纯虚拟的方法,如下面的代码所示C++ 为什么这个纯虚拟方法没有编译?,c++,pure-virtual,C++,Pure Virtual,我使用纯虚拟的方法,如下面的代码所示 #include <iostream> using namespace std; class Advertisment { public: vitual void price (int Uchrg, int no_of_unt) = 0; { } }; class TVadvertisment : public Advertisment { public: void price (int Uchrg, int no_of
#include <iostream>
using namespace std;
class Advertisment
{
public:
vitual void price (int Uchrg, int no_of_unt) = 0;
{
}
};
class TVadvertisment : public Advertisment
{
public:
void price (int Uchrg, int no_of_unt)
{
int adPrice = Uchrg * no_of_unt;
cout << "Advertisment Price: " << adPrice;
}
};
int main()
{
TVadvertisment T;
T.price(1000, 60);
return 0;
}
#包括
使用名称空间std;
班级广告
{
公众:
虚拟无效价格(int Uchrg,int no of unt)=0;
{
}
};
电视广告类:公共广告
{
公众:
无效价格(整数次,整数次)
{
int adPrice=Uchrg*不含unt;
cout您的函数是纯虚拟的,这意味着该方法是虚拟的,甚至没有在基类(=0)
中实现。
因此,您必须删除它之后的块
它必须是:
virtual price(int Uchrg, int no_of_unt) = 0;
没有{}
虚拟意味着,从基类继承的类可以重写该方法并通过基类接口调用。如果类具有纯虚拟方法,则该类是抽象的,并且该函数需要由从该基类继承的类重写以进行实例化
简言之:
虚拟:
virtual price(int Uchrg, int no_of_unt)
{
// Implementation
}
virtual price(int Uchrg, int no_of_unt) = 0; // No implementation
Base* pBase = new Derived;
pBase->fun();
有一个实现是基类。子类不需要重写,但可以重写。类不是抽象的,可以实例化
纯虚拟:
virtual price(int Uchrg, int no_of_unt)
{
// Implementation
}
virtual price(int Uchrg, int no_of_unt) = 0; // No implementation
Base* pBase = new Derived;
pBase->fun();
没有实现,子类必须重写它才能不是抽象的
通过基类调用虚拟方法:
virtual price(int Uchrg, int no_of_unt)
{
// Implementation
}
virtual price(int Uchrg, int no_of_unt) = 0; // No implementation
Base* pBase = new Derived;
pBase->fun();
该方法是通过基类的接口调用的,但它将是派生类的方法。请您的问题在问题本身中提供一个。我使用纯虚拟,如下所示。下面是什么?复制粘贴问题本身中的所有相关代码。请不要链接文本图像,只需复制并粘贴相关信息即可问题:一个纯的虚拟函数在它之后没有{ },也请不要尝试猜测C++语法是如何工作的,那是行不通的。