Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2005, 6:46 PM   #1
TimC
Newbie
 
Join Date: Dec 2005
Posts: 1
Rep Power: 0 TimC is on a distinguished road
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;
}
TimC is offline   Reply With Quote
Old Dec 7th, 2005, 6:49 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
Rule #12:
Code Tags - When posting code please put all code between the tags. This makes reading source code alot easier for everyone trying to read the code. If you haven't used code tags in a post, moderators may add them in for you, but it would make our job a lot easier were you to use them yourself.
Cant help you with your problem though..

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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Dec 7th, 2005, 6:56 PM   #3
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
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...
UnKnown X is offline   Reply With Quote
Old Dec 7th, 2005, 7:16 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 7th, 2005, 7:42 PM   #5
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Dec 7th, 2005, 8:31 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Dec 7th, 2005, 8:39 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 7th, 2005, 9:03 PM   #8
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Yes, please check the posting guidelines. Nobody wants to read and critique poor posts.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC