![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Posts: 1
Rep Power: 0
![]() |
PolyMorphism-Easy Q
Ref Polymorphism: I am trying to get the method calculate_pay() to work. As each type of doctor will have a differnt payrate I am trying to call the calculate_pay() but i am having difficulty. Should it be
int doctor::calculate_pay() { return (doctor::get_Payrate()*hours); } //will this line return each type of doctors payrate multiplied by hours or will it only call the doctor payrate??? or am i way off track #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; class doctor{ protected: string newname; string address; int payrate; int hours; int age; public: doctor(); //constructor virtual ~doctor(); virtual void set_Name(string itsname); virtual string get_Name(); virtual void set_Address(string itsaddress); virtual string get_Address(); virtual void set_Age(int itsage); virtual int get_Age(); virtual void set_Hours(int itshours); virtual int get_Hours(); //virtual void set_Payrate(); virtual int get_Payrate(); virtual void get_holidaypay(); virtual int calculate_pay(); }; //end of class declaration doctor::doctor() { } doctor::~doctor() { cout<<"Doctor Destructer called"<<endl; } void doctor::set_Name(string itsname){ newname=itsname; } string doctor::get_Name(){ return newname; } void doctor::set_Address(string itsaddress){ address=itsaddress; } string doctor::get_Address(){ return address; } void doctor::set_Age(int itsage){ age=itsage; } int doctor::get_Age(){ return age; } void doctor::set_Hours(int itshours){ hours=itshours; } int doctor::get_Hours(){ return hours; } //void doctor::set_Payrate(int itspay){ //payrate=itspay; //} int doctor::get_Payrate(){ return payrate=100; } void doctor::get_holidaypay(){ cout<<"You Have weekend off "<<endl; } int doctor::calculate_pay() { return (doctor::get_Payrate()*hours); } //Junior Doctor Class class junior: virtual public doctor { public: junior(){} virtual ~junior(); virtual void get_holidaypay(); virtual int get_Payrate(); }; void junior::get_holidaypay() { cout<<"You Have 2months off "<<endl; } int junior::get_Payrate(){ return payrate=200; } junior::~junior() { cout<<"junior destroctor called"<<endl; } //Consultant Class class consultant:virtual public doctor { public: virtual void get_holidaypay(); consultant(){} virtual ~consultant(); virtual int get_Payrate(); }; consultant::~consultant() { cout<<"Consultant Destructor"<<endl; } int consultant::get_Payrate(){ return payrate=600; } void consultant::get_holidaypay() { cout<<"You Have 6 months off "<<endl; } int main(int argc, char *argv[]) { doctor* Array[5]; doctor* newdoctor; int choice; int i; int ag; string nm; int a,b; string c; int hrs; for(int i=0;i<3;i++) { cout<<"1.doctor, 2.Junior, 3.Consultant"<<endl; cin>>choice; if (choice==1) newdoctor = new doctor(); else if (choice==2) newdoctor = new junior(); else newdoctor = new consultant(); cout<<"Enter doctor name"<<endl; cin>>nm; newdoctor->set_Name(nm); cout<<"Enter doctor Age"<<endl; cin>>ag; newdoctor->set_Age(ag); cout<<"Enter no of hrs worked"<<endl; cin>>hrs; Array[i]=newdoctor; } cout<<"\n"; for(i=0;i<3;i++) { cout<<Array[i]->get_Name(); cout<<"\n"; cout<<Array[i]->get_Age(); cout<<"\n"; Array[i]->get_holidaypay(); cout<<"\n"; cout<<Array[i]->calculate_pay(); cout<<"\n"; cout<<Array[i]->get_Payrate(); cout<<"\n"; delete Array[i]; } system("PAUSE"); return 0; } |
|
|
|
|
|
#2 | ||
|
Professional Programmer
|
Quote:
Question: Is all that your trying to do be able to enter the doctors hours worked and how much he/she makes, then the answer is printed to the screen once you've given each doctor a value amount? If so i can help you by changing your code completely, but i know an easier way to do it if so..
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
||
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
By the looks of it, it will get the return value of doctor::get_Payrate(), multiply it by hours and return that.
Though I'm not an expert on this at all... |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
int main(int argc, char *argv[])
{
doctor* Array[5];
doctor* newdoctor;
int choice;
int i;
int ag;
string nm;
int a,b;
string c;
int hrs;
for(int i=0;i<3;i++)
{Declarie int i once and delete variables a and b if you're not using them.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Also, in OPINION anytime anywhere that you are calculating a paycheck(Rate*Hours) use FLOAT not INT?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#6 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Subclassing makes more sense when behaviors, rather than values, vary. For example, a dog is an animal that can also bark. In your example, all of the "types" of doctors are identical except for different hardcoded return values in two accessor funtions. It seems like variables would have been easier.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Does the red text mean you don't have enough money to pay the doctors, anyway? Quite frankly, I wouldn't have read the post enough to discover if you'd declared forty-leben "i"s in it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Yes, please check the posting guidelines. Nobody wants to read and critique poor posts.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|