Abstract Base Class discription.

This is a discussion on Abstract Base Class discription. within the C++ Programming forums, part of the General Programming Boards category; I just created an abstract base class called button that I will use as an interface button in a dirctdraw/network ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Abstract Base Class discription.

    I just created an abstract base class called button that I will use as an interface button in a dirctdraw/network game I am creating.
    Take a look and tell me if it looks ok.
    I questioned one thing, can you make the destructor the pure virtual function?
    Code:
    #ifndef _BUTTON_H_
    #define _BUTTON_H_
    
    #include <ddraw.h>
    #include <windows.h>
    #include <windowsx.h>
    
    class Button
    {
    public:
    	Button();
    	virtual IDirectDrawSurface * hover(IDirectDrawSurface * pddshovered,int width,int height); 
    	virtual sfns * pressed(IDirectDrawSurface * pddspressed, int width, int height);
    	virtual bool released(sfns * pscleansfns);
    	virtual IDirectDrawSurface * display(IDirectDrawSurface * pddsbuttonsurface, int width, int height);
    	virtual IDirectDrawSurface * erase(IDirectDrawSurface * pddsbackbufferclearsurface,int width, int height);  
    	virtual ~Button(void) = 0;
    
    private:
    	STRUCT
    	{
    		IDirectSurface * pdds;
    		int surfacewidth;
    		int surfaceheight;
    		int result;
    	} sfns;
    };//endclass
    
    #endif;
    Last edited by curlious; 11-07-2003 at 07:08 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can you make the destructor the pure virtual function?
    Yes.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I should have said a pure virtual function, but I do not think I will make the other functions pure virtual because the abstract base class will have an implementation of these functions even though it can not be instantiated. Another question if I overide these function is my derived class can I call the functionality of the base classes version from a base class pointer then call the derived class version from the same pointer. Guess I need a little review on this so I'll have to pull out my book. I think I will need a derived class pointer to implement either the base class version of the virtual function or the derived class version of the virtual function, but then again maybe you can only use the derived class version.
    Last edited by curlious; 11-07-2003 at 10:39 AM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Another question if I overide these function is my derived class can I call the functionality of the base classes version from a base class pointer then call the derived class version from the same pointer.
    I don't have the ability to test this right now, but I think something like this will work:

    Code:
    #include <iostream>
    
    class Base
    {
    public:
      virtual const char* func() { return "base"; } 
    };
    
    class Derived: public Base
    {
    public:
      virtual const char* func() { return "derived"; }
    };
    
    
    int main()
    {
      Base* p = new Derived;
      std::cout << p->func() << std::endl; // should output "derived"
      std::cout << p->Base::func() << std::endl; // should output "base"
      delete p;
    }

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Ok I'll give that a shot when it comes to the implementation of my derived button class. For now I have to write the function definitions for my base class, then my first concrete button class may be a few days to get that done. Then I will call the functions from routine that handles my user interface.
    Just thought a more advanced idea would be to create the abstract base class as say userinterface (similar to component in java idea) then create an entire little gui. Thats overkill at my level though, I'm just trying to reinforce my knowledge of direct draw and direct play.

    I'm finding I am refreshing my knowledge of C++ in general though. (Inheritance, Pointers to functions, passing by value, refrence etc.)

  6. #6
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Originally posted by Prelude
    >can you make the destructor the pure virtual function?
    Yes.
    But you still have to provide a function body for the destructor. This is useful to make the class abstract when all the other functions in the class will have "default" code.

    If you need to call the base class function for an overridden function, you can also call it directly from the overriding definition:
    Code:
    #include <iostream>
    using namespace std;
    
    class Base {
      public:
        virtual void func() { cout << "Base::func()"; }
        virtual ~Base() = 0;
    };
    
    // without the definition, the class will not compile
    Base::~Base() { cout << "~Base" << endl; }
    
    class Derived : public Base {
      public:
        void func() {
          Base::func();
          cout << " called from Derived::func()." << endl;
        }
        ~Derived() { cout << "~Derived" << endl; }
    };
    
    int main() {
     // Base b;           // Will cause a compile error since Base is abstract
      Base* pb = new Derived;
      pb->func();       // "Base::func() called from Derived::func()"
      pb->Base::func(); // "Base::func()"
      cout << endl;
      delete pb;        // "~Derived" followed by "~Base"
    }
    Yes, thefroggy, your code works, I just wanted to add the Virtual Destructor in to completely answer the first question as well.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    So you provide a definition for the destructor even though you specify it as pure virtual? I thought this was only true if it is just virtual. Pure virtual == no implementation in the base class.

    Also should I neglect the constructor for the abstract base class, seeing how it can not be instantiated.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by curlious
    So you provide a definition for the destructor even though you specify it as pure virtual? I thought this was only true if it is just virtual. Pure virtual == no implementation in the base class.

    Also should I neglect the constructor for the abstract base class, seeing how it can not be instantiated.
    No, a pure virtual destructor *MUST* be implemented, even if the implementation is empty. This is because every derived class always calls its parent's destructor.

    You can implement pure virtual functions. One case might be where you want to provide a default behavior, but only if the creator of the derived class specifically wants to. This would be a case where usually, the default is inadequate, but sometimes you want to use it. The derived class can still call it, but they need to call it explicitly.

    ABCs *do* have their constructors called, as the first step of constructing their children, so yes, you can and should use them when appropriate. The ABC's constructor should handle instantiation of any variables of the ABC.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks that about clears up all my questions. I did review my book, but you guys answered my lingering questions tonight I will start on the hard part the definitions and my first concrete button.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  2. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Abstract Base Class and References
    By Thantos in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 01:35 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21