Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,324 people online right now. Registration is fast and FREE... Join Now!
Please don't create two accounts, just to post the same issue. It is extremely disrespectful. Plus, when you post your code, please use
In any case, you have numerous errors.
I'm going to assume each of those classes is in it's own header. In any case, here is your code slightly reordered, and reformatted. I added comments (All towards the top) of some of your issues
cpp
#include <string> #include <iostream>
using namespace std;
class Person { public: string Name; int SSNum; bool Male;
enum Sex // Name of the enum. NOT a member variable name. { Male, // Name Conflict. You have bool Male, and enum Male Female };
// You are missing a ), you have Sex (Sex) at the end... Why? Person(string Name = "", int SSNum = 0, Sex (Sex) :Name(), SSNum(),Sex ()) // You specified Initilizers, but you didn't { // specify anything to initilze them with } };
class Faculty: public Person { public: string collegeName; double hourlySalary; int facultyID;
Faculty(string collegeName, double hourlySalary, int facultyID) : collegeName(collegeName), hourlySalary(hourlySalary), facultyID(facultyID) // You need to define the body of the constructor here
The problem here is that the class Faculty depends on the class Person. Yet, with the order you specified, the Person class is not defined before the Faculty class. IE, the compiler has no idea what your Person class is. To correct this, just include "Person.h" before "Faculty.h"
Once you correct all the above issues, I'll be able to further assists you.
This post has been edited by Cerolobo: 9 Jul, 2008 - 04:04 PM
OK, starting from the top, and working my way down
CODE
GetName();
Here, you have prototyped a function, but you did not specify the return type. You must specify the return type, for this to compile. From what I can tell, the original return type you specified was correct (string), since that's the type of your member variable, Name.
CODE
enum Sex{Male, Female };
Again, Sex is the name of the enum type you specified. For example
CODE
enum alpha { A, B, C, D, // ect... Z };
alpha foo;
Looking at the above code, the enum type is called alpha. IT IS NOT a variable. foo, on the other hand, is a variable. The type of foo is alpha, which is the enum listed. Going back to
CODE
enum Sex{Male, Female };
Sex is a enum type. It is not a variable. IE, you cannot access Sex like a member varible (which you attempt in the constructor).
CODE
Person(string (Name), int (SSNum = 0), Sex ():Name(John), SSNum(17423456),Sex (Male));
This one is all over the place. The proper way to declare a constructor, is as such
Looking at the above example, asdf is the name of the class. The constructor name is the same name of the class. For the parameters, the class takes in 2 parameters, int a and int b.
In the initializer list, the member variable A is initialized with the parameter a, that was passed in. B is initialized with parameter b.
The body is empty, since nothing else has to be done.
Going back to your code
CODE
Person(string (Name), int (SSNum = 0), Sex ():Name(John), SSNum(17423456),Sex (Male));
You did not correctly specify your parameter list. Plus, your initialize list appears to be mixed in with your parameters.
Sex () Sex (Male) Again, Sex is NOT A VARIABLE. It is a enum type. So, you cannot access it like a variable.
Name(John) Name is a string. John is nothing. I assume you wanted to initialize it with the string John, which will look like this
Name("John")
CODE
get.Name()
What is this supposed to do? The compiler and I have no idea. Please provide your logic behind this line.
You are also missing your closing "};" at the end of the class; however I'm willing to assume that was a copy/paste error.
In any case, try to correct the above, and I'll help your further.
That's a lot better, but you still have a few things you need to take care of
CODE
GetName(string);
You still haven't specified the return type.
A function is declared like so
CODE
<Return Type> <Function Name>(<Paramaters>);
So, to be more verbose
CODE
int asdf(int foo, int bar);
So, the name of the above function is asdf. It returns a int. It takes in two ints as parameters.
So,
CODE
GetName(string);
You are still missing your return type, and there really isn't a reason to take a parameter, for that function.
CODE
Person (string name, int num, string type): Name("John"), SSNum(164523456), type("Male")
OK, for the most part, this is correct.
First, you take in 3 parameters, but you don't do anything with them.
Looking at them, I assume you want to set your member variables to the values that were passed in. To do this, you need to replace the hard coded value with the ones that were passed in. This can be done is two ways
Initializer List
CODE
Person (string name, int num, string type): Name(name), SSNum(num), type(type)
or in the body of the constructor
CODE
Person (string name, int num, string type) { Name = name; SSNum = num; type = type; }
That way, the values that were passed in will be used.
There are two more issues you need to correct though. First, you should never have a parameter name that is the same as a member variable name, and the same capitalization.
Looking at the above code, Name and name are fine. As is SSNum and num. type, on the other hand, can be extremely confusing and cause you issues.
Other then that you have a mismatch between the parameter type, and the member variable type.
parameter type is a string. member variable type is a Sex.
So, you should change the parameter type to match the member variable.
Once you do that, you should be able to do something like this
Other then that, you still need to add something to the Faculty, Staff, and Student constructor.
Since you didn't specify any default values for the Person class, you have to call the constructor for Person from the initializer list in the Faculty, Staff, and Student constructor.
For example, take the Staff class
CODE
Staff(double WeeklySalary, int YearsOfService) : WeeklySalary(WeeklySalary), YearsOfService(YearsOfService)
That's good and all; however, since you are deriving your Staff class from the Person class, you have to call a constructor for the Person class. This will look like this
CODE
Staff(double WeeklySalary, int YearsOfService) : Person("John", 123456789, Person::Male), // <- Like This WeeklySalary(WeeklySalary), YearsOfService(YearsOfService)
Now, it doesn't really make sense to hard code the values in like that. So, you'll probably want to take in each of those parameters, in the constructor of Staff, and then pass those to the Person constructor. So, it'll be something like this
CODE
Staff(string name, int num, Sex type, double WeeklySalary, int YearsOfService) : Person(name, num, type), // <- Like This WeeklySalary(WeeklySalary), YearsOfService(YearsOfService)
You'll have to do that to all your derived classes.
After hardcoding it is giving my 19 errors. Most of the errors are this. Error 14 error C2065: 'cout' : undeclared identifier c:\documents and settings\administrator\my documents\visual studio 2005\projects\exam 1\exam 1\faculty.h 22 and Error 8 error C2327: 'Person::string' : is not a type name, static, or enumerator c:\documents and settings\administrator\my documents\visual studio 2005\projects\exam 1\exam 1\faculty.h 8
Attached is my code
CODE
#include <string> using namespace std;
class Person { public: string Name; int SSNum;
string GetName(); int GetSSNum(); enum Sex{Male, Female}; Sex Gender;
Person (string name, int num, sex type); { Name = name; SSNum = num; Gender = type;
}
};
#include <string> using namespace std;
class Faculty: public Person { public: string collegeName; double hourlySalary; int facultyID;
All right, you've just got a bunch of little errors
Two errors here
CODE
Person (string name, int num, sex type);
First, "sex" is not defined. In C/C++, capitalization matters. "sex" is not a type, but "Sex" is.
Second, you need to remove the semicolon at the end. The only time you add a semicolon at the end, is if you are only defining the function, but want to define the actual body somewhere else.
For example
File: foo.h
CODE
int foo(void); // Funciton Prototype
File: foo.cpp
CODE
#include "foo.h"
// The actual body is declared here int foo(void) { return 1; }
Since you have the body in the header, you do not need that semicolon.
CODE
Faculty(string cName, double money, int id):Person(name, num, type)
CODE
Student(double gpa = 0.0, int grades = 0):Person(name, num, type)
You have not defined "name", "num", or "type". You have take these variables in as parameters, or just hard code some value in (not recommended)
For the cout errors, cout is declared in iostream. You need to #include <iostream> in all the files that use it
CODE
Staff(string name, int num, Sex type, double wSalary, int yrsService):Person(name, num, type),
You have a extra comma at the end. You only add a comma if another initializer comes after the initializer you just specified.
Am I calling out my parameters wrong. Because now it is giving me seven errors. Ah.. I appreciate you teaching me. I have been learning a lot through this.
Here is my updated lines
CODE
Faculty(string cName, double money, int id):Person (string name, int num, Sex type)
And then my staff code
CODE
Staff(string name, int num, Sex type, double wSalary, int yrsService):Person (string name, int num, Sex type)