Abstract classes

This is a discussion on Abstract classes within the C++ Programming forums, part of the General Programming Boards category; I have modified your latest post below. I don't have a compiler available so I can't verify accuracy of modifications. ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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;
    You're only born perfect.

  2. #17
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Thank you very much, after many hours reading and debugging I finally got the system working. The problem know is that I have variables count and maxcount for each of the classes, I have to create a dean and chancellor which have 1 new member each, problem with that is I have to check if there are some created we can't instaiante more of them. There can only be 6 deans and 1 chancellor, once they have been created, they shouldn't be allowed to be created again. If the user tries to, they will get an error of the number of chancellors being created.

    Code:
    Dean::Dean(string name, int age, int years_in_college, SEX sex, string department, LEVEL level, DIVISION division)
      : Faculty(name, age, years_in_college, sex, department, level)
    {
        name = name;
        age = age;
        years_in_college = years_in_college;
        sex = sex;
        department = department;
        level = level;
        this->division = division;
    
        Dean::Count()++;
    
        if( Dean::Count() > Person::MaxCount() || Dean::Count() > 7 )
        {
            cout << "Tried to make " << Dean::Count() << " Dean" << endl;
            exit(1);
        }
    
    }
    
    Dean::~Dean()
    {
        --Dean::count;
    }
    
    void Dean::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;
        cout << "\tLevel: " << Level() << endl;
        cout << "\tDivision: " << Division() << endl;
    }
    
    Chancellor::Chancellor(string name, int age, int years_in_college, SEX sex, string department, LEVEL level, bool cameFromUCSB)
      : Faculty(name, age, years_in_college, sex, department, level)
    {
        name = name;
        age = age;
        years_in_college = years_in_college;
        sex = sex;
        department = department;
        level = level;
        this->cameFromUCSB = cameFromUCSB;
    
        Chancellor:Count()++;
    
        if( Chancellor::Count() > Person::MaxCount() || Chancellor::Count() != 1 )
        {
            cout << "Tried to make " << Chancellor::Count() << " Chancellor" << endl;
            exit(1);
        }
    }
    
    Chancellor::~Chancellor()
    {
        --Chancellor::count;
    }
    Last edited by cisokay; 05-28-2005 at 12:10 PM.

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I assume you want

    If number of persons greater than max number of persons allowed OR if number of deans greater than number of deans allowed then notify user and exit program

    which would translate to something like this:

    if(Person::count > Person::maxCount || Dean::count > Dean::maxCount)
    cout << "too many people or too many Deans" << endl;
    exit(1);

    Using a wrapper class (or some other code) to be more polite to user rather than just exiting program would be nice. If you feel comfortable with all the other lessons, now might be a good time to look more into that. I've never done it, so good luck. Does sound like a trick that would be nice to have though. Maybe I'll try working something up later.
    You're only born perfect.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with abstract classes
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 03:03 PM
  2. Operators of abstract classes
    By Thanuja91 in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2007, 05:30 AM
  3. Replies: 7
    Last Post: 03-10-2004, 03:10 PM
  4. Abstract classes and operators
    By ygfperson in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2003, 10:50 PM
  5. Purpose of Abstract Classes
    By luckygold6 in forum C++ Programming
    Replies: 15
    Last Post: 04-29-2003, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21