inheritance program problem

When I compile the following code, it compiles fine. When I run the code it allows me to enter the input for the first set of data and then give an error that program has stopped running. I believe the issue is how info is defined in each of the classes but I have tried defining it a number of different ways and the program still does not run correctly. I cannot figure out where I went wrong. Any guidance would be appreciated.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

class name {  

protected:  

   char *first, *last;  

public:  

        name() {}; 

        name(char *f, char *l): first (f), last (l){}
                  
        virtual void info(){
               
               name:info();
          
                       } 
              void Print (void)
              
              { setw(30);                                        
              cout <<  "The first name is" << first << endl;
              cout << "The last name is" << last << endl;}
 
          };  

class person : public name {

char gender;  

int  ssn;

public:

   person() {};  

       person(char *f, char *l, char g, int s): gender(g), ssn(s), name::name(f, l) {}

   virtual void info() { // formatted display of the information

        name:info();
    }
     void Print (void) {
         setw(30);
         cout << "The gender is" << gender << endl;
         cout << "The social security number is" << ssn<< endl;
         }
};  
class employee : public person {  

protected:  

int payroll;

char *position;

public:

   employee() {};     

   employee(char *f, char *l, char g, int s, int p, char *pos): payroll(p), position (pos), person::person(f,l,g,s) {}

   void info() { 
    
   employee:info();
       }
 
  void Print (void) {
       setw(30);
       cout << "The payroll is" << payroll << endl;
       cout << "The position is" << position << endl;     
       }
};  
int main()  

{

char fn[20], ln[20], pos[20];

char gen;

int s, pay;

name *worker;

for (int i=0; i<3; i++) {

       cout << "Input the first name: " << endl;

       cin >> fn;

       cout << "Input the last name: " << endl;

       cin >> ln;    

       cout << "Input the gender: " << endl;

       cin >> gen;

       cout << "Input the social security number: " << endl;

       cin >> s;   

       cout << "Input the payroll: " << endl;

       cin >> pay;

       cout << "Input the position: " << endl;

       cin >> pos;   

       worker = new employee(fn, ln, gen, s, pay, pos);

       worker->info();

      }

      system("pause");

      return 0;

}   
In each info() function, you are calling the parent class's info() function incorrectly. You need 2 colons (scope operator ::). Also, it appears that you want to print out the info in the info() functions. So in the name class, the info() function should call Print(). Then in each child class, it should call its parent class's info() function then its own Print() function.

Example:
1
2
3
4
5
void Person::Info()
{
    name::info();
    Print();
}
Thank you for your reply. I have corrected the obvious missing colons and used your example to code the program to call the parent class. The program compiles but still stops on execution. So I think I am not calling the parent classes correctly.

I am going to post the first 2 class sections instead of the entire program for further advice.

1
2
3
4
5
6
7
8
9
10
11
class name {  
protected:  
   char *first, *last;  
public:  
        name() {}; 
        name(char *f, char *l): first (f), last (l){}
                 
        virtual void name::info(){
               
               name::info();
               Print();



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class person : public name {
char gender;  
int  ssn;

public:

   person() {};  

       person(char *f, char *l, char g, int s): gender(g), ssn(s), name::name(f, l) {}

   virtual void person::info() {
        name::info();
        person::info();
        Print();
          


Please advise what I am doing wrong.

Look at lines 10 and 13 in each code section respectively.
Topic archived. No new replies allowed.