Problem with inheritance

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 73
Reputation: SHWOO is an unknown quantity at this point 
Solved Threads: 0
SHWOO SHWOO is offline Offline
Junior Poster in Training

Problem with inheritance

 
0
  #1
Feb 23rd, 2007
I have implemented inheritance and in the print function for class playertype I have called the print function from personType. When I call the playerType fucntion print() with a playerType object only the playerType variables are printed and not the personType variable firstName and lastName.

  1. #ifndef H_personType
  2. #define H_personType
  3.  
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class personType
  9. {
  10.  
  11. friend ostream& operator<<(ostream& , const personType& );
  12. friend istream& operator>>(istream& , personType& );
  13. public:
  14. void print() const;
  15. //Function to output the first name and last name
  16. //in the form firstName lastName.
  17.  
  18. void setName(string first, string last);
  19. //Function to set firstName and lastName according
  20. //to the parameters.
  21. //Postcondition: firstName = first; lastName = last
  22.  
  23. string getFirstName() const;
  24. //Function to return the first name.
  25. //Postcondition: The value of the firstName is returned.
  26.  
  27. string getLastName() const;
  28. //Function to return the last name.
  29. //Postcondition: The value of the lastName is returned.
  30.  
  31. personType(string first = "", string last = "");
  32. //constructor
  33. //Sets firstName and lastName according to the parameters.
  34. //The default values of the parameters are empty strings.
  35. //Postcondition: firstName = first; lastName = last
  36.  
  37. private:
  38. string firstName; //variable to store the first name
  39. string lastName; //variable to store the last name
  40. };
  41.  
  42. #endif
  43.  
  44.  
  45.  
  46.  
  47. #include <iostream>
  48. #include <string>
  49. #include "personType.h"
  50.  
  51. using namespace std;
  52.  
  53. //Overloaded extration << operator
  54. ostream& operator<<(ostream& osObject, const personType& name)
  55. {
  56. osObject << name.firstName << " " << name.lastName << endl;
  57.  
  58. return osObject;
  59. }
  60.  
  61. //Overloaded insertion >> operator
  62. istream& operator>>(istream& isObject, personType& name)
  63. {
  64. cout << "Please enter Person's first and last names: " << endl;
  65. isObject >> name.firstName >> name.lastName;
  66.  
  67. return isObject;
  68. }
  69.  
  70. void personType::print() const
  71. {
  72. cout << firstName << " " << lastName << endl;
  73. }
  74.  
  75. void personType::setName(string first, string last)
  76. {
  77. firstName = first;
  78. lastName = last;
  79. }
  80.  
  81. string personType::getFirstName() const
  82. {
  83. return firstName;
  84. }
  85.  
  86. string personType::getLastName() const
  87. {
  88. return lastName;
  89. }
  90.  
  91. //constructor
  92. personType::personType(string first, string last)
  93. {
  94. firstName = first;
  95. lastName = last;
  96. }
  97.  
  98.  
  99. #ifndef H_playerType
  100. #define H_playerType
  101.  
  102. #include "personType.h"
  103.  
  104.  
  105. class playerType : public personType
  106. {
  107. public:
  108. void print();
  109. //Function to print
  110. void setPlayerNum(int num);
  111. void setPlayerPos(string pos);
  112. playerType(string first = "", string last = "", int num = 0, string pos = "");
  113.  
  114. private:
  115. personType name;
  116. int playerNum;
  117. string playerPos;
  118. };
  119.  
  120. #endif
  121.  
  122.  
  123.  
  124. #include <iostream>
  125.  
  126. #include "playerType.h"
  127.  
  128. playerType::playerType(string first, string last, int num, string pos)
  129.  
  130. : name(first, last)
  131. {
  132. playerNum = num;
  133. playerPos = pos;
  134.  
  135. } // end function playerType
  136.  
  137. void playerType::setPlayerNum(int num)
  138. {
  139. playerNum = num;
  140. } //end function setPlayerNum
  141.  
  142. void playerType::setPlayerPos(string pos)
  143. {
  144. playerPos = pos;
  145. } //end function setPlayerPos
  146.  
  147. void playerType::print()
  148. {
  149. personType::print();
  150. cout << "Player's Number: " << playerNum << endl
  151. << "Player's Position: " << playerPos << endl << endl;
  152. } //end function print
  153.  
  154.  
  155.  
  156.  
  157. #include <iostream>
  158. #include <string>
  159.  
  160. #include "playerType.h"
  161. #include "personType.h"
  162.  
  163. using namespace std;
  164.  
  165. void callPrint(personType& p)
  166. {
  167. p.print();
  168. }
  169.  
  170. int main()
  171. {
  172. personType person("Jim", "Webb");
  173. playerType player("Ray", "Allen", 34, "SG");
  174.  
  175. person.print();
  176. player.print();
  177.  
  178. cin >> person;
  179.  
  180. cout << "Person's name input by user: ";
  181. cout << person;
  182. }
Climbing the learning curve of C++
Becoming an expert seems light years away!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem with inheritance

 
1
  #2
Feb 23rd, 2007
You've got a problem with your derived class's constructor:
  1. playerType::playerType(string first, string last, int num, string pos)
  2.  
  3. : name(first, last) // try changing 'name' to 'personType'
  4. {
  5. playerNum = num;
  6. playerPos = pos;
  7.  
  8. } // end function playerType

Better yet, keep the whole constructor in the class declaration:
  1. class playerType : public personType
  2. {
  3. public:
  4. void print();
  5. //Function to print
  6. void setPlayerNum(int num);
  7. void setPlayerPos(string pos);
  8. playerType(string first = "", string last = "", int num = 0, string pos = "")
  9. : personType(first, last), playerNum(num), playerPos(pos) {}
Check this out for more info on why:
http://www.parashift.com/c++-faq-lit....html#faq-10.6
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 73
Reputation: SHWOO is an unknown quantity at this point 
Solved Threads: 0
SHWOO SHWOO is offline Offline
Junior Poster in Training

Re: Problem with inheritance

 
0
  #3
Feb 23rd, 2007
Hey thanks that was the problem. Now my inherited print() function works just fine. The text I am using defines constructors the way I have coded it so I think I will just stick with that way for now.

Are there any benefits to including the constructor in the declaration like that?
Climbing the learning curve of C++
Becoming an expert seems light years away!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem with inheritance

 
1
  #4
Feb 23rd, 2007
Read my link.
Originally Posted by http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
[10.6] Should my constructors use "initialization lists" or "assignment"?


Initialization lists. In fact, constructors should initialize as a rule all member objects in the initialization list. One exception is discussed further down.

Consider the following constructor that initializes member object x_ using an initialization list: Fred::Fred() : x_(whatever) { }. The most common benefit of doing this is improved performance. For example, if the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object. Even if the types are not the same, the compiler is usually able to do a better job with initialization lists than with assignments.

The other (inefficient) way to build constructors is via assignment, such as: Fred::Fred() { x_ = whatever; }. In this case the expression whatever causes a separate, temporary object to be created, and this temporary object is passed into the x_ object's assignment operator. Then that temporary object is destructed at the ;. That's inefficient.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC