Classes and Inheritance Problems

I cannot understand why I am getting an error for 'nameInput' and 'weightInput' on Line 15 in Pitcher.cpp. I am simply trying to create a "PITCHER" class derived from the "PLAYER" class.

I have been over this problem for hours, comparing it to similar code, and simply do not get it. I have not even got to writing the main function yet or filled in some of the functions because this problem is holding me up. Any help would be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//          PLAYER.CPP FILE
// Implementation File for Player Class
#include "Player.h"
#include <iostream>

Player::Player() // Default Constructor
{
   name = "Blank";
   weight = 0;
}

Player::Player(string nameInput, int weightInput)
{
   name = nameInput;
   weight = weightInput;
}

void Player::show()
{
   cout << "Name: " << name << endl;
   cout << "Weight: " << weight << endl;
}
  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//         PLAYER.H FILE
// Specification File for Player Class

#ifndef PLAYER_H
#define PLAYER_H
#include<string> // string class included for name of Player
using namespace std; // needed for string 

class Player
{
private:
   string name;
   int weight;

public:
   Player(); //Default Constructor
   Player(string, int); //Constructor to set name and weight

   //Function prototypes
   void show(); //Show method to print name and weight
   
};

#endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//           PITCHER.CPP FILE
#include "Pitcher.h"
#include "Player.h"
#include <iostream>  
#include <string>

//PITCHER CLASS DEFINITIONS:

Pitcher::Pitcher()  // Default Constructor
{
   earnedRuns = 0;
   inningsPitched = 0;
}

// ERROR IS ON THE FOLLOWING LINE. nameInput and weightInput below:
Pitcher::Pitcher(int runs, int innings) : Player(nameInput, weightInput)
{
   earnedRuns = runs;
   inningsPitched = innings;
}


float Pitcher::eraCalc(int earnedRuns, int inningsPitched)
{
   float temp;
   temp = ((earnedRuns*inningsPitched) * 9);
   return temp;
}

void Pitcher::show()
{
   cout << "Earned Runs: " << earnedRuns << endl;
   cout << "Innings Pitched: " << inningsPitched << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//          PITCHER.H FILE
#ifndef PITCHER_H
#define PITCHER_H
#include "Player.h"

class Pitcher : public Player
{
protected:
   int earnedRuns;
   int inningsPitched;
public:
   Pitcher();  // Default Constructor
   Pitcher(int runs, int innings); // Constructor #2 

   //Function prototypes
   float eraCalc(int, int); // Earned run average calc function
   void show(); //Show method to print earnedRuns, inningsPitched, and calculated ERA
   
};

#endif 

Last edited on
There are no variables named nameInput or weightInput in scope on line 15 in Pitcher.cpp.
The Player.cpp references those named variables, which are both in public as part of the constructor. Shouldn't that work?
Last edited on
closed account (E0p9LyTq)
You still need to provide valid values for the Player data members in your Pitcher constructor since you reference the non-default Player constructor.

Your non-default constructor prototype might be
Pitcher(string nameInput, int weightInput, int runsInput, int inningsInput);
Thanks FurryGuy. If I change the prototype, then the actual definition in the cpp file looks like it needs to change as well, which is confusing, because the order of the prototype is:

 
Pitcher(int, int, string, int); // Constructor #2  


and the first line of the definition is:
 
Pitcher::Pitcher(int runs, int innings) : Player(nameInput, weightInput)


But that doesnt seem to work.
closed account (E0p9LyTq)
You need to change the constructor definition as well.

Pitcher::Pitcher(string nameInput, int weightInput, int runsInput, int inningsInput) : Player(nameInput, weightInput)

Remember, function parameters have to match in the declarations and definitions. A constructor is simply a special function.
Last edited on
That worked! I believe that I was thinking that the function parameter DID already match, because the 4 used variables were listed already, albeit split between the Pitcher and the Player.

Which makes me wonder: If the Pitcher already has all 4 of the used variables, what is the significance/point of the " : Player(nameInput, weightInput)" at the end?
Last edited on
closed account (E0p9LyTq)
Which makes me wonder: If the Pitcher already has all 4 of the used variables, what is the significance/point of the " : Player(nameInput, weightInput)" at the end?

Because Pitcher is a derived class from Player, that bit of code constructs a Player object when it constructs the Pitcher object.
Ah, gotcha. I really appreciate your help. After hours of struggling, I can finally continue. Thanks!
Last edited on
closed account (E0p9LyTq)
Class inheritance can be somewhat difficult to grasp at first. A quick example might help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>

class Base
{
public:
   Base()
   {
      itsBaseData = 0;
      std::cout << "Base() constructed!\n";
   }
   Base(int data) : itsBaseData(data)
   {
      std::cout << "Base(int) constructed!\n";
   }
   ~Base()
   {
      std::cout << "Base destructed!\n";
   }

private:
   int itsBaseData;
};

class Derived : public Base
{
public:
   Derived() : Base()
   {
      std::cout << "Derived() constructed!\n";
   }
   Derived(double moreData) : Base()
   {
      itsDerivedData = moreData;
      std::cout << "Derived(int) constructed!\n";
   }
   Derived(int data, double moreData) : Base(data), itsDerivedData(moreData)
   {
      std::cout << "Derived(int, double) constructed!\n";
   }
   ~Derived()
   {
      std::cout << "Derived destroyed!\n";
   }

private:
   double itsDerivedData;
};


int main()
{
   Base* b1 = new Base();
   delete b1;
   b1 = nullptr;
   std::cout << "\n";

   Base* b2 = new Base(150);
   delete b2;
   b2 = nullptr;
   std::cout << "\n";

   Derived* d1 = new Derived();
   delete d1;
   d1 = nullptr;
   std::cout << "\n";

   Derived* d2 = new Derived(8.3);
   delete d2;
   d2 = nullptr;
   std::cout << "\n";

   Derived* d3 = new Derived(150, 8.3);
   delete d3;
   d3 = nullptr;

   return 0;
}
Awesome, thanks again!
Topic archived. No new replies allowed.