Help with a Class

This is a discussion on Help with a Class within the C++ Programming forums, part of the General Programming Boards category; the private data in this class should include a a first name, a last name, a middle initial, an employee ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    43

    Help with a Class

    the private data in this class should include a a first name, a last name, a middle initial, an employee id, a birthdate, a hiredate,
    and an array of no more than 4 pointers to courses, but im not sure if im doing it right, this is what i have so far. How could i use the array of 4 pointers to store information about a course. I am reading this info from a file that has something like this with the first number being employee id the second being course name, third being course number and fourth being number of students.

    9600 Math 2444 60

    Code:
    
    
    #include "Constants.cpp"
    
    class Instructor
     {
      public:
    
       Instructor(string fn, string ln, string mi, string eid);
       ~Instructor();
    
       
    
    
      private:
    
       string firstName;
       string lastName;
       string middleInitial;
       string employeeID;
       Course *[4];
    
     };
    
    #endif

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you will probably also want to pass to your constructor the four pointers to fill the array. also the array needs a name.And you will need a forward declaration of class Course.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Umm.. I think you should actually have a variable for the array.

    Code:
    private:
    
       string firstName;
       string lastName;
       string middleInitial;
       string employeeID;
       Course *ptr_Courses[4];
    There's nothing wrong with that code.

    >>How could i use the array of 4 pointers to store information about a course.

    Include a variable to hold the number of courses that the instructor is giving.

    Code:
    int noOfCourses;
    //Set this value to 0 in the constructor
    Then, whenever you need to, just allocate memory to the appropriate element.

    Code:
    BOOL Instructor::addCourse()
    {
         if( noOfCourses == 4)
              return FALSE;
    
         ptr_Courses[noOfCourses] = new Course;
         noOfCourses++;
    }
    And don't forget to delete the allocated memory.
    Last edited by The Dog; 09-27-2002 at 02:21 PM.

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> but im not sure im doing it right
    This is when you write your driver code to test it out. That's the fun part!
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    ok here is the driver file but im gettin these weird syntax error

    Code:
    
    
    #include "lab1InstructorCL.cpp"
    
    void printCourses(Instructor *);
    void printData(Instructor *[]);
    void printSpaces(int num_spaces_to_print);
    void printInstructor(Instructor *);
    void readCourses(ifstream &I, Instructor *[]);
    void readData(ifstream &, Instructor *[]);
    
    int main()
     {
      cout << "This is line number " << __LINE__ << " of the file " << 
    __FILE__
           << " which was\ncompiled on " << __DATE__ << " at time " << 
    __TIME__
           << ".\n";
    
    
      ifstream inFile("input.dat", ios::in);
    
      if(!inFile)
       {
        cerr << "Could not open the file "
             << "for input.\n";
        exit(ABNORMAL_EXIT);
       }
      else
       cout << "The file input.dat is open for 
    input.\n\n";
    
      Instructor *S[MAX_INSTRUCTORS] = {NULL_POINTER};
    
      readData(inFile, S);
    
      cout << "\n\nThe instructors hired at XYZ are:\n\n";
      //printData(S);
    
      //readCourses(inFile, S);
    
      cout << "\n\nThe XYZ instructors and their teaching assignments 
    are:\n\n";
      //printData(S);
    
      cout << "The retirement ceremony has begun:\n\n";
      for(int index = LOOP_BASE; index < MAX_INSTRUCTORS; index++)
       {
        delete S[index];
        S[index] = NULL_POINTER;
       }
     }
    
    /******************************************************************************
     FUNCTION NAME : printCourses
     CLASS         : (global)
     QUALIFIERS    : none
     INPUTS        : a non-consant pointer to a non-constant object of 
    class
                      Instructor
     RETURNS       : nothing
     PURPOSE       : print the courses taught by this instructor, to the 
    screen,
                      in columnar format
    ******************************************************************************/
    
    /******************************************************************************
     FUNCTION NAME : printData
     CLASS         : (global)
     QUALIFIERS    : none
     INPUTS        : a nonconstant array of pointers to nonconstant objects 
    of
                      class Instructor
     RETURNS       : nothing
     PURPOSE       : for every non-null pointer in the incoming argument, 
    pass
                      pointer to printInstructor().
    ******************************************************************************/
    void printData(Instructor *S[])
     {
      for(int index = LOOP_BASE; index < MAX_INSTRUCTORS; index++)
       {
        if(S[index] != NULL_POINTER)
         printInstructor(S[index]);
       }
     }
    
    /******************************************************************************
     FUNCTION NAME : printSpaces
     CLASS         : global
     QUALIFIERS    : none
     INPUTS        : an integer
     RETURNS       : nothing
     PURPOSE       : print to the screen a number of blank spaces equal to 
    the
                      value of the incoming argument
    ******************************************************************************/
    void printSpaces(int num_spaces_to_print)
     {
      for(int index = LOOP_BASE; index < num_spaces_to_print; index++)
       {
        cout << ' ';
       }
     }
    
    /******************************************************************************
     FUNCTION NAME : printInstructor
     CLASS         : global
     QUALIFIERS    : none
     INPUTS        : a non-constant pointer to a non-constant object of 
    class
                      Insructor
     RETURNS       : nothing
     PURPOSE       : print to the screen the data concerning this 
    instructor,
                      in columnar format, using the printCourses() function
                      as appropriate
    ******************************************************************************/
    void printInstructor(Instructor *S)
     {
      cout << "Instructor " << S->getLastName() << ',';
      printSpaces(LAST_NAME_FIELD_WIDTH - S->getLastName().length());
    
      cout << S->getFirstName();
      printSpaces(FIRST_NAME_FIELD_WIDTH - S->getFirstName().length());
    
      cout << ' ' << S->getMiddleInitial()
           << ' ' << S->getEmployeeID();
    
      if(S->Courses[INDEX_BASE].courseDepartment.size() != 
    EMPTY_STRING_SIZE)
       {
        printCourses(S);
       }
      else
       cout << '\n';
     }
    
    /******************************************************************************
     FUNCTION NAME : readCourses
     CLASS         : none
     QUALIFIERS    : none
     INPUTS        : an ifstream object, by reference using a reference 
    parameter
                     an array of non-constant pointers to non-constant 
    objects of
                      class Instructor
     RETURNS       : nothing
     PURPOSE       : for all instructors pointed to by the second argument, 
    read
                      from the stream referred to by the first argument the
                      courses to be taught by that instructor and store 
    those
                      values in the appopriate fields in the object
    ******************************************************************************/
    void readCourses(ifstream &I, Instructor *S[])
     {
      string courseDepartment;
      int numStudents;
      string courseNumber;
      string employeeID;
    
      while(!I.eof())
       {
        I >> employeeID >> courseDepartment >> courseNumber >> numStudents;
    
        if(I.eof())
         break;
    
        for(int instructorIndex = LOOP_BASE;
                instructorIndex < MAX_INSTRUCTORS;
                instructorIndex++)
         {
          if(S[instructorIndex]->getEmployeeID() == employeeID)
           {
            for(int courseIndex = LOOP_BASE;
                    courseIndex < MAX_COURSES;
                    courseIndex++)
             {
              
    if(S[instructorIndex]->Courses[courseIndex].courseDepartment.size() ==
                 EMPTY_STRING_SIZE)
               {
                S[instructorIndex]->Courses[courseIndex].courseDepartment =
                  courseDepartment;
                S[instructorIndex]->Courses[courseIndex].courseNumber = 
    courseNumber;
                S[instructorIndex]->Courses[courseIndex].numStudents = 
    numStudents;
                break;
               }
             }
           }
         }
       }
     }
    
    /******************************************************************************
     FUNCTION NAME : readData
     CLASS         : none
     QUALIFIERS    : none
     INPUTS        : a reference to an ifstream object
                     an array of non-constant pointers to non-constant 
    objects of
                      class Instructor
     RETURNS       : nothing
     PURPOSE       : read from the stream referred to by the first argument 
    the
                      values necessary to instantiate an Instructor object
                     instantiate the Instructor object with those values 
    and
                      assign the address of the object to the next 
    available
                      pointer in the second argument
    ******************************************************************************/
    void readData(ifstream &I, Instructor *S[])
     {
      string firstName;
      string lastName;
      string middleInitial;
      string employeeID;
      string bmn;  // Birth Month
      string bdy;  // Birth Day
      string byr   // Birth Year
      string hmn;  // Hire Month
      string hdy;  // Hire Day
      string hyr;  // Hire Year
      
    
      for(int index = 0; index < 3; index++)
       {
        inFile >> firstName >> middleInitial >> lastName >> employeeID>>bmn
    		>>bdy>>byr>>hmn>>hdy>>hyr;
    	S[index] = new Instructor(firstName, lastName, middleInitial, 
    	employeeID);
        
       }
    
    }
    Compiling...
    lab1Driver.cpp
    c:\documents and settings\massa\lab1instructorcl.cpp(9) : error C2629: unexpected 'class Instructor ('
    c:\documents and settings\massa\lab1instructorcl.cpp(9) : error C2238: unexpected token(s) preceding ';'
    c:\documents and settings\massa\lab1instructorcl.cpp(17) : error C2146: syntax error : missing ';' before identifier 'getFirstName'
    c:\documents and settings\massa\lab1instructorcl.cpp(17) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(18) : error C2146: syntax error : missing ';' before identifier 'getLastName'
    c:\documents and settings\massa\lab1instructorcl.cpp(18) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(19) : error C2146: syntax error : missing ';' before identifier 'getMiddleInitial'
    c:\documents and settings\massa\lab1instructorcl.cpp(19) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(20) : error C2146: syntax error : missing ';' before identifier 'getEmployeeID'
    c:\documents and settings\massa\lab1instructorcl.cpp(20) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(22) : error C2061: syntax error : identifier 'string'
    c:\documents and settings\massa\lab1instructorcl.cpp(23) : error C2061: syntax error : identifier 'string'
    c:\documents and settings\massa\lab1instructorcl.cpp(24) : error C2061: syntax error : identifier 'string'
    c:\documents and settings\massa\lab1instructorcl.cpp(25) : error C2061: syntax error : identifier 'string'
    c:\documents and settings\massa\lab1instructorcl.cpp(29) : error C2146: syntax error : missing ';' before identifier 'firstName'
    c:\documents and settings\massa\lab1instructorcl.cpp(29) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(29) : error C2501: 'firstName' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(30) : error C2146: syntax error : missing ';' before identifier 'lastName'
    c:\documents and settings\massa\lab1instructorcl.cpp(30) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(30) : error C2501: 'lastName' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(31) : error C2146: syntax error : missing ';' before identifier 'middleInitial'
    c:\documents and settings\massa\lab1instructorcl.cpp(31) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(31) : error C2501: 'middleInitial' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(32) : error C2146: syntax error : missing ';' before identifier 'employeeID'
    c:\documents and settings\massa\lab1instructorcl.cpp(32) : error C2501: 'string' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(32) : error C2501: 'employeeID' : missing storage-class or type specifiers
    c:\documents and settings\massa\lab1instructorcl.cpp(37) : fatal error C1020: unexpected #endif
    Error executing cl.exe.

    lab1Driver.exe - 27 error(s), 0 warning(s)
    Last edited by maloy; 09-27-2002 at 03:27 PM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    I think there could be something wrong with my constants file

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    I replied to you PM maloy

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    In future if you need to post that amount of code attach it as a zipfile instead. It also helps especially with visual c if you include the project/workspace files.Its a real pain to trawl through that amount of code on a webpage when it could so easily be downloaded and put thru the compiler.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    How do i put everything in a zip file

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why on earth are you #include-ing .cpp files? Sure you can, but just because you can do something, doesn't mean you should.

    You shouldn't include .cpp files. .cpp files are meant to be compiled, not included.

    You include header files. Typically (read: never) people don't include .cpp files. That's just backwards.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    because the other files are classes i have three different classes and then their member function files

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by maloy
    because the other files are classes i have three different classes and then their member function files
    You don't include class source files. You include headers to them. Or rather, it's Bad Form(TM) to include .cpp files.

    A much better way to do this, is to create a header file for you class, and include that.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    ok here is my code in a zip file, i have error problems as i posted above.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 12:36 PM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 03:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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