Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 16th, 2006, 5:42 PM   #11
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Or even
bool CharCon::eq(Comparator &x)
{
	return (character == ((CharCon &)x).getValue());
}

Note that this means you are telling the compiler that x is always a CharCon - there is nothing to stop you passing in a different derived class to the eq function and your program will probably crash.
The Dark is offline   Reply With Quote
Old Feb 16th, 2006, 6:43 PM   #12
gizzy
Newbie
 
Join Date: Feb 2006
Location: Glasgow, Scotland
Posts: 8
Rep Power: 0 gizzy is on a distinguished road
Got the tree working now I am now considering using a stack to keep all the 'new' pointers together, in order to delete them at the end of the program.

Thanks for all the help folks.
gizzy is offline   Reply With Quote
Old Feb 17th, 2006, 1:12 AM   #13
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
If you want a bit more safety, you can also check the the Comparator is a CharCon like this;
bool CharCon::eq(Comparator &x)
{
      if (dynamic_cast<CharCon *>(&x) != (CharCon *)NULL)
      {
	   return (character == ((CharCon &)x).getValue());
      }
      else
      {
           do_something_else();
      }       
}
A better way, from a design perspective and a lot of practical perspectives as well, is for the Comparator class to provide member functions like getValue(), possibly as pure virtuals that are overridden by derived classes. That avoids the need to use casting. Of course, that sort of thing is a bit hard to retrofit if you're not allowed to change the Comparator class.

It is probably a good idea to provide a virtual destructor (even if it does nothing) in the Comparator class as well. Not important for the current problem, but saves difficulties with other types of problem.
grumpy is offline   Reply With Quote
Old Feb 18th, 2006, 1:37 PM   #14
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by grumpy
An abstract base class in C++ is a class that declares one or more pure virtual member functions. Optionally, in C++, any virtual member function can be defined whether it is pure or not (i.e. contrary to Dawei's and Cerulean's comments, an abstract base class CAN actually provide a body for any virtual function, whether it is pure or not). The existence of any virtual member functions, however, means that the class cannot be instantiated (just as it is not possible to create an instance of a Java interface).
I'm confused Robert, can you explain a bit more? I also thought that pure virtual functions lack implementation, so that the derived class, and only the derived class may do so later.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Feb 18th, 2006, 2:11 PM   #15
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by nnxion
I'm confused Robert, can you explain a bit more? I also thought that pure virtual functions lack implementation, so that the derived class, and only the derived class may do so later.
I think he meant that the existence of any pure virtual member functions prevent a class from being instantiated. Only the existance of pure virtual functions make a class abstract.
Arevos is offline   Reply With Quote
Old Feb 18th, 2006, 5:14 PM   #16
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
No. What I mean is that there is nothing preventing a class that declares a pure virtual function from also implementing it.

For example;
#include <iostream>
class Base
{
     public:

           Base(); 
           virtual ~Base() = 0;

           virtual void Operation() = 0;
};

class Derived : public Base
{
     public:

          Derived();
          ~Derived();
          void Operation();
}

// implement member functions

Base::Base()
{
    std::cout << "Constructing Base\n";
}

Base::~Base()    // We are actually REQUIRED to implement a destructor we declare even if it is pure virtual
{
    std::cout << "Destructing Base\n";
}

void Base::Operation()     //   YES, we are allowed to implement a pure virtual function
{
    std::cout << "Operating on Base\n";
}

Derived::Derived() : Base()    // explicitly initialising Base is optional
{
    std::cout << "Constructing Derived\n";
}

Derived::~Derived() 
{
    std::cout << "Destructing Derived\n";
}

void Derived::Operation()
{
    Base::Operation();                             // we can explicitly call a pure virtual function if we have implemented it
    std::cout << "Operating on Derived\n";
}

int main()
{
      Base *base = new Base;      // remove this line as it will result in compiler error as Base is abstract

      Derived *derived = new Derived;    //   OK.  We will see that Base's constructor is invoked, then Derived's

      derived->Operation();  //  OK.  We will see that Base::Operation is called by Derived::Operation()

      derived->Base::Operation()   //  I'll leave working out what this does as an exercise....

      delete derived;  // Derived's destructor is invoked then Base's
}
grumpy is offline   Reply With Quote
Old Feb 18th, 2006, 5:46 PM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Well, I certainly learned something there. Thanks.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 18th, 2006, 7:29 PM   #18
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by grumpy
No. What I mean is that there is nothing preventing a class that declares a pure virtual function from also implementing it.
Really? Didn't know you could do that
Arevos is offline   Reply With Quote
Old Feb 18th, 2006, 7:53 PM   #19
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Since nobody seems to have mentioned it, I'm just wondering if everyone is aware that you can declare abstract classes in Java as well, and from what I understand from reading grumpy's post, they are essentially the same in both languages.
titaniumdecoy is offline   Reply With Quote
Old Feb 18th, 2006, 9:00 PM   #20
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Abstract classes will behave similarly between the two languages; however, in Java, you have to use the abstract keyword if you define a class that has a "pure virtual" (i.e. not yet defined) function.
C++:
class abs // no special keywords on this line
{
public:
	abs(){}
	virtual int something() = 0;
};
class full : public abs
{
public:
	full(){}
	int something() { return 1; }
};
Java:
public abstract class abs // have to have abstract here
{
	public abs() {}
	public abstract int something();
}
public class full extends abs
{
	public full() {}
	public int something() { return 1; }
}
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC