Natwar Singh
Natwar Singh

Reputation: 2275

Which one is better, pointer to other class or inherit from that class

I want to know which is better way for this problem:

A teacher can teach more than one subject or can take more than on lab or may be both in a institute. Lab,Subject,Teacher also have some other variables. I did not write them all here. New lab,subject and teacher can be added individually. When a teacher is added I only input all variables of Teacher class and labCode and subCode. There are some Labs and Subjects which are predefined and others which are added later. I can view record of a teacher which shows his Lab and Subject details also if available.

My concepts in c++ are not too strong and I don't know which one is better for this. Should I give a pointer in Teacher class to Lab and Subject class or should I inherent them, or is there any better way to do this.

class Lab{
    char labCode[20];
    char labName[40];
        int labYear;
    //here default constructor 
};
class Subject{

    char subCode[20];
    char subName[20];
        int subYear;
    //here default constructor 
};
//-------------------------------------
class Teacher{
    char facName[30];
    int teacherSubCount;
        int teacherLabCount;
    Subject *subject;
        Lab *lab;
    //here default constructor 

};
//------------or should i do like this--------------
class Teacher:public Lab,public Subject{
   char teacherName[30];
    int teacherSubCount;
        int teacherLabCount; 
   //here default constructor 

};

If you can suggest any tutorials so I can learn the power of a pointer as my book states "pointers are very powerful c++", but they only give some simple examples :)

Upvotes: 3

Views: 344

Answers (6)

comonad
comonad

Reputation: 5251

You will understand the concept, if you look at this example:

void doExperimentsIn( Lab &room ){...}

If you derive Teacher from Lab, you will be able to do experiments inside a Teacher, too. (Which is silly.)

Upvotes: 0

Philipp
Philipp

Reputation: 31

Inheritance implies a "is-a" relationship, while a pointer implies a "has-a" relationship.

Is a teacher a lab? Is a teacher a subject? No, it has a subject and it has a lab, so it's a "has-a" relationship. So you should use pointers, in this case.

Why inheritance wouldn't work would become apparent when you would try to change the lab of a teacher. You can easily change the object a pointer is pointing to, but replacing all the data of the base class is a lot more cumbersome.

To give an example where you would use inheritance, EnglishTeacher could inherit Teacher, because it is also a teacher, but with additional abilities.

Upvotes: 3

David
David

Reputation: 218887

"Favor 'object composition' over 'class inheritance'." - Gang of Four

More specific to your classes, though, first ask yourself... "Is a Teacher a type of Lab or a type of Subject?" Clearly, the answer is "no."

In an inheritance model, the child class should itself be an instance of the parent class. Considering the Liskov Substitution Principle, that child object should be treatable as that parent object. So if there's code that's looking for a Subject you would be able to pass it a Teacher. Intuitively, that doesn't make much sense and is not an accurate representation of the real-world domain entities being modeled.

In this case you definitely want object composition instead of class inheritance. The Teacher has a Lab and has a Subject, not is a Lab or is a Subject. Indeed, if the Teacher can have multiple Labs or Subjects then the composition would simply change from a single instance class member to an array or list of some kind. Whereas the inheritance approach would break entirely in that case.

When modeling objects that represent real-world concepts like this, consider intuition of those real world concepts foremost and the technical shortcuts to implementation secondary. If it doesn't make sense in terms of what's being modeled, then it won't make sense in the code either.

Upvotes: 4

Component 10
Component 10

Reputation: 10497

Think about whether the 'is-a' (for inheritence) and 'has-a' (for aggregation) relationships make sense in your proposed solution

A Teacher is neither a Lab nor a Subject - I think you should go with the aggregation solution.

However, what you really need to get completely sorted before you get started on the code is your data model. You need to identify the relationships and multiplicity (i.e. one-to-one, one-to-many etc.) between the objects.

Upvotes: 3

David W
David W

Reputation: 10184

When you inherit a class, as in your last example, think of it as extending that class' existing, defined behavior. In that vein, then, ask yourself the question: Does a "teacher" enhance the capabilities of a "lab"? To me, the answer is clear - they don't - they're entirely different objects, so inheritance isn't the ideal way to model these elements in your problem domain. A teacher has a class, a class has a subject, and so on - hence, aggregation seems to be a better path to follow.

Upvotes: 1

pmr
pmr

Reputation: 59811

Taking the common sense approach:

Inheritance expresses an is-a relationship. A teacher is obviously neither a Subject or a Lab and this would be a serious eye-brow raiser. So I'd say aggregation (a has-a or has-many) is the way to go.

You will also need to decide if you are using value semantics or reference semantics. That means will equality be actual equality of objects in your program or will equality be computable through certain parts of your object.

If you use reference semantics you will need to make sure you are using smart-pointers or some other form of memory managment, otherwise things get ugly really quickly.

Upvotes: 1

Related Questions