PDA

View Full Version : C++ Inheritance, Class help




sixstorm
Feb 6, 2007, 08:07 PM
Hello again fellow programmers. I'm taking a part two class of C++ for those who remember me last semester.

I've finally understood the concepts of classes fairly well, but I'm not understanding why my program is not working. Before I spill code, know that the method used is as followed:

* Header File
* Implementation File
* Test file (test methods)

XCode isn't friendly and will not let me have 'manager.h' and 'manager.cpp' in the same project. Oh well. I think my problem deals with Inheritance between two header files 'employee.h' and 'manager.h'.

Here's the implementation file:
// manager.cpp // implements Manager class
// 01/29/2007

#include <iostream>
#include <string>

#include "manager.h" // include declaration of Manager class
#include "employee.h"

using namespace std;

Manager::Manager(int theId, string theName)
{
// Initialize fields inherited from parent class Employee

// Initialize fields of class Manager

hrsWorked = 0;

for(int i=0; i<5; i++)
hours[i] = 0;
}

void Manager::setHours(int theHours[])
{
for(int i=0; i<5; i++)
{
hours[i] = theHours[i];
hrsWorked += hours[i];
}
}

int Manager::getTotalHours()
{
return hrsWorked;
}

void Manager::setRate(double payRate)
{
ratePerHour = payRate;
}

double Manager::getRate()
{
return ratePerHour;
}

void Manager::setBonus(double bonus)
{
overtimePay = bonus;
}

double Manager::getBonus()
{
return overtimePay;
}

void Manager::calcSalary()
{
salary = hrsWorked * ratePerHour + overtimePay;
}

void Manager::printInfo()
{
cout << "*********************************" << endl;
cout << "Hours Worked: " << hrsWorked << endl;
cout << "Rate per Hour: " << ratePerHour << endl;
cout << "Overtime Pay: " << overtimePay << endl;
cout << "Total Salary: " << salary << endl;
cout << "*********************************" << endl;
}

Header File 'manager.h'

// manager.h // declares Manager class
// 01/29/2007

#ifndef MANAGER_H
#define MANAGER_H

#include <string>
#include "employee.h" // include declaration of Employee class

using namespace std;

class Manager : public Employee
{
protected:
int hours[5]; // number of hours worked per day
int hrsWorked; // total hours worked
double ratePerHour; // pay rate per hour
double overtimePay; // bonus

public:

// Postcondition: this name has been initialized from the given parameters
Manager(int theId, string theName);

// Postcondition: the number of hours worked per day has been set from the given array.
// the total hours worked has been computed.
void setHours(int theHours[]);

// Precondition: the hoursWorked has been computed.
// Postcondition: returns the total number of hours worked.

int getTotalHours();

// Postcondition: the pay rate per hour has been set from the given value.

void setRate(double payRate);

// Postcondition: returns the payRate.

double getRate();

// Postcondition: the bonus pay has been set.

void setBonus(double bonus);

// Postcondition: returns the bonus.

double getBonus();

// Postcondition: the salary has been computed.

void calcSalary();

// Postcondition: All information about this employee has been displayed.

void printInfo();

// For Extra Credits
// Postcondition: The current manager (calling object) contains a copy of otherManager.
// void operator= (const Manager& otherManager);

// if you would allow the assignment like a = b = c; use the following solution
// const Manager& operator= (const Manager& otherManager);


}; // Manager

#endif


Finally, the test file 'testmanager.cpp'

// testmanager.cpp
// Jonathan Andrew Scott
// CSCI 2010
// 2/5/2007

#include <iostream>
#include "manager.h"
using namespace std;

