LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   c++ Abstract Base Classes (http://www.linuxquestions.org/questions/programming-9/c-abstract-base-classes-332563/)

exodist 06-11-2005 05:24 PM

c++ Abstract Base Classes
 
I am very familiar with java, and in java use Interface classes often. Now I am re-learning c++ after a while of not using it, and never having used it for anything majior before.

I am trying to use the c++ equivilent of interfaces, or in other words abstract defenitions to which multiple derived classes need to conform with no actual implementation of the base class.

like most tutorials have you define the 'Vehicle' main class, then 'car', 'bus' and 'train' derived classes.

Most tutorials I have found show you the implementation in clips of code:
http://www.cplusplus.com/doc/tutorial/tut4-4.html
http://software.allindiansite.com/cfaq/abcs.html

but they do nto explain a few things:
Once you define the header file, do you need a .cpp, do you need to compile anything specifically for the interface?
or do you simply include the header in the derived class?

example:
here is a header, chaser.h:
Code:

#ifndef CHASER_H
#define CHASER_H
#include "MapObject.h"
/*
        Interface for anything that needs to track on object on the map
*/

namespace OpenRPGGDE
{
       
        class MapObject;
        class Chaser
        {
        public:
                virtual void endChase(MapObject&) = 0;
        };
}
#endif //CHASER_H

do I need a chaser.cpp file liek this:
Code:

#include "Chaser.h"
#include "MapObject.h"
using namespace std;
using namespace OpenRPGGDE;

namespace OpenRPGGDE
{
        void Chaser::endChase(MapObject& theObject)
        {}
}

or is that completely unneccessary, it compiles fine with g++ -c chaser.cpp -o chaser.o

It seems to me that making a .o file for all the interfaces would not be necessary, but my code is not yet to the point where I can test this yet. As well I would rather not write my code making any assumptions over this. I am going to try and implement a test for this, but I would liek some information to alleviate the headackes I am sure to have over this.

Any help is much appreciated.

exodist 06-11-2005 06:05 PM

Answer (I think)
 
I found my own answer, the test was easyer than I expected to set up, here are my files:

ABC.h:
Code:

#ifndef ABC_H
#define ABC_H

class ABC
{
public:
        virtual void output() = 0;
};
#endif

I did not make an ABC.cpp

derivedA.h:
Code:

#include "ABC.h"
class derivedA : public ABC
{
public:
        derivedA();

        void output();
};

derivedA.cpp:
Code:

#include "derivedA.h"
#include <cstdlib>
#include <iostream>

derivedA::derivedA() {}

void derivedA::output()
{
        std::cout << "derivedA\n";
}

derivedB.h:
Code:

#include "ABC.h"
class derivedB : public ABC
{
public:
        derivedB();

        void output();
};

derivedB.cpp:
Code:

#include "derivedB.h"
#include <cstdlib>
#include <iostream>

derivedB::derivedB() {}

void derivedB::output()
{
        std::cout << "derivedB\n";
}

now there was a typeo I decided not to fix, but here is the class for the executable to test the above:
primeary.cpp:
Code:

#include "ABC.h"
#include "derivedA.h"
#include "derivedB.h"

int main()
{
        ABC *a;
        derivedA A;
        derivedB B;
        A.output();
        B.output();
        a = &A;
        a->output();
        a = &B;
        a->output();
        return 0;
}

now here is my compile syntax to make it all work:

g++ -c derivedA.cpp
g++ -c derivedB.cpp
g++ primeary.cpp derivedA.o derivedB.o -o test

then I ran test and got this:

Code:

exodist@Abydos-64 Tests $ ./test
derivedA
derivedB
derivedA
derivedB



All times are GMT -5. The time now is 10:00 AM.