943,630 Members | Top Members by Rank

View Poll Results: I am sorry for this. Please never mind it
sorry 1 50.00%
sorry 1 50.00%
Voters: 2. You may not vote on this poll

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 353
  • C++ RSS
Aug 14th, 2009
0

again having problem about inheritance please help

Expand Post »
When I compile the code below it generates more than 30 syntax errors. It does not make sense. The problem is probably somewhere in
C++ Syntax (Toggle Plain Text)
  1. Patient(string name_of_patient, int age_of_patient, string address_of_pat, double visit_duration_ofp, string ssn_of_patient, Doctor &doc);
but I could not figure out the problem.
I know it is not convenient to post all the code but I think it will be helpful. Alsoi I attached the cpp file
The code is below:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. class Person
  7. {
  8. public:
  9. Person();
  10. Person(string name_of_person, int age_of_person, string address_of_person);
  11. Person(const Person &p);
  12. ~Person();
  13. string get_name()const;
  14. string get_address() const;
  15. int get_age()const ;
  16. void set_address(string address_ofp);
  17. void set_name(string name_ofp);
  18. void set_age(int age_ofp);
  19. void input(istream &inp);
  20. void output(ostream &outp);
  21. protected:
  22. string name;
  23. string address;
  24. int age;
  25. };
  26.  
  27. class Patient: public Person
  28. {
  29. public:
  30. Patient();
  31. Patient(string name_of_patient, int age_of_patient, string address_of_pat, double visit_duration_ofp, string ssn_of_patient, Doctor &doc);
  32. Patient(const Patient &p);
  33. ~Patient();
  34. double get_visit_duration() const;
  35. string get_ssn() const;
  36. void set_ssn_(string ssn_ofp);
  37. void set_visit_duration(double visit_duration_ofp);
  38.  
  39. void setDoctor(Doctor & doc)
  40. virtual double billing() const;
  41. void input(istream &inpa);
  42. void output(ostream &outpa);
  43.  
  44. protected:
  45. double visit_duration;
  46. string ssn;
  47. Doctor doctor;
  48. };
  49. class Doctor: public Person
  50. {
  51. public:
  52. Doctor();
  53. Doctor(string name_of_doctor,int age_of_doctor, string add_of_doctor, double hour_fee_of_doc, long reg_number, string specialty);
  54. Doctor(const Doctor &d);
  55. ~Doctor();
  56. double get_fee_of_doc() const;
  57. long get_regnum_of_doc() const;
  58. string get_spec_of_doc() const;
  59. void set_spec_of_doc(string specialty);
  60. void set_fee_of_doc(double hour_fee);
  61. void set_regnum_of_doc(long reg_num);
  62. void input(istream &ind);
  63. void output(ostream &outd);
  64. protected:
  65. double doctor_hour_fee;
  66. long doctor_reg_num;
  67. string spacialty;
  68. };
  69. int main()
  70. {
  71. Doctor doctor1("ali",21,"deneadres",100,123333,"kalp");
  72. //doctor1.input(cin);
  73. doctor1.output(cout);
  74. Patient patient1("mustafa",23,"adresim",0.5,23232,doctor1());
  75. //patient1.input(cin);
  76. patient1.output(cout);
  77.  
  78. }
  79. Person::Person()
  80. {
  81. name = "No name yet";
  82. address = "No address yet";
  83. age = 0;
  84. }
  85. Person::Person(string name_of_person, int age_of_person,string address_of_person)
  86. {
  87. name = name_of_person;
  88. age = age_of_person;
  89. address = address_of_person;
  90. }
  91. Person::Person(const Person &p)
  92. {
  93. name = p.name;
  94. age = p.age;
  95. address = p.address;
  96. }
  97. Person::~Person()
  98. {
  99. }
  100. string Person::get_name() const
  101. {
  102. return name;
  103. }
  104. string Person::get_address() const
  105. {
  106. return address;
  107. }
  108. int Person::get_age() const
  109. {
  110. return age;
  111. }
  112. void Person::set_address(string address_ofp)
  113. {
  114. address = address_ofp;
  115. }
  116. void Person::set_name(string name_ofp)
  117. {
  118. name = name_ofp;
  119. }
  120. void Person::set_age(int age_ofp)
  121. {
  122. age = age_ofp;
  123. }
  124.  
  125. void Person::input(istream &inp)
  126. {
  127. cout << "Please enter information of person (name--address--age)" << endl;
  128. inp >> name >> address >> age;
  129. }
  130. void Person::output(ostream &outp)
  131. {
  132. outp << "Name: " << get_name() << " Address: " << get_address() << endl << " Age : " << get_age() << endl;
  133. }
  134.  
  135. Patient::Patient():Person(),visit_duration(0.0),ssn("0"),doctor(Doctor())
  136. {
  137.  
  138. }
  139. Patient::Patient(string name_of_patient, int age_of_patient, string address_of_pat, double visit_duration_ofp, string ssn_of_patient, Doctor & doc):Person(name_of_patient,age_of_patient,address_of_pat),visit_duration(visit_duration_ofp),ssn(ssn_of_patient),doc(doctor)
  140. {
  141. }
  142. Patient::Patient(const Patient &p)
  143. {
  144. name = p.name;
  145. age = p.age;
  146. address = p.address;
  147. visit_duration = p.visit_duration;
  148. ssn = p.ssn;
  149. doctor = doc;
  150. //set_doc_fee = p.set_doc_fee;
  151. }
  152. Patient::~Patient()
  153. {
  154. }
  155. double Patient::get_visit_duration() const
  156. {
  157. return visit_duration;
  158. }
  159. string Patient::get_ssn() const
  160. {
  161. return ssn;
  162. }
  163. void Patient::set_ssn_(string ssn_ofp)
  164. {
  165. ssn = ssn_ofp;
  166. }
  167.  
  168. void Patient::set_visit_duration(double visit_duration_ofp)
  169. {
  170. visit_duration = visit_duration_ofp;
  171. }
  172.  
  173. void Patient::setDoctor(Doctor &doc)
  174. {
  175. doctor = doc;
  176. }
  177. double Patient::billing() const
  178. {
  179. /*Doctor *docto_try;
  180. docto_try = new Doctor;*/
  181.  
  182. return (doctor.get_fee_of_doc()*get_visit_duration());
  183. }
  184.  
  185. void Patient::input(istream &inpa)
  186. {
  187. cout << endl;
  188. cout << "Please enter information of patient(name--address--age--visit duration--ssn)" << endl;
  189. inpa >> name >> address >> age >> visit_duration >> ssn ;
  190. }
  191.  
  192. void Patient::output(ostream &outpa)
  193. {
  194. outpa << "Name of Patient: " << get_name() << " Address of Patient: " << get_address()
  195. << endl << " Age of Patient: " << get_age() << " Visit Duration of Patient: " << get_visit_duration() << endl << " SSN: of Patient " << get_ssn()
  196. << endl << "The amount you have to pay is: " << Patient::billing() <<endl;
  197. }
  198. //
  199. Doctor::Doctor():Person(),doctor_hour_fee(0.0), doctor_reg_num(0), spacialty("0")
  200. {
  201. }
  202. Doctor::Doctor(string name_of_doctor,int age_of_doctor, string add_of_doctor, double hour_fee_of_doc, long reg_number, string specialty):Person(name_of_doctor,age_of_doctor,add_of_doctor),doctor_hour_fee(hour_fee_of_doc),doctor_reg_num(reg_number),spacialty(specialty)
  203. {
  204. }
  205. Doctor::Doctor(const Doctor &d)
  206. {
  207. name = d.name;
  208. age = d.age;
  209. address = d.address;
  210. doctor_hour_fee = d.doctor_hour_fee;
  211. doctor_reg_num = d.doctor_reg_num;
  212. spacialty = d.spacialty;
  213. }
  214. Doctor::~Doctor()
  215. {
  216. }
  217. double Doctor::get_fee_of_doc()const
  218. {
  219. return doctor_hour_fee;
  220. }
  221. long Doctor::get_regnum_of_doc() const
  222. {
  223. return doctor_reg_num;
  224. }
  225.  
  226. string Doctor::get_spec_of_doc() const
  227. {
  228. return spacialty;
  229. }
  230.  
  231. void Doctor::set_spec_of_doc(string specialty)
  232. {
  233. spacialty = specialty;
  234. }
  235. void Doctor::set_fee_of_doc(double hour_fee)
  236. {
  237. doctor_hour_fee = hour_fee;
  238. }
  239. void Doctor::set_regnum_of_doc(long reg_num)
  240. {
  241. doctor_reg_num = reg_num;
  242. }
  243. void Doctor::input(istream &ind)
  244. {
  245. cout << "Please enter information of doctor " << endl << " (name--address--age--1 hour fee--registraiton number--specialty) " << endl;
  246. ind >> name >> address >> age >> doctor_hour_fee >> doctor_reg_num >> spacialty;
  247. }
  248. void Doctor::output(ostream &outd)
  249. {
  250. outd << "Name of Doctor: " << name << " Address of Doctor: " << address << " Age: not allowed to see " << endl << " 1 Hour fee of doctor: " <<
  251. doctor_hour_fee << "Registratioon number of doctor: " << doctor_reg_num << endl << "Specialty of doctor: " << spacialty;
  252. }
