开发者

Getting OOP right

Ok, this is my problem. I have the following classes:

class Job {
   bool isComplete() {}
   void setComplete() {}
   //other functions
};

class SongJob: public Job {
   vector<Job> v;
   string getArtist() {}
   void setArtist() {}
   void addTrack() {}
   string getTrack() {}
   // other functions
};
// This were already implemeted

Now I want to implement a VideoJob and derived it from Job. But here is my problem. I also have the following function witch it was set to work only with SongJob:

void process(SongJob s)
{
// not the real functions
   s.setArtist();
   ..............
   s.getArtist();
   .............
   s.getArtist();
   ...............
   s.setArtist()
}

Here I just want it to show that the function uses only derived 开发者_StackOverflow社区object methods. So if I have another object derived from Job, I will need to change the parameter to Job, but then the compiler would not know about thoose functions and I dont what to test for everyone what kind of object it is and then cast it so I can call the correct function.

So it is okay to put all the functions in the base class, because then I will have no problem, but I don't know if this is correct OOP, if one class deals with Songs and the other with videos, I thing good oop means to have 2 clases.

If I didn't make myself clear, please say so and I will try explaining better.

And in short words, I want to use polymorfism.


It is totally fine to put all the things that the classes SongJob and VideoJob have in common into a common base-class. However, this will cause problems once you want to add a subclass of Job that has nothing to do with artists.

There are some things to note about the code you have posted. First, your class Job is apparently not an abstract base class. This means that you can have jobs that are just jobs. Not SongJob and not VideoJob. If you want to make it clear that there can not be a simple Job, make the base-class abstract:

class Job {
    virtual bool isComplete() = 0;
    virtual void setComplete() = 0;
   //other functions
};

Now, you cannot create instances of Job:

Job job; // compiler-error
std::vector<Job> jobs; // compiler-error

Note that the functions are now virtual, which means that subclasses can override them. The = 0 and the end means that subclasses have to provide an implementation of these functions (they are pure virtual member functions).

Secondly, your class SongJob has a member std::vector<Job>. This is almost certainly not what you want. If you add a SongJob to this vector, it will become a normal Job. This effect is called slicing. To prevent it, you'd have to make it a std::vector<Job*>.

There is much more to say here, but that would go to far. I suggest you get a good book.


In your Base class Job you could add those methods as virtual methods so that a class deriving from Job may or may not override these specific methods.

In your SongJob class you override the methods and dont override them in VideoJob

In, void process() pass a pointer to Base class Job

void process(Job *s) 

It will then call the appropriate methods depending on the adress of the objec s is pointing to which will be a SongJob object.


In C++, you have to do two things to get polymorphism to work:

  • Access polymorphic functions by a reference (&) or pointer (*) to a base type
  • Define the polymorphic functions as virtual in the base type

So, change these from:

class Job {
  bool isComplete() {}
  void setComplete() {}
};

void process(SongJob s)
{
  // ...
}

To:

class Job {
  public: // You forgot this...
  virtual bool isComplete() { }
  virtual void setComplete() { }
};

void process(Job& s)
{
  // ...
}

If you can't define all the functionality you need inside process on your base class (if all the member functions you'd want don't apply to all the derived types), then you need to turn process into a member function on Job, and make it virtual:

class Job {
  public:
  virtual bool isComplete() { }
  virtual void setComplete() { }
  virtual void process() = 0;
};

// ...

int main(int argc, char* argv[])
{
    SongJob sj;
    Job& jobByRef = sj;
    Job* jobByPointer = new SongJob();

    // These call the derived implementation of process, on SongJob
    jobByRef.process();
    jobByPointer->process();

    delete jobByPointer;
    jobByPointer = new VideoJob();

    // This calls the derived implementation of process, on VideoJob
    jobByPointer->process();

    return 0;
}

And of course, you'll have two different implementations of process. One for each class type.

People will tell you all sorts of "is-a" vs "has-a" stuff, and all sorts of complicated things about this silly "polymorphism" thing; and they're correct.

But this is basically the point of polymorphism, in a utilitarian sense: It is so you don't have to go around checking what type each class it before calling functions on it. You can just call functions on a base type, and the right derived implementation will get called in the end.

BTW, in C++, virtual ... someFunc(...) = 0; means that the type that function is defined in cannot be instantiated, and must be implemented in a derived class. It is called a "pure virtual" function, and the class it is defined on becomes "abstract".


Your problem comes from the fact you're calling a process method on an object. You should have a method Process on the Job class and override this method in your derived classes.


use pure virtual functions:

class Job
{
virtual string getArtist() =0;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