I have modified your latest post below. I don't have a compiler available so I can't verify
accuracy of modifications. They are offered as recommendations in terms of what I would try.
Note the following changes:
1) I use initialization lists wherever possible
2) Don't pass in parameters with same name as variables. Here I preceded all parameter names
with an underscore, a pretty common practice, to differentiate them.
3) When using static variables you have the same access options and requirements as with
non-static variables. In your case you declared count and maxCount as private. Therefore,
to access/mutate these variables you need public accessor/mutator functions to access/change
these variables outside the class. In your case you will change the values of the count
variables and maxCount outside the class using Initialize() and access them using PrintCounts().
I think trying to assign values to a function accounts for all the error messages you had
posted. I think the following syntax will fix that.
4) Every time you call a constructor of a derived class you will call a constructor of the
base class. Here, both Student and Employee are derived from the Person class. Only the
Person class needs maxCount, because Person count will include both Student and Employee counts,
and since Person is abstract, you can't declare a Person outright. The Person constructor will
only be called by a constructor of a derived class.
5) I'm pretty sure enum and bool variables will print out as an int and bool variables. So I added
code to convert the int into a string so it makes sense when printed.
6) For now I wouldn't worry about the issue regarding what to do if count exceeds maxCount. I added a line to exit the program if count exceeds maxCount. This isn't the best way to deal with this issue, see discussion regarding wrapper classes for a better idea. However, this seems a minor
issue compared with learning about the other issues. Therefore, I elected to just exit before
completing the constructor.
7) If a member variable/function is referred to within a class I don't prefix it with the
scope operator. If a member variable/function is referred to outside a class (for example,
static member initialization/printing,) you need to use the scope resolution operator.
8) As long as Print() is a member function, you really don't need to use accessor functions
like Age(), Name(), etc; you can access the private variables directly within Print().
9) Declaring an enumerated constant doesn't require use of keyword typedef. Syntax changed.
10) In the future I suggest you limit your class declarations to one class per header file.
I think you'll find that to be more flexible.
Code:
///////////
//school.h
///////////
#include <iostream>
using namespace std;
enum SEX {male, female};
class Person {
private:
static int count;
static int maxcount;
string name;
int age;
int years_in_college;
SEX sex;
public:
Person(string, int, int, SEX);
~Person();
static void setCount(int i) {count = i;}
static void setMaxCount(int max) {maxCount = max};
static int getCount() {return count;}
static int getMaxCount() {return maxCount;}
virtual void Print() = 0;
string Name() { return name; }
int Age() { return age; }
int Years_in_college() { return years_in_college; }
SEX Sex();
};
class Student : public Person {
private:
string major;
bool transfer;
static int count;
public:
Student(string, int, int, SEX, string, bool);
~Student();
static void setCount(int i) {count = i;}
static int getCount() { return count;}
void Print();
string Major() { return major; }
bool Transfer();
};
class Employee : public Person {
private:
string department;
static int count;
public:
Employee(string name, int age, int years_in_college, SEX sex, string department);
~Employee();
static void setCount(int i) {count = i;}
static int getCount();
void Print();
string Department() { return department; }
};
void Initialize(int max);
void PrintCounts();
///////////////////////
//school.cpp
///////////////////////
#include "school.h"
/************************************************** ****************
*
* Person Abstract CLass
*
************************************************** ******************/
Person::Person(string _name, int _age, int _years_in_college, SEX _sex) : name(_name),
age(_age),
years_in_college(_years_in_college),
sex(_sex)
{
if (count >= maxCount)
{
cerr << "Error: Can't Add Any More People, MAX has been reached." << endl;
exit(1);
}
else
++count;
}
Person::~Person()
{
--count;
}
/************************************************** ****************
*
* Student is a Person
*
************************************************** ******************/
Student::Student(string _name, int _age, int _years_in_college, SEX _sex,
string _major, bool _transfer) : Person(_name, _age, _years_in_college, _sex),
major(_major), transfer(_transfer)
{
++count;
}
Student::~Student()
{
--count;
}
void Student::Print()
{
cout << "Info:\n";
cout << "\tName: " << Name() << endl;
cout << "\tAge: " << Age() << endl;
cout << "\tYears in college: " << Years_in_college() << endl;
cout << "\tSex: " << Sex() << endl;
cout << "\tMajor: " << Major() << endl;
cout << "\tTransfer: " << Transfer() << endl;
}
string Student::Sex()
{
if(sex == male)
return "male";
else
return "female";
}
string Student::Transfer()
{
if(transfer)
return "true";
else
return "false";
}
/************************************************** ****************
*
* Employee is a Person
*
************************************************** ******************/
Employee::Employee(string _name, int _age, int _years_in_college, SEX _sex,
string _department) : Person(_name, _age, _years_in_college, _sex),
department(_department)
{
++count;
}
Employee::~Employee()
{
--count;
}
void Employee::Print()
{
cout << "Info:\n";
cout << "\tName: " << Name() << endl;
cout << "\tAge: " << Age() << endl;
cout << "\tYears in college: " << Years_in_college() << endl;
cout << "\tSex: " << Sex() << endl;
cout << "\tDepartment: " << Department() << endl;
}
string Employee::Sex()
{
if(sex == 0)
return "male";
else
return "female";
}
/****************************************
*
* Global Functions
*
*****************************************/
void Initialize(int max)
{
Person::setMaxCount(max);
Person::setCount(0);
Student::setcount(0);
Employee::setCount(0);
}
void PrintCounts()
{
cout << "Person: " << Person::getCount() << endl;
cout << "Student: " << Student::getCount() << endl;
cout << "Employee: " << Employee::getCount() << endl;
}
// Here put the definitions of the static "count" variables
int Person::count;
int Person::maxcount;
int Student::count;
int Employee::count;