Inheritance Problems

Hello you guys...well I've been working on this project now for six days...I'm totally exhausted...can someone please look over this program and tell me how to correct the program in order for it to run......Thank You.......

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef Person_H
#define Person_H
#include <iostream>
#include <string>
#include < fstream>
using namespace std;

class Person
{
	string lname;
	string fname;
	char dept;
	int id;
public:
	Person();
	Person(string L, string F, char D, int N);
	void setL(string L);
	void setF(string F);
	void setD(char D);
	void setN(int N);
	string getL();
	string getF();
	char getD();
	int getN();
	void print();
};
class Employee:public Person
{
	float hour;
	float rate;
public:
	Employee();
	Employee(string L, string F, char D, int N, float hour, float rate);
	void setH(float h);
	void setR(float r);
	float getH();
	float getR();
	virtual float cal_salary();
	void print();

};
class Student:public Person // I WANT TO USE THIS ONE (IS IT CORRECT???)
{ 
	double gpa; 
	float credit;
public: 
	Student(string L, string F, float credit, int i = 0, double g = 0.0) 
	void print() 

#endif 

#include "Person.h"
Person::Person()
{
}
Person::Person(string L, string F, char D, int N)
{
	lname = L;
	fname = F;
	dept = D;
	id = N;
}
void Person::setF(string F) { fname = F; }
void Person::setL(string L) { lname = L; }
void Person::setD(char D) { dept = D; }
void Person::setN(int N) { id = N; }
string Person::getL() {return lname;}
string Person::getF() {return fname;}
char Person::getD() {return dept;}
int Person::getN() {return id;}
void Person::print()
{
	cout << fname <<"  "<< lname <<"  "<< dept <<"  "<< id;
}
Employee::Employee()
{
}
Employee::Employee(string L, string F, char D, int N, float hour, float rate)
{ 
	lname = L;
	fname = F;
	dept = D;
	id = N;
	hour = H;
	rate = R;
}
void Employee::setH(float H) { hour = H; }
void Employee::setR(float R) { rate = R; }
string Employee::getH() {return H;}
string Employee::getR() {return R;}
void Employee::print()
{
Person::print();
cout<<"  "<<
}
float Employee::cal_salary()
{
if (hour > 40)
return 40*rate + (hour-40)*rate*1.5;
else
hour *rate;

}
Student::Student()
{
}
Student::Student(string L, string F, int i = 0, double g = 0.0) : Person(L, F, i), gpa(g) {} 
{	
	lname = L;
	fname = F;
	dept = D;
	id = N;
    credit = C;
}
void Student::setC(float C) { credit = C; }
float Student::getC() { return C; }
void Student::print()
{ 
	Person::print(); 
	cout << "GPA: " << gpa << endl; } 
};

#include "Person.h"
void main() 
{

	const int STUDENTS = 3;		// Try changing this value

	Student aStudent[STUDENTS];	// Array of students 0..STUDENTS-1
	char aString[80];		// Temporary variable to store name
	double aDouble;			// Temporary variable to store GPA

					// Ask user for student data...

	for (int i=0; i < STUDENTS; i++)
	{
		cout << "\n\nStudent " << i+1 << endl;

		cout << "Enter Name: ";
		cin >> aString;
		aStudent[i].setFirstName(aString);

		cin >> aString;
		aStudent[i].setLastName(aString);

		cout << "Enter GPA : ";
		cin >> aDouble;
		aStudent[i].setGPA(aDouble);

					// ** Input and set additional variables here **

	}

					// Output all student data

	cout << "\n\nHere's all the data:\n";

	for (int j=0; j < STUDENTS; j++)
	{
		cout << "\n\nStudent " << j+1 << endl;
		aStudent[j].showInfo();
	}

	cout << "\nGOODBYE!\n";
	return 0;

}
Last edited on
Can you post the error messages?
With a quick view I can see:
Line 49: missing };
Line 94: missing semicolon
Line 101: Missing return
Line 124: should be int main
I've corrected your comments and now I get just this one....
Compiling...
Student.cpp
.\Student.cpp(46) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\darren\my documents\visual studio 2008\projects\person\person\Person.h(43)' was matched
Build log was saved at "file://c:\Documents and Settings\Darren\My Documents\Visual Studio 2008\Projects\Person\Person\Debug\BuildLog.htm"
Person - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Disregard the last message...corrected...here are some of the error messages...

.\Person.cpp(30) : error C2248: 'Person::lname' : cannot access private member declared in class 'Person'
Person.cpp(31) : error C2248: 'Person::fname' : cannot access private member declared in class 'Person'
Person.cpp(32) : error C2248: 'Person::dept' : cannot access private member declared in class 'Person'
Person.cpp(33) : error C2248: 'Person::id' : cannot access private member declared in class 'Person'
Person.cpp(34) : error C2065: 'H' : undeclared identifier
.\Person.cpp(35) : error C2065: 'R' : undeclared identifier
.\Person.cpp(39) : error C2556: 'std::string Employee::getH(void)' : overloaded function differs only by return type from 'float Employee::getH(void)'
1
2
3
4
5
6
7
class Person
{
    protected: // make members accessible by derived classes
	string lname;
	string fname;
	char dept;
	int id;


1
2
3
4
5
6
7
8
9
Employee::Employee(string L, string F, char D, int N, float hour, float rate) // hour, rate
{ 
	lname = L;
	fname = F;
	dept = D;
	id = N;
	hour = H; // Not matching the parameter name
	rate = R; // same here
}


Declaration:
float getH();
Definition: -different return types-
string Employee::getH() {return H;}
Same for getR




ok...I've made those correction but I still get errors from H and R and some more.....

float Employee::getH() {return H;}//Line 39
float Employee::getR() {return R;}//Line 40
void Employee::print()
{
Person::print();
cout<<" "<<
}//Line 45

hour*rate;//Line 51

Student::Student(string L, string F, int i = 0, double g = 0.0) : Person(L, F, i), gpa(g) {} //Line 57
{
lname = L;
fname = F;
dept = D;
id = N;
credit = C;
}

void Student::print()
{
Person::print();
cout << "GPA: " << gpa << endl;
} //Line 72

Person.cpp(39) : error C2065: 'H' : undeclared identifier
.\Person.cpp(40) : error C2065: 'R' : undeclared identifier
.\Person.cpp(45) : error C2059: syntax error : '}'
.\Person.cpp(51) : warning C4552: '*' : operator has no effect; expected operator with side-effect
.\Person.cpp(57) : error C2511: 'Student::Student(std::string,std::string,int,double)' : overloaded member function not found in 'Student'
.\Person.cpp(72) : fatal error C1004: unexpected end-of-file found
Oh yeah...by the way...Thanks for all the help
1
2
float Employee::getH() {return H;}//Line 39
float Employee::getR() {return R;}//Line 40 

Class Employee doesn't have members called H or R, you have hour and rate

You still have some problems I pointed two posts ago: http://www.cplusplus.com/forum/general/16342/#msg81535
Line 94: missing semicolon (won't work with or without it)...
.\Person.cpp(44) : error C2059: syntax error : ';'

Well all is corrected except for these three errors...
Person.cpp(44) : error C2059: syntax error : ';'
Person.cpp(58) : error C2511: 'Student::Student(std::string,std::string,int,double)' : overloaded member function not found in 'Student'
Person.cpp(73) : fatal error C1004: unexpected end-of-file found
Line 94: missing semicolon (won't work with or without it)...
.\Person.cpp(44) : error C2059: syntax error : ';'
You also have an extra <<
Person.cpp(58) : error C2511: 'Student::Student(std::string,std::string,int,double)' : overloaded member function not found in 'Student'

Try removing the default arguments value from the implementation
I've tried every thing possible...removing...renaming...adding...changing...all failed...all that's left are still these two errors...

Person.cpp(58) : error C2511: 'Student::Student(std::string,std::string,int,double)' : overloaded member function not found in 'Student'
Person.cpp(71) : fatal error C1004: unexpected end-of-file found

Could there be a way possible for me to implement this class....???(would it be better than the one I'm using now...I also tried this one but ended up with more errors than the latter)

class Student : public Person
{
private:
double gpa;
public:
Student(string n, int i = 0, double g = 0.0) : Person(n, i), gpa(g) {}
void display()
{
Person::display();
cout << "GPA: " << gpa << endl; }
};
Well you can't have done everything?

The first error is saying that in person.cpp you have defined a member function of the Student class, that takes parameters, string, string, int, double.
Have you declared such a function in the Student class - or
is the one you declared in the student class string, int, double. ???????

For the second error:
unexpected end-of-file found can be reported when you haven't matched up your { and } brackets properly - so a function isn't properly closed.
your efforts come with full code and working examples! i like that. i will throw it in my compiler.

make your "Student:public Person" match the other two classes in Person.h.
1
2
3
4
5
6
7
8
9
10
11
12
class Student:public Person // I WANT TO USE THIS ONE (IS IT CORRECT???)
{
protected: // we don't derive from this, but what the hell. thanks Bazzy!
	double gpa; 
	float credit;
public: 
	Student(); // other ones have this. in case you create a new 
	           //   Student without adding parameters?
	// what is i and g for? doesn't matter since you never call it?
	Student(string L, string F, float credit, int i = 0, double g = 0.0);
	void print();
}

match your class definition with your class code. your constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Student::Student(string L, string F, int i = 0, double g = 0.0) : Person(L, F, i), gpa(g) {}
// i have no idea what that stuff on the end is supposed to do here
//   note: i am beginner too! fun stuff.
// default parameters here and in the Student class at the same time 
//   cause a redefinition error. i had no idea.
// Employee::Employee keep Person's D and N in their constructor
//  and add hour and rate.
Student::Student(string L, string F, int i, double g) // this should be enough
{	
	lname = L;
	fname = F;
	// dept = D; // you don't have a D here
	// id = N; // you don't have an N here
	// credit = C; // you don't have a C here
}

this constructor is never used, as far as i know. you could get rid of it here and in the class definition. i won't tell anyone.

once you get past the syntax errors, like missing ; and }, you still have other mismatches. aStudent[i].setGPA(aDouble) has no Student::setGPA(double aDouble) or or a setGPA(double aDouble) in the Student class. same thing for SetFirstName and SetLastName. Student::getC, Student::setC. all those lonely functions.

you may run into some odd output if you call Person::print() without giving a value to dept and id.

your Person::print() cout line is missing stuff at the end. your Employee::cal_salary() is missing a return. Employee::getH doesn't have an H and Employee::getR doesn't have an R.

other than that, it works fine.
THANK YOU SO MUCH FOR ALL YOUR HELP...IT WAS NICE WORKING WITH YOU...YOU KEPT ME FOCUS AND DETERMINED NOT TO GIVE UP....I FOUND ANOTHER WORKING EXAMPLE BECAUSE OF YOU...IF YOU WOULD LIKE TO SEE WHAT I PUT TOGETHER JUST LEAVE ME A MESSAGE HERE...LOL
YES! PM OR POST WHAT WORKED! I'M INTO THAT SORT OF THING!

 
Student::Student(string L, string F, int i = 0, double g = 0.0) : Person(L, F, i), gpa(g) {}

oh! this is supposed to be a derived class constructor calling one of your base constructors to get Person::Person to initialize its data. that way, your call to Person::print wouldn't output strange nonsense for D and N.

like:
 
Student::Student(string L, string F, char D, int N, int i, double g) : Person(L, F, D, N)


similarly:
 
Employee::Employee(string L, string F, char D, int N, float hour, float rate)

could be:
 
Employee::Employee(string L, string F, char D, int N, float hour, float rate) : Person(L, F, D N)


you would have to call your special Student and special Employee constructors with the right number of arguments.
Topic archived. No new replies allowed.