General question about inheritance

I'm building a program that has a number of different classes and am having a problem with accessing other classes from a non-derived class. I was wondering what the best way to do this is.

Example.

There is a class Doctor, Person and Patient. Patient is a child of Person but Doctor is not related to either. Each Patient is to have a Doctor when the object is contructed. What is the best way to include a Doctor in a Patient object. How do I pass Patient all the information from Doctor. I assume this has to happen in Patient's construtor. To further confuse things Doctor is a child of another class, in which the Doctor's name is inherited. Is the only way to access Doctor's data members from Patient through getters and setters? and if I have to access the Doctor's name specifically from Patient do I need to create a getter in Patient that links back to Doctor and in Doctor have a getter that links back to it's parents class? I have read alot on how to access data members and functions from parent to child, but there is not alot of material out there on how to access classes outside of the derived relationship.

On a side note. Does anyone have a good book recommendation on how to "think like a programmer."

Thanks for any and all help.
Its hard to understand all this without code, so I may be mistaken, but you can declare a Doctor object in Patient, as in
1
2
3
4
class patient : public Person
{
      Doctor m_Doctor
};

Have the constructor take a doctor object. You won't need getters and setters for the doctor object in patient this way.
patient can have a doctor and a doctor can have a patient too.

so this is kind of "has a" relation. what drowdemon42 said looks fine. And you will have many get/set functions for your class like for name and so on. That's a fine thing to have getter/setter in a class.

Also, keep in mind that a doctor can have many patients and vice versa, so keeping a list of doctor's in patient or a list of patient in doctor class will help.

both the class should inherit from person which will give them some common characteristics of a human. If doctor inherits from other class too, there could be a multiple inheritance also.
programmer are not made by reading books.... just do a lot of programming and you will think like that soon.
So if I have two classes not related the only way for class B to access class A is through getters and setters, correct?
yes... through the object.
For the book, I recomend you Effective C++ 3rd edition by Scott Meyers. The 2nd edition is good too.
This is very interesting and even after more than 2 year of c++ practise I learn a lot of things from this book.
closed account (z05DSL3A)
Teelnaw wrote:
On a side note. Does anyone have a good book recommendation on how to "think like a programmer."
writetonsharma wrote:
programmer are not made by reading books.... just do a lot of programming and you will think like that soon.

Books are a very important part of my learning process. The whole 'just do a lot of programming and you will think like that soon' is wrong. You need exposure to new things to learn, you then start making connections about how you get form A to B. Those connections then start you thinking, if that is how I got from A to B can I do something similar to get me somewhere else.

Teelnaw,

A few books that may make interesting reading (see if you can find them in a Library)

The Object-Oriented Thought Process: An Object Lesson Plan
by Matt Weisfeld

Beautiful Code: Leading Programmers Explain How They Think
by Andy Oram (Editor), Greg Wilson (Editor)

The Pragmatic Programmer: From Journeyman to Master
By Andrew Hunt and David Thomas
http://pragprog.com/the-pragmatic-programmer
Grey Wolf,

he wants to think like a programmer and not learn c++...
closed account (z05DSL3A)
he wants to think like a programmer and not learn c++...

and? I have given no C++ specific info.

That was a reply to you on "why i didn't refer some books"... I was not commenting on your post.. :)
writetonsharma wrote:
And you will have many get/set functions for your class like for name and so on. That's a fine thing to have getter/setter in a class.
You were born with a name, ¿how many times did you change it?
¿why do you care about your doctor's name?

writetonsharma wrote:
patient can have a doctor and a doctor can have a patient too.
But a doctor can have a lot of patients. I think it will be better to maintain a reference instead or a composition.

Teelnaw wrote:
So if I have two classes not related the only way for class B to access class A is through getters and setters, correct?
Not sure if I follow you. To send a message you need a receiver object, how do you obtain it, it is another issue. http://en.wikipedia.org/wiki/Law_of_Demeter#In_object-oriented_programming
However, ¿why do you need to access to its data?

Teelnaw wrote:
if I have to access the Doctor's name specifically from Patient do I need to create a getter in Patient that links back to Doctor and in Doctor have a getter that links back to it's parents class?
Or just make the variables public...
But yeah, do the chain.

You were born with a name, ¿how many times did you change it?

Its not about changing.. Its about giving an interface to the members of a class. Nothing will change doesn't mean, we will not provide interface. The examples are not to be taken as they are.. these posts are only to let someone understand. :)

But a doctor can have a lot of patients.

this is what i said above, "keep in mind that a doctor can have many patients and vice versa". Now how one goes about implementing is a choice.

closed account (z05DSL3A)
Changing your name is a perfectly viable thing to do.
Green Grass wrote:
Changing your name is a perfectly viable thing to do.
Agreed.
Topic archived. No new replies allowed.