LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-11-2005, 06:24 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
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.
 
Old 06-11-2005, 07:05 PM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 11:57 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 12:45 PM
Abstract Control Model juris Linux - Networking 0 10-26-2004 04:45 AM
interface vs abstract (Java) Mohsen Programming 2 07-24-2004 03:06 PM
A simple language abstract, please, re: making my Linux network public lhoff Linux - Networking 3 02-07-2002 10:52 AM


All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration