Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting OOP right

Tags:

c++

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 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.

like image 519
Adrian Avatar asked Dec 03 '25 21:12

Adrian


2 Answers

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.

like image 92
Björn Pollex Avatar answered Dec 06 '25 13:12

Björn Pollex


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.

like image 34
Alok Save Avatar answered Dec 06 '25 13:12

Alok Save



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!