Attached Files
File Type: cpp for_forum.cpp (6.2 KB, 10 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
esesili is offline Offline
38 posts
since Apr 2009
Aug 14th, 2009
1

Re: again having problem about inheritance please help

A multitude of errors, mostly caused by trying to write everything in on go.

(a) Patient includes an instance to Doctor. Therefore you must have a declaration of Doctor before Patient is declared. If it was just a reference or pointer you could have used a forward declaration e.g
class Doctor; BUT you can't in this instance.

(b) Lots of typos. you use doc to mean doctor etc e.g. in the copy constructor for Patient. You write doctor1() in the main() { } section.

(c) Failure to call the base constructor in derived constructors and in the assignment operator.

(d) Lots of inconsitancy in format/layout which itself is not wrong but makes it difficult to see what is going on. For beginners I would recommend:
(i) Never use using namespace std; (ii) Don't put names of variables within declarations.
(iii) Consistent set/get naming so it is clear
(iv) copy Constructors are build like this e.g.
c++ Syntax (Toggle Plain Text)
  1. Doctor::Doctor(const Doctor& A) : Person(A),
  2. hour_fee(A.hour_fee),reg_num(A.reg_num)
  3. {}

and the assignment operator like this
c++ Syntax (Toggle Plain Text)
  1. Doctor&
  2. Doctor::Doctor(const Doctor& A)
  3. {
  4. if (this!=&A)
  5. {
  6. Person::operator=(A);
  7. hour_fee=A.hour_fee;
  8. reg_num=A.reg_num;
  9. }
  10. return *this;
  11. }

(v) Don't use a general protected, if that is what you have then change your model to a pure virtual inheritance.

(vi) Validate input.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can't seem to let user input necessary information
Next Thread in C++ Forum Timeline: C++ SDL help.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC