Aggregate class returns

This is a discussion on Aggregate class returns within the C++ Programming forums, part of the General Programming Boards category; Hi all, I've been trying to create a function to display all lecturers in a particular department. The clue given ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Thumbs up Aggregate class returns

    Hi all,

    I've been trying to create a function to display all lecturers in a particular department. The clue given to me by my tutor was to write two versions of this function, one at the universityClass level that invokes a second version at the departmentClass level.

    After many failed attempts I wonder, can anybody explain the method to me? I worked out how to print of all the department names, but this seems to be a different kettle of fish!

    Code:
    //////////////////////////////////////////////////////////////////////////
    // Title	: Aggregation and Composition in a University System
    // Author	: Dr Ian van der Linde
    // Date 	: 01-04-10
    //////////////////////////////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////////////////////////
    // Pre-processor Directives
    //////////////////////////////////////////////////////////////////////////
    
    #include <iostream>
    #include <cstdlib>
    #include <string.h>
    
    using namespace std;
    
    #define MAXDEPTSPERUNIV      10
    #define MAXLECTSPERDEPT       5
    #define MAXSTRINGLENGTH     25
    
    //////////////////////////////////////////////////////////////////////////
    // Class Definition
    //////////////////////////////////////////////////////////////////////////
    
    class lecturerClass
    {
      private:
        char lecturerName[MAXSTRINGLENGTH];
      public:
        lecturerClass()             { strcpy(lecturerName,"");       }
        char* getName()             { return lecturerName;           }
        void setName(char* newName) { strcpy(lecturerName, newName); }
    };
    
    //////////////////////////////////////////////////////////////////////////
    // Class Definition
    //////////////////////////////////////////////////////////////////////////
    
    class departmentClass
    {
      private:
        char departmentName[MAXSTRINGLENGTH];
        int numLecturers;
        lecturerClass* lecturer[MAXLECTSPERDEPT]; // Aggregation
      public:
        departmentClass();
        void setDepartmentName(char *newName) { strcpy(departmentName, newName); }
        char* getDepartmentName()             { return departmentName;           }
        void addLecturer(lecturerClass &newLecturer);
    };
    
    //////////////////////////////////////////////////////////////////////////
    // Member Function Implementations
    //////////////////////////////////////////////////////////////////////////
    
    departmentClass::departmentClass()
    {
      strcpy(departmentName, "");
      numLecturers=0;
    }
    
    //////////////////////////////////////////////////////////////////////////
    
    void departmentClass::addLecturer(lecturerClass &newLecturer)
    {
      lecturer[numLecturers] = &newLecturer;
      numLecturers++;
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Class Definition
    //////////////////////////////////////////////////////////////////////////
    
    class universityClass
    {
      private:
        departmentClass department[MAXDEPTSPERUNIV]; // Composition
        int currentNumDepts;
        string uniName;
      public:
        void addLecturer(lecturerClass &lect, int deptcode);
        void intialiseDepartmentNames();
        void setUniversityName( string );
        string getUniversityName() const;
        string getDepartmentName( int );
    };
    
    //////////////////////////////////////////////////////////////////////////
    // Member Function Implementations
    //////////////////////////////////////////////////////////////////////////
    
    string universityClass::getDepartmentName( int i )
    {
        return department[ i ].getDepartmentName();
    } // end function getDepartmentName
    
    //////////////////////////////////////////////////////////////////////////
    
    string universityClass::getUniversityName() const
    {
        return uniName;
    } // end function getUniversityName
    
    //////////////////////////////////////////////////////////////////////////
    
    void universityClass::setUniversityName( string a )
    {
        uniName = a;
    } // end function setUniversityName
    
    //////////////////////////////////////////////////////////////////////////
    
    void universityClass::addLecturer(lecturerClass &lect, int deptcode)
    {
      department[deptcode].addLecturer(lect);
    }
    
    //////////////////////////////////////////////////////////////////////////
    
    void universityClass::intialiseDepartmentNames()
    {
         department[0].setDepartmentName("Engineering");
         department[1].setDepartmentName("Computing");
         department[2].setDepartmentName("Psychology");
         department[3].setDepartmentName("Divination");
         department[4].setDepartmentName("Chemistry");
         department[5].setDepartmentName("Biology");
         department[6].setDepartmentName("Potions");
         department[7].setDepartmentName("Economics");
         department[8].setDepartmentName("Dark Arts");
         department[9].setDepartmentName("Ninja");
         currentNumDepts=10;
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Main Function Implementation
    //////////////////////////////////////////////////////////////////////////
    
    int main(void)
    {
      // declare an integer variable
      int userChoice;
    
      // create an object of university class
      universityClass angliaRuskin;
    
      // set university class data member uniName
      angliaRuskin.setUniversityName( "Anglia Ruskin University" );
    
      // get university class data member uniName
      cout << "Welcome to " << angliaRuskin.getUniversityName() << "!" << endl << endl;
    
      // initialise array of department names
      angliaRuskin.intialiseDepartmentNames();
    
      cout << "Courses on offer:" << endl << endl;
    
      // begin a loop to count to 10
      for ( int i = 0; i < MAXDEPTSPERUNIV; i++ )
      {
          // return the names of each course in the array
          cout << i << "\t" << angliaRuskin.getDepartmentName( i ) << endl;
      } // end for loop
    
      // declare 3 lecturer class objects
      lecturerClass l1, l2, l3;
    
      // initialize them
      l1.setName("Ian van der Linde");
      l2.setName("Antony Carter");
      l3.setName("Mike Smith");
    
      // add them to the array
      angliaRuskin.addLecturer(l1, 1);
      angliaRuskin.addLecturer(l2, 4);
      angliaRuskin.addLecturer(l3, 8);
    
      // ask the user a question
      cout << "Which course are you interested in?" << endl;
    
      // put user response into integer variable userChoice
      cin >> userChoice;
    
    
    } // end main

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    #include <string.h>
    That should be <cstring>. On the other hand, since you're already using std::string objects elsewhere in your code I'd ask why you're bothering with this and also why you haven't included the <string> header which is required for containers of that type.

    Code:
    class lecturerClass
    {
        ...
        char* getName()             { return lecturerName;           }
        ...
    };
    ...
    class departmentClass
    {
        ...
        char* getDepartmentName()             { return departmentName;           }
        ...
    };
    Unless you want the potential of people using that returned pointer to modify the internal (private) member, I'd make the return type const char * and the functions themselves should be declared const as well.


    Code:
    class universityClass
    {
        ...
        string getUniversityName() const;
        string getDepartmentName( int );
    };
    Ok, why isn't the getDepartmentName member function const as well?



    Now, as an example of what I think your tutor is suggesting:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    class car
    {
        std::string make;
        std::string model;
        std::string color;
        int year;
    public:
        car(const std::string make_, const std::string model_,
            const std::string color_, int year_) : make(make_), model(model_),
                                                   color(color_), year(year_)
        {
        }
        //Public print function for car class, called by global operator<<
        std::ostream& print(std::ostream& os) const
        {
            return os << year << ' ' << make << ' ' << model << ' ' << color;
        }
    };
    
    // Overloaded operator<< for class car
    std::ostream& operator<<(std::ostream& os, const car& rhs)
    {
        return rhs.print(os);
    }
    
    class cardealership
    {
        std::string name;
        std::vector<car> cars;
    public:
        cardealership(const std::string& name_) : name(name_)
        {
        }
        void addinventory(const car& new_car)
        {
            cars.push_back(new_car);
        }
        //Public print function for cardealership class, called by global operator<<
        std::ostream& print(std::ostream& os) const
        {
            os << "Welcome to " << name << "!!!\nHere is our current inventory: " << std::endl;
            std::copy(cars.begin(),cars.end(),std::ostream_iterator<car>(os,"\n"));
            return os;
        }
    };
    
    //Overloaded operator<< for class cardealership
    std::ostream& operator<<(std::ostream& os, const cardealership& rhs)
    {
        return rhs.print(os);
    }
    
    int main()
    {
        cardealership foo("Bob's Super Happy Fun Time Car Dealership");
        
        // Add some inventory to object foo
        foo.addinventory(car("Toyota","Camry","Red",2006));
        foo.addinventory(car("Chevy","Impala","Silver",2010));
        foo.addinventory(car("Ford","Mustang","Black",2005));
    
        // Now display what is in object foo.
        std::cout << foo << std::endl;
    
        return 0;
    }
    Attached Images Attached Images  
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    I realized my example is not really what you were asking about. In your case, your University class would have some kind of printDeptLecturers function and you'd pass in either the deptcode or the departmentName. You'd have to find the department which matched the name you give to the function and then you'd just iterate through that departments lecturers one-by-one and print them out.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. custom doublylinkedlist class returns wrong data
    By r0flc0pter in forum C++ Programming
    Replies: 0
    Last Post: 06-13-2009, 06:18 AM
  2. Function that returns a class...
    By cjmdjm in forum C++ Programming
    Replies: 16
    Last Post: 02-21-2009, 03:21 PM
  3. function which returns a pointer to a class.
    By apacz in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2005, 04:30 PM
  4. Replies: 10
    Last Post: 06-05-2004, 08:40 PM
  5. Class function that returns the instance it's called from
    By L Boksha in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2002, 12:52 PM

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