int main()
{
int hrsMgr1[5] = {5, 6, 7, 3, 6};
int hrsMgr2[5] = {4, 5, 4, 7, 5};

Manager mgr1(101, "Andrew Scott");
Manager mgr2(102, "Kyle Gregory");

cout << "Test all methods \n";
mgr1.setHours(hrsMgr1);
mgr1.setRate(7.00);
mgr1.setBonus(150.00);
mgr1.calcSalary();

cout << "ID: " << mgr1.getID() << endl;
cout << "Name: " << mgr1.getName() << endl;
cout << "Total Hours Worked Per Week: " << mgr1.getTotalHours() << endl;
cout << "Hourly Rate of Pay: " << mgr1.getRate() << endl;
cout << "Bonus Pay This Week: " << mgr1.getBonus() << endl;
cout << "Gross Pay: " << mgr1.getSalary() << endl;
cout << endl;

return 0;
}

Sorry for the indentations but you can thank the forum for that. :D Any suggestions would be appreciated.



jsw
Feb 6, 2007, 08:10 PM
Would you mind:

(1) editing your post to use tags instead of tags (and paste the code in again) [that will preserve indents], and

(2) let us know what your problem is (won't compile?) - sorry, I'm just not clear on what you're asking

Thanks!

sixstorm
Feb 6, 2007, 08:34 PM
Would you mind:

(1) editing your post to use tags instead of tags (and paste the code in again) [that will preserve indents], and

(2) let us know what your problem is (won't compile?) - sorry, I'm just not clear on what you're asking

Thanks!

Fixed on #1.

#2, I cannot get it to compile. The errors that it's giving are "manager is not defined in this scope" and my methods are returning invalid (undefined reference).

dcr
Feb 7, 2007, 02:05 AM
I think you'll find the answer here:

http://www.apsu.edu/lij/csci2010.html

under the heading

"Academic Misconduct"

You are required to turn in your own work. Collaboration on assignments and exams is prohibited, unless otherwise specified by the instructor. Likewise, plagiarism of other's work or web-related sources constitutes as a serious infraction. A penalty will be enforced for participating in academic dishonesty. Violation of academic integrity will result in a grade of 0 being assigned for the work involved or and referral to the proper university officials.

pilotError
Feb 7, 2007, 07:32 AM
You didn't post the employee code.

sixstorm
Feb 7, 2007, 09:09 AM
I think you'll find the answer here:

http://www.apsu.edu/lij/csci2010.html

under the heading

"Academic Misconduct"

You are required to turn in your own work. Collaboration on assignments and exams is prohibited, unless otherwise specified by the instructor. Likewise, plagiarism of other's work or web-related sources constitutes as a serious infraction. A penalty will be enforced for participating in academic dishonesty. Violation of academic integrity will result in a grade of 0 being assigned for the work involved or and referral to the proper university officials.

We get to use his "starter files" in our assignments. Like we have to add certain methods and all that good stuff so it's legit.

SamMiller0
Feb 7, 2007, 09:26 AM
please post employee.h


samm@joule:~/code$ g++ -c -o manager.o manager.cpp
In file included from manager.cpp:7:
manager.h:8:65: error: employee.h: No such file or directory
manager.h:13: error: expected class-name before ‘{’ token
manager.cpp: In member function ‘void Manager::calcSalary()’:
manager.cpp:60: error: ‘salary’ was not declared in this scope
manager.cpp: In member function ‘void Manager::printInfo()’:
manager.cpp:69: error: ‘salary’ was not declared in this scope

iMeowbot
Feb 7, 2007, 10:05 AM
#2, I cannot get it to compile. The errors that it's giving are "manager is not defined in this scope" and my methods are returning invalid (undefined reference).

Using the three files you posted at the top of this thread and adding the employee.h and employee.cpp from this page (http://www.apsu.edu/lij/07s2010/hw/a2.htm), it all compiles OK here with one warning (hrsMgr2 on line 13 of testmanager is unused). I'm using g++ -Wall -o testmanager testmanager.cpp manager.cpp employee.cpp to build.

I didn't bother setting up an Xcode project for this, just built and ran from the shell. For simple projects like this one, that's probably a good way to go. The output from the program looks incomplete (name is empty, for example), but I assume you're not finished yet.

mward333
Feb 7, 2007, 10:19 AM
It might be helpful, when posting questions from a university course, to let us know what the professor allows and disallows.

You pointed out that the professor provided a few files to get you started. OK!

Nonetheless, I'm still troubled by the quote: "Collaboration on assignments and exams is prohibited, unless otherwise specified by the instructor."

Can you clarify for us here? Maybe collaboration is allowed on this assignment for some reason?

(Sorry for trolling here....)

bousozoku
Feb 7, 2007, 10:52 AM
As far as I'm concerned, as long as people attending a class are doing their own work and only need an extra set of eyes to see what they're doing wrong, it's not a violation of anything.

I'd say that the word "collaboration" needs to be further defined if people are going to consider asking for someone to look at code collaboration. Students in the same class often do it and programmers in the real world certainly do it.

I disagree with coming to MacRumors to ask for someone else to do the assignment and someone else providing the code. It hurts that person and their future employers and usually, their co-workers.

Doctor Q
Feb 7, 2007, 11:01 AM
Although MacRumors is not in a position to judge academic rules, we do ask that our student members and those who help them observe the spirit of such rules.

It's hard to give precise definitions of "collaboration" and "plagiarism" in this context but we can start by distinguishing between giving advice on programming in general and posting of complete ready-to-use code.

It may be perfectly fine under the assignment's rules for sixstorm to have questions answered about the meaning of a "not declared in this scope" error and what to do about it, without using code written by others. But making that interpretation is up to sixstorm and his professor.

sixstorm
Feb 7, 2007, 11:18 AM
All I've asked around here was basically an extra set of eyes. My class has jumped into C++ and a lot of more advanced concepts that I don't completely understand yet. Heck, that's why I'm in class I guess lol.

employee.h (http://www.apsu.edu/lij/07s2010/hw/employee.h)
employee.cpp (http://www.apsu.edu/lij/07s2010/hw/employee.cpp)

I've already turned in the project but I still need to understand what I've been doing wrong. Trust me, I've been looking this code over for a few days but still can't get it to compile. Any suggestions or hints would really help.

iMeowbot
Feb 7, 2007, 11:27 AM
I've already turned in the project but I still need to understand what I've been doing wrong. Trust me, I've been looking this code over for a few days but still can't get it to compile. Any suggestions or hints would really help.

As before, it compiles just fine for me. The problem, it seems, must be in the way you are trying to build the project. What steps are you using to build the program? Are you required to use the Xcode system to build?

=-=-=-=

Adding: this worked fine for me in Xcode. Here's exactly how I built it.

First, I created a new project, using C++ Tool under the Command Line Utility section.

In the project window, I dragged the three .cpp and two .h files from the Finder over to the Source folder icon in Xcode, and told it to copy the files (top checkbox - everything else in the dialog was left at the defaults). Next, I removed the stock main.cpp file because it wasn't needed, told it to get rid of both files and references when it asked.

That was it. Build and Go, using the files you supplied unmodified, compiled and ran (with the one unused variable warning I mentioned earlier).

sixstorm
Feb 7, 2007, 12:21 PM
As before, it compiles just fine for me. The problem, it seems, must be in the way you are trying to build the project. What steps are you using to build the program? Are you required to use the Xcode system to build?

=-=-=-=

Adding: this worked fine for me in Xcode. Here's exactly how I built it.

First, I created a new project, using C++ Tool under the Command Line Utility section.

In the project window, I dragged the three .cpp and two .h files from the Finder over to the Source folder icon in Xcode, and told it to copy the files (top checkbox - everything else in the dialog was left at the defaults). Next, I removed the stock main.cpp file because it wasn't needed, told it to get rid of both files and references when it asked.

That was it. Build and Go, using the files you supplied unmodified, compiled and ran (with the one unused variable warning I mentioned earlier).

Ok I'll give it a try here soon. I don't have XCode on my laptop yet. I have been using the school's linux server to code and compile. So basically, I compile like this: "g++ employee.h manager.h manager.cpp testmanager.cpp"

I'll give your suggestion a shot this afternoon though, hopefully it might work. Last time I tried XCode with this project, I did what you did but kept getting a message asking me if I wanna replace "manager.h" with "manager.cpp". Oh well.

iMeowbot
Feb 7, 2007, 12:30 PM
So basically, I compile like this: "g++ employee.h manager.h manager.cpp testmanager.cpp"

Oh, there's your problem, you aren't including employee.cpp in your compile command. You also can leave out the *.h files, because #include in your source files will take care of that.

What you want is:

g++ -o whatever employee.cpp manager.cpp testmanager.cpp

jsw
Feb 7, 2007, 01:00 PM
I think you'll find the answer here:

http://www.apsu.edu/lij/csci2010.html
It would seem that no help with the code itself was being sought. Instead, there was a question as to why it wasn't being compiled within Xcode.

So I see no violation.

As a personal note, I find it odd that academia discourages collaboration when companies actively encourage it.

sixstorm
Feb 7, 2007, 01:00 PM
Oh, there's your problem, you aren't including employee.cpp in your compile command. You also can leave out the *.h files, because #include in your source files will take care of that.

What you want is:

g++ -o whatever employee.cpp manager.cpp testmanager.cpp

May I ask what the -o does?

Our professor told us to include the header files when compiling . . . so whatever. I'm not out of class yet but I'll definitly try it when I get home. Thanks!

iMeowbot
Feb 7, 2007, 01:26 PM
May I ask what the -o does?

You use that to specify where the finished program should go, so -o whatever leaves you with an executable program named whatever.

Our professor told us to include the header files when compiling . . . so whatever.
It makes sense to add include paths (with -I) when your headers and other sources aren't in the same directory, but putting the individual includes on the command line doesn't do anything useful. The preprocessor takes care of that with #include, pulling the headers right into your .cpp files at compile time.

sixstorm
Feb 7, 2007, 02:19 PM
Adding: this worked fine for me in Xcode. Here's exactly how I built it.

First, I created a new project, using C++ Tool under the Command Line Utility section.

In the project window, I dragged the three .cpp and two .h files from the Finder over to the Source folder icon in Xcode, and told it to copy the files (top checkbox - everything else in the dialog was left at the defaults). Next, I removed the stock main.cpp file because it wasn't needed, told it to get rid of both files and references when it asked.

That was it. Build and Go, using the files you supplied unmodified, compiled and ran (with the one unused variable warning I mentioned earlier).

Ok, I just tried what you suggested and I'm getting the same errors:

In testmanager.cpp:
"Manager not declared in this scope"
"mgr1 not declared in this scope"
"mgr2 not declared in this scope"
Two ';' errors - Seems like it can't read from the manager header file.

jeremy.king
Feb 7, 2007, 02:33 PM
Ditch the IDE until you understand the language, it seems to be getting in your way. Are they teaching this course with an IDE?

iMeowbot
Feb 7, 2007, 02:42 PM
Ok, I just tried what you suggested and I'm getting the same errors:

In testmanager.cpp:
"Manager not declared in this scope"
"mgr1 not declared in this scope"
"mgr2 not declared in this scope"
Two ';' errors - Seems like it can't read from the manager header file.

The compiler will also have told you the files and lines where those errors happened. Where are they?

Are you 100% certain that you are using exactly the same source code versions that you posted here (and linked here for the employee.* files)?

sixstorm
Feb 7, 2007, 02:52 PM
The compiler will also have told you the files and lines where those errors happened. Where are they?

Are you 100% certain that you are using exactly the same source code versions that you posted here (and linked here for the employee.* files)?

Ok, I recopied what I had listed above and I'm finally getting a successful build. I think I might have tried something this morning in the computer lab before turning it in. At least I know it compiles though . . .

Thanks for your help guys! I really appreciate it.