Jump to content


C++ abstract class help


6 replies to this topic

#1 dicksan

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 01 November 2005 - 08:33 PM

Hey

I have an abstract base class containing pure virtual functions and I want to implement these functions in 2 derived classes using different API's for each of them. e.g. one in OGl and one in DX.

Do declare 2 classes exactly the same as the abstract base class first, without the functions being PV? Or can i just implement them straight away and include the base class header in each cpp file. Or something else?

please help, cheers

#2 corey

    Member

  • Members
  • PipPip
  • 78 posts

Posted 01 November 2005 - 08:58 PM

You need to declare and define the method the same in the derived class as the abstract base class.
class Abstract {
public:
  virtual int test() = 0;
};

class Derived: public Abstract {
public:
  virtual int test() {
    return 0;
  }
};
corey

#3 MJeannig

    Member

  • Members
  • PipPip
  • 29 posts

Posted 02 November 2005 - 09:57 AM

Don't forget to put the destructor virtual if you delete the base class object, else your object will be partially destroyed.
Another way to achieve platform independance is to have two cpp implementation and one header and to use the linker to select the implementation.

#4 dicksan

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 02 November 2005 - 03:28 PM

Cheers for all the help.

If abstract classes inherit other abstract base classes:


                          Abstract


          abstract                     abstract


DerivedGL derivedDX    DerivedGL  DerivedDX




How do i implement the virtual methods of the base class in GL/DX. Are they implemented in both the derivedGL/DX files? Do i have to derive a new GL and DX classes from the base class or what? sorry im new to all this oop stuff.
cheers

#5 corey

    Member

  • Members
  • PipPip
  • 78 posts

Posted 02 November 2005 - 03:55 PM

If you will be deriving from classes that derive from the same base classes, you will want to use virtual inheritance. This will create only one base class no matter how many times it is virtually inheirited.

I am not sure I understand how you would get into that problem with the above classes.
class Derived_From_Base: virtual public Base {

};

class Derived2_From_Base: virtual public Base {

};

class Derived: public Derived_From_Base, public Derived2_From_Base {

};

corey

#6 dicksan

    New Member

  • Members
  • PipPip
  • 12 posts

Posted 02 November 2005 - 04:15 PM

None of the classes inherit from 2 base classes, here is a simple diagram of the class structure:



 --------------> Base

              /         \

DerivedFromBase1         Derived2FromBase

         /     \                /        \

DerivedGL DerivedDX    DerivedGL  DerivedDX


The methods in DerivedFromBase are implemented in the DerivedGL and DX .cpp files. How would the methods in the Base class be implemented in GL+DX?

#7 corey

    Member

  • Members
  • PipPip
  • 78 posts

Posted 02 November 2005 - 04:41 PM

Same way as any base class.

Pure virtual methods are inherited as pure virtual unless you implement them.

corey





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users