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