| | |
Problem with inheritance
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 73
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#ifndef H_personType #define H_personType #include <string> using namespace std; class personType { friend ostream& operator<<(ostream& , const personType& ); friend istream& operator>>(istream& , personType& ); public: void print() const; //Function to output the first name and last name //in the form firstName lastName. void setName(string first, string last); //Function to set firstName and lastName according //to the parameters. //Postcondition: firstName = first; lastName = last string getFirstName() const; //Function to return the first name. //Postcondition: The value of the firstName is returned. string getLastName() const; //Function to return the last name. //Postcondition: The value of the lastName is returned. personType(string first = "", string last = ""); //constructor //Sets firstName and lastName according to the parameters. //The default values of the parameters are empty strings. //Postcondition: firstName = first; lastName = last private: string firstName; //variable to store the first name string lastName; //variable to store the last name }; #endif #include <iostream> #include <string> #include "personType.h" using namespace std; //Overloaded extration << operator ostream& operator<<(ostream& osObject, const personType& name) { osObject << name.firstName << " " << name.lastName << endl; return osObject; } //Overloaded insertion >> operator istream& operator>>(istream& isObject, personType& name) { cout << "Please enter Person's first and last names: " << endl; isObject >> name.firstName >> name.lastName; return isObject; } void personType::print() const { cout << firstName << " " << lastName << endl; } void personType::setName(string first, string last) { firstName = first; lastName = last; } string personType::getFirstName() const { return firstName; } string personType::getLastName() const { return lastName; } //constructor personType::personType(string first, string last) { firstName = first; lastName = last; } #ifndef H_playerType #define H_playerType #include "personType.h" class playerType : public personType { public: void print(); //Function to print void setPlayerNum(int num); void setPlayerPos(string pos); playerType(string first = "", string last = "", int num = 0, string pos = ""); private: personType name; int playerNum; string playerPos; }; #endif #include <iostream> #include "playerType.h" playerType::playerType(string first, string last, int num, string pos) : name(first, last) { playerNum = num; playerPos = pos; } // end function playerType void playerType::setPlayerNum(int num) { playerNum = num; } //end function setPlayerNum void playerType::setPlayerPos(string pos) { playerPos = pos; } //end function setPlayerPos void playerType::print() { personType::print(); cout << "Player's Number: " << playerNum << endl << "Player's Position: " << playerPos << endl << endl; } //end function print #include <iostream> #include <string> #include "playerType.h" #include "personType.h" using namespace std; void callPrint(personType& p) { p.print(); } int main() { personType person("Jim", "Webb"); playerType player("Ray", "Allen", 34, "SG"); person.print(); player.print(); cin >> person; cout << "Person's name input by user: "; cout << person; }
Climbing the learning curve of C++
Becoming an expert seems light years away!
Becoming an expert seems light years away!
You've got a problem with your derived class's constructor:
Better yet, keep the whole constructor in the class declaration:
Check this out for more info on why:
http://www.parashift.com/c++-faq-lit....html#faq-10.6
C++ Syntax (Toggle Plain Text)
playerType::playerType(string first, string last, int num, string pos) : name(first, last) // try changing 'name' to 'personType' { playerNum = num; playerPos = pos; } // end function playerType
Better yet, keep the whole constructor in the class declaration:
C++ Syntax (Toggle Plain Text)
class playerType : public personType { public: void print(); //Function to print void setPlayerNum(int num); void setPlayerPos(string pos); playerType(string first = "", string last = "", int num = 0, string pos = "") : personType(first, last), playerNum(num), playerPos(pos) {}
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Jul 2006
Posts: 73
Reputation:
Solved Threads: 0
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?
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!
Becoming an expert seems light years away!
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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- problem about font (C)
- Problem with program (Java)
- Ask a problem,about inheritance :) (C++)
- inheritance, polymorphism, and other features (C++)
- Point to Point3D Inheritance (C++)
- multiple inheritance (C)
- also having a problem with inheritance (C++)
- hybrid inheritance prob (C++)
Other Threads in the C++ Forum
- Previous Thread: Need some help on my program!!!!!!
- Next Thread: Genealogy chart
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






