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