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.
It is totally fine to put all the things that the classes
SongJobandVideoJobhave in common into a common base-class. However, this will cause problems once you want to add a subclass ofJobthat has nothing to do with artists.There are some things to note about the code you have posted. First, your class
Jobis apparently not an abstract base class. This means that you can have jobs that are just jobs. NotSongJoband notVideoJob. If you want to make it clear that there can not be a simpleJob, make the base-class abstract:Now, you cannot create instances of
Job:Note that the functions are now virtual, which means that subclasses can override them. The
= 0and the end means that subclasses have to provide an implementation of these functions (they are pure virtual member functions).Secondly, your class
SongJobhas a memberstd::vector<Job>. This is almost certainly not what you want. If you add aSongJobto this vector, it will become a normalJob. This effect is called slicing. To prevent it, you’d have to make it astd::vector<Job*>.There is much more to say here, but that would go to far. I suggest you get a good book.