Confused !! :: Abstract Vs Interface Class
AI Generated image

Confused !! :: Abstract Vs Interface Class

Lets make the difference very simple. It could create panic if purpose is not understood well.

As such there is no keyword in C++ unlike java that defines any class of this type.

So what exactly are these and how to differentiate and make use of them?

Purpose define both the class, from implementation front surely both classes needs to have pure virtual function in it.

Below example shows simple class having the pure virtual function. Lets now make it abstract or Interface.

class Cpp {
public :
virtual Bla()=0;
};        

Major difference between both the class is the way they would be used.

If there is need to have the implementation you need to create the Interface class.

If is to expand the class then abstract class come handy.

Interface class :

Interface class are made for the implementation, as such one inheriting it has to write the implementation of all the functions that is defined in the Interface class.

By theory you also do not need to have any member variables and methods definition as purpose is solely to implement it by some one.

class Interface {
public:
virtual void bla (void) = 0;
virtual void bla2 (void) = 0;
virtual void bla3 (void) = 0;
};        

Abstract Class

When we need to expand the class we create the abstract class. This class is allowed to have member and methods definition as well. Surely there needs to have the pure virtual function making its inheritance mandatory.

class Abstract {
public :
int count;
public :
int getCount(void) { return count; }
virtual int pureVirtualFunction (void) =0;
};        

要查看或添加评论,请登录

RAJ PRAJAPATI的更多文章

社区洞察

其他会员也浏览了