I'm still confused about the Person constructor, when you create an instance of another type of person you have to handle it in the person constructor?
Code:
Person::Person(string name, int age, int years_in_college, SEX sex)
{
if (Count() >= MaxCount()) {
cerr << "Error: Can't Add Any More People, MAX has been reached." << endl;
}
else
//what keeps count of everything ?
Count() += 1;
}
I know some people made post about using a wrapper class/etc. Again I haven't gotten that advanced in our class to use that yet. We've only learned a few things for C++.
The main program has problems trying to compile, I get errors such as
Code:
: undefined reference to `Person::Count()'
./libschool.a(school.o)(.text+0x2b): In function `Person::Person[not-in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX)':
: undefined reference to `Person::MaxCount()'
./libschool.a(school.o)(.text+0x63): In function `Person::Person[not-in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX)':
: undefined reference to `Person::Count()'
./libschool.a(school.o)(.text+0xb9): In function `Person::Person[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX)':
: undefined reference to `Person::Count()'
./libschool.a(school.o)(.text+0xc1): In function `Person::Person[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX)':
: undefined reference to `Person::MaxCount()'
./libschool.a(school.o)(.text+0xf9): In function `Person::Person[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX)':
: undefined reference to `Person::Count()'
./libschool.a(school.o)(.text+0x208): In function `Student::Student[not-in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)':
: undefined reference to `Student::Count()'
./libschool.a(school.o)(.text+0x2d8): In function `Student::Student[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool)':
: undefined reference to `Student::Count()'
./libschool.a(school.o)(.text+0x62c): In function `Employee::Employee[not-in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `Employee::Count()'
./libschool.a(school.o)(.text+0x6f6): In function `Employee::Employee[in-charge](std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int, SEX, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `Employee::Count()'
./libschool.a(school.o)(.text+0x98b): In function `Initialize(int)':
: undefined reference to `Person::MaxCount()'
./libschool.a(school.o)(.text+0x997): In function `Initialize(int)':
: undefined reference to `Person::Count()'
./libschool.a(school.o)(.text+0x9a2): In function `Initialize(int)':
: undefined reference to `Student::Count()'
./libschool.a(school.o)(.text+0x9ad): In function `Initialize(int)':
: undefined reference to `Employee::Count()'
./libschool.a(school.o)(.text+0x9cf): In function `PrintCounts()':
: undefined reference to `Person::Count()'
./libschool.a(school.o)(.text+0xa0e): In function `PrintCounts()':
: undefined reference to `Student::Count()'
./libschool.a(school.o)(.text+0xa4d): In function `PrintCounts()':
: undefined reference to `Employee::Count()'
collect2: ld returned 1 exit status
school.cpp
Code:
#include "school.h"
/******************************************************************
*
* Person Abstract CLass
*
********************************************************************/
Person::Person(string name, int age, int years_in_college, SEX sex)
{
this->name = name;
this->age = age;
this->years_in_college = years_in_college;
this->sex = sex;
if (Count() >= MaxCount()) {
cerr << "Error: Can't Add Any More People, MAX has been reached." << endl;
}
else
Count() += 1;
}
Person::~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)
{
name = name;
age = age;
years_in_college = years_in_college;
sex = sex;
this->major = major;
this->transfer = transfer;
Student::Count() += 1;
}
Student::~Student()
{
--Student::count;
}
void Student::Print()
{
cout << "Info:\n";
cout << "\tName: " << Name() << endl;
cout << "\tAge: " << Age() << 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;
}
/******************************************************************
*
* 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)
{
name = name;
age = age;
years_in_college = years_in_college;
sex = sex;
this->department = department;
Employee::Count() += 1;
}
Employee::~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;
}
/****************************************
*
* Global Functions
*
*****************************************/
void Initialize(int max)
{
Person::MaxCount() = max;
Person::Count() = 0;
Student::Count() = 0;
Employee::Count() = 0;
}
void PrintCounts()
{
cout << "Person: " << Person::Count() << endl;
cout << "Student: " << Student::Count() << endl;
cout << "Employee: " << Employee::Count() << endl;
}
// Here put the declarations of the static "count" variables
int Person::count;
int Person::maxcount;
int Student::count;
int Student::maxcount;
int Employee::count;
int Employee::maxcount;
school.h
Code:
#include <iostream>
using namespace std;
typedef enum {male, female} SEX;
class Person {
private:
static int count;
static int maxcount;
string name;
int age;
int years_in_college;
SEX sex;
public:
Person(string name, int age, int years_in_college, SEX sex);
~Person();
virtual void Print() = 0;
static int& Count();
static int& MaxCount();
string Name() { return name; }
int Age() { return age; }
int Years_in_college() { return years_in_college; }
SEX Sex() { return sex; }
};
class Student : public Person {
private:
string major;
bool transfer;
static int count;
static int maxcount;
public:
Student(string name, int age, int years_in_college, SEX sex, string major, bool transfer);
~Student();
static int& Count();
static int& MaxCount();
void Print();
string Major() { return major; }
bool Transfer() { return transfer; }
};
class Employee : public Person {
private:
string department;
static int& count;
static int& maxcount;
public:
Employee(string name, int age, int years_in_college, SEX sex, string department);
~Employee();
static int& Count();
static int& MaxCount();
void Print();
string Department() { return department; }
};
void Initialize(int max);
void PrintCounts();
I've added the required code that has been posted on here and this is my latest source code trying to run test1.cpp
test1.cpp
Code:
#include "school.h"
int main()
{
Initialize(10); // No more than 10 people
// Plus 3
Student *sp1 = new Student("Joe Smith", 20, 2, male, "CS", false);
Student *sp2 = new Student("Mike Jones", 20, 2, male, "CS", true);
Student *sp3 = new Student("Hannah Reed", 21, 3, male, "CS", false);
// Minus 3
delete sp1;
delete sp2;
delete sp3;
Student s1("Jules Adams", 21, 1, male, "CS", false);
Student s2("Kelly Kennedy", 22, 4, female, "Psychology", false);
Employee e1("Rick James", 18, 2, male "Math");
Employee e1("John Doe",54, 5, female, "Pols");
PrintCounts();
s1.Print();
st1.Print();
return 0;
}