+ Reply to Thread
Results 1 to 3 of 3

Thread: What is abstract base class?

  1. #1

    What is abstract base class?

    Hi all,

    What is abstract class?

    How can I define it? What are its usage?

    Provide some example.

  2. #2
    Abstract class is a class which object cannot be created.

    Abstract class defines several methods which are empty and there is no definition.

    Classes derived from abstract class which implements empty methods of base class can be instantiated.

    Following is the example where we cannot create object of CLogMessage class type.

    But we can create object of CDebugMessage class which is derived from CLogMessage class.

    Example:

    Code:
    class CLogMessage
    {
    public:
    	virtual void DisplayMessage(const std::string& strMessage) = 0 ;
    } ;
    
    class CDebugMessage : public CLogMessage
    {
    public:
    	virtual void DisplayMessage(const std::string& strMessage)
    	{
    		OutputDebugString(strMessage.c_str()) ;
    	}
    } ;
    
    
    void UseLogMessage()
    {
    	//CLogMessage objLogMessage ; //error C2259: 'CLogMessage' : cannot instantiate abstract class due to following members:
    
    	CDebugMessage objDebugMessage ;
    	objDebugMessage.DisplayMessage("I am using CDebugMessage class object") ;
    }

  3. #3
    Wow, great example.

    Thank you.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts