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.

#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;
}

Recommended Answers

All 3 Replies

You've got a problem with your derived class's constructor:

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:

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) {}

Check this out for more info on why:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

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?

Read my link.

[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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.