Problem with inheritance assignment..

This is a discussion on Problem with inheritance assignment.. within the C++ Programming forums, part of the General Programming Boards category; Hello all. The class Student in this program is a child class of Person. In it, I overloaded the input ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    5

    Question Problem with inheritance assignment..

    Hello all.
    The class Student in this program is a child class of Person. In it, I overloaded the input and output operators, so that the lines in my int main() function will work properly.

    I am having some run time issues involving the input operator in my Student class. Towards the end of the function, when the user is asked to input the courses (name and ID) that they are enrolled in, the program crashes. I'm thinking the problem may be a segmentation fault involving the input operator that I overloaded for the Struct called "Course."

    It isn't due for a few more days but any help would be greatly appreciated.


    Code:
    #include <iostream>
    
    
    
    
    using namespace std;
    
    
    struct Course {
           Course();
           Course(char* the_name, long the_courseID);
           Course(const Course& the_object);
           ~ Course(){delete name;};
           void operator = (const Course& the_object);
           friend ostream & operator << (ostream & out, Course& r);
           friend istream & operator >> (istream & in, Course& r);
           char* name;
           long courseID;
    };
    
    
    enum EStudentStatus {Fulltime, Partime, Exchange };
    
    
    static char* studentstatusLabels[] = {"Fulltime", 
                                          "Partime",
                                          "Exchange" };
                                          
    enum EDepartment { Business, Engineering, Physics, ComputerScience };
    
    
    static char* departmentLabels[] = {"Business",
                                       "Engineering",
                                       "Physics",
                                       "ComputerScience" };
    enum ERank {Instructor, AssociateProfessor, Professor, Dean };
    
    
    static char* rankLabels[] = {"Instructor",
                                 "AssociateProfessor",
                                 "Professor",
                                 "Dean" };
                                 
                                 
    class Person {
          
          public:
                 Person();
                 Person(string the_name, string the_address, unsigned long the_ID);
                 Person(const Person& the_object){};
                 ~Person(){};
                 void operator = (const Person& the_object);
                 string get_name();
                 void set_name(string the_name);
                 string get_address();
                 void set_address(string the_address);
                 unsigned long get_ID();
                 void set_ID(unsigned long the_ID);
          
          private:
                  string name;
                  string address; //office location for faculty
                  unsigned long ID;
    };
    
    
    const unsigned MAX_COURSES_FOR_STUDENT = 10;
    
    
    class Student : virtual public Person {
          
          public:
                 Student();
                 Student(const Student& the_object);
                 ~Student(){};
                 void operator = (const Student& s);
                 friend istream & operator >> (istream & in, Student& r);
                 friend ostream & operator << (ostream & out, Student& r);
                 
                 
                 /*
                 EStudentStatus get_status();
                 void set_status(EStudentStatus the_status);
                 EDepartment get_department();
                 void set_department(EDepartment the_department);
                 Course* get_courses();
                 void set_courses(int courseCount);
                 int get_numCourses();
                 void set_numCourses(int courseCount);
                 */
                 
                 
          private:
                  EStudentStatus status;
                  EDepartment department;
                  Course* enrolled[MAX_COURSES_FOR_STUDENT];
                  int nCourses;
    };
    
    
    const unsigned MAX_COURSES_FOR_FACULTY = 5;
    const unsigned MAX_GRADERS = 10;
    
    
    
    
    
    
    
    
    Course :: Course()
    {
        name = NULL;
        courseID = 0;   
    }
    
    
    
    
    Course :: Course(char* the_name, long the_courseID)
    {
           name = the_name;
           courseID = the_courseID;
    }
    
    
    Course :: Course(const Course& the_object)
    {
           
    }
    
    
    
    
    void Course :: operator =(const Course& the_object)
    {
      if (this == &the_object) {
        cout << "self" << endl;
          
      }
    
    
      name = the_object.name;
      courseID = the_object.courseID;
      return;
    }
    
    
    
    
    
    
    
    
    ostream & operator << (ostream & out, Course& r)
    {
            out << "Course Name: ";
            out << r.name;
            out << "\nCourse ID: ";
            out << r.courseID;
            
            return out;
    }
    
    
    
    
    
    
    
    
    
    
    istream & operator >> (istream & in, Course& r)
    {
            cout << "\nEnter Course Name: ";
            in >> r.name;
            cout << "\nEnter Course ID: ";
            in >> r.courseID;
            cout << "\nCourse Added.";
            return in;
    }
    
    
    
    
    
    
    Person :: Person()
    {
             name = "empty";
             address = "empty";
             ID = 0;
    }
    
    
    
    
    
    
    Person :: Person(string the_name, string the_address, unsigned long the_ID)
    {
           name = the_name;
           address = the_address;
           ID = the_ID;
    }
    
    
    
    
    
    
    void Person :: operator = (const Person& the_object)
    {
        if (this == &the_object) {
        cout << "self" << endl;
          
      }
    
    
      name = the_object.name;
      address = the_object.address;
      ID = the_object.ID;
      return;
    }
    
    
    
    
    string Person :: get_name()
    {
           return name;
    }
    
    
    
    
    
    
    void Person :: set_name(string the_name)
    {
         name = the_name;
    }
    
    
    
    
    
    
    string Person :: get_address()
    {
           return address;
    }
    
    
    
    
    
    
    void Person :: set_address(string the_address)
    {
         address = the_address;
    }
    
    
    
    
    
    
    unsigned long Person :: get_ID()
    {
             return ID;
    }
    
    
    
    
    
    
    void Person :: set_ID(unsigned long the_ID)
    {
         ID = the_ID;
    }
    
    
    Student :: Student() : Person()
    {
             status = Fulltime;
             department = Business;
             nCourses = 0;
             enrolled[MAX_COURSES_FOR_STUDENT];
    }
    
    
    istream & operator >> (istream & in, Student& r)
    {
            string s_name;
            string s_address;
            int s_ID;
            int status_label;
            int department_label;
            Course Course1;
            cout << "Enter student name: ";
            getline(in, s_name);
            r.set_name(s_name);
            cout << "\nEnter student address: ";
            getline(in, s_address);
            r.set_address(s_address);
            cout <<"\nEnter student ID: ";
            in >> s_ID;
            r.set_ID(s_ID);
            cout <<"What is their status as a student? Enter 0 for Fulltime,"
                 <<"\n 1 for Partime, or 2 for Exchange.";
            in >> status_label;
            if (status_label == 0)
            {
              r.status = Fulltime;
            }
            else if (status_label == 1)
            {
              r.status = Partime;
            }
            else if (status_label == 2)
            {
              r.status = Exchange;
            }
            else
            {
                cout <<"Error, invalid student label." << endl;
            }
            
            cout <<"What department is the student in? Enter 0 for Business, 1 for Engineering,"
                 <<"\n 2 for Physics, and 3 for Computer Science." << endl;
            in >> department_label;
            if (department_label == 1)
            {
               r.department = Business;
            }
            else if (department_label == 2)
            {
                 r.department = Engineering;
            }
            else if (department_label == 3)
            {
                r.department = Physics;
            }
            else if (department_label == 4)
            {
                 r.department = ComputerScience;
            }
            else 
            {
                 cout << "Error, invalid department label." << endl;
            }
            cout << "How many courses are you taking?";
            in >> r.nCourses;
            
            cout << "List the courses you are taking. ";
            for (int i = 0; i < r.nCourses; i++)
            {
                cin >> Course1;
                r.enrolled[i] = &Course1;
            }
          
            
            cout << "Student Data entered." << endl;
            return in;
    }
    
    
    ostream & operator << (ostream & out, Student& r)
    {
            out << "Student name is: " << r.get_name() << endl;
            out << "Student address is: " << r.get_address() << endl;
            out << "Student ID is: " << r.get_ID();
            out << "Student status is: " << r.status << endl;
            out << "Student department is: " << r.department << endl;
            out << "Student is taking " << r.nCourses << " classes." << endl;
            out << "Students class list is: " << endl;
            for (int i = 0; i < MAX_COURSES_FOR_STUDENT; i++)
            {
                if (i < r.nCourses)
              {
              out << r.enrolled[i];
              }
                else
              {
                out << "Empty class slots." << endl;
              }
            }
            
            return out;
    }
      
    
    
    
    
    
    
    int main()
    {
    
    
        Student test;
        cin >> test; 
        cout << test;
     
        system("pause");
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    For your includes:
    • #include <string>
    • #include <vector>
    • #include <cstdlib> for std::system, though really you can do without the std::system call.


    For your Course struct:
    • Use std::string for name. Once you do this, you can get rid of the copy constructor, copy assignment operator and destructor because the compiler generated ones would then suffice.
    • The overloaded operator<< should have a const Course& parameter.


    For the labels:
    • Why are they declared static? If they are meant to be implementation detail, then define them in an unnamed namespace.
    • Use std::string


    For your Person class:
    • Person is meant to be a polymorphic base class, so where are its virtual member functions?
    • At the very least the destructor of Person should be virtual since it is public.
    • The std::string parameters should be const references.
    • You don't need to define the copy constructor and destructor since the compiler generated ones will suffice. You only need to define the destructor to declare it virtual.
    • Your getter functions should be declared const.


    For your Student class:
    • Why do you use virtual inheritance here?
    • enrolled should be a std::vector<Course>.
    • Get rid of the copy constructor, copy assignment operator and destructor.
    • The overloaded operator<< should have a const Student& parameter.


    Your constructors should use their initialiser list. Your indentation is nearly there: you just need to be more consistent, e.g., stick to whatever number of spaces per indent level.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    I implemented many of the changes you suggested...now it won't compile. In my input and output operators, it isn't allowing me to access the private variables, enrolled, status and department, even though I am overloading a friend function of the same class!!

    Also, some of the little things you questioned me about were given to us as our "here's a template to get you started" gesture from our professor. This includes the static labels and the virtual inheritance for the Student class.


    Code:
    #include <iostream>
    #include <vector>
    
    
    
    
    using namespace std;
    
    
    struct Course {
           Course();
           Course(string the_name, long the_courseID);
           friend ostream & operator << (ostream & out, const Course& r);
           friend istream & operator >> (istream & in, Course& r);
           string name;
           long courseID;
    };
    
    
    enum EStudentStatus {Fulltime, Partime, Exchange };
    
    
    char* studentstatusLabels[] = {"Fulltime", 
                                          "Partime",
                                          "Exchange" };
                                          
    enum EDepartment { Business, Engineering, Physics, ComputerScience };
    
    
    char* departmentLabels[] = {"Business",
                                       "Engineering",
                                       "Physics",
                                       "ComputerScience" };
    enum ERank {Instructor, AssociateProfessor, Professor, Dean };
    
    
    char* rankLabels[] = {"Instructor",
                                 "AssociateProfessor",
                                 "Professor",
                                 "Dean" };
                                 
                                 
    class University {
          
          public:
                void run();
                 
          private:
      //            Student* l_students;
      //            Faculty* l_faculties;
    };
    
    
    class Person {
          
          public:
                 Person();
                 Person(string the_name, string the_address, unsigned long the_ID);
                 virtual ~Person(){};
                 const virtual string get_name();
                 virtual void set_name(string the_name);
                 const virtual string get_address();
                 virtual void set_address(string the_address);
                 const virtual unsigned long get_ID();
                 virtual void set_ID(unsigned long the_ID);
          
          private:
                  string name;
                  string address; //office location for faculty
                  unsigned long ID;
    };
    
    
    const unsigned MAX_COURSES_FOR_STUDENT = 10;
    
    
    class Student : virtual public Person {
          
          public:
                 Student();
                 friend istream & operator >> (istream & in, Student& r);
                 friend ostream & operator << (ostream & out, const Student& r);
                 
                 
                 /*
                 EStudentStatus get_status();
                 void set_status(EStudentStatus the_status);
                 EDepartment get_department();
                 void set_department(EDepartment the_department);
                 Course* get_courses();
                 void set_courses(int courseCount);
                 int get_numCourses();
                 void set_numCourses(int courseCount);
                 */
    
    
                 
          private:
                  EStudentStatus status;
                  EDepartment department;
                  vector <Course> enrolled;
                  int nCourses;
    };
    
    
    const unsigned MAX_COURSES_FOR_FACULTY = 5;
    const unsigned MAX_GRADERS = 10;
    
    
    /*
    class Faculty : virtual public Person {
          
          public:
                 
                 
          private:
                  EDepartment departmnet;
                  ERank rank;
                  Person* supervisor;
                  Course* offered[MAX_COURSES_FOR_FACULTY];
                  int nCourses;
                  Person* graders[MAX_GRADERS];
                  int nGraders;
    };
    */
    
    
    
    
    
    
    
    
    
    
    Course :: Course()
    {
        name = "No name";
        courseID = 0;   
    }
    
    
    
    
    
    
    
    
    
    
    Course :: Course(string the_name, long the_courseID)
    {
           name = the_name;
           courseID = the_courseID;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    ostream & operator << (ostream & out, Course& r)
    {
            out << "Course Name: ";
            out << r.name;
            out << "\nCourse ID: ";
            out << r.courseID;
            
            return out;
    }
    
    
    
    
    
    
    
    
    
    
    istream & operator >> (istream & in, Course& r)
    {
            cout << "\nEnter Course Name: ";
            in >> r.name;
            cout << "\nEnter Course ID: ";
            in >> r.courseID;
            cout << "\nCourse Added.";
            return in;
    }
    
    
    
    
    
    
    Person :: Person()
    {
             name = "empty";
             address = "empty";
             ID = 0;
    }
    
    
    
    
    
    
    Person :: Person(string the_name, string the_address, unsigned long the_ID)
    {
           name = the_name;
           address = the_address;
           ID = the_ID;
    }
    
    
    
    
    const string Person :: get_name()
    {
           return name;
    }
    
    
    
    
    
    
    void Person :: set_name(string the_name)
    {
         name = the_name;
    }
    
    
    
    
    
    
    const string Person :: get_address()
    {
           return address;
    }
    
    
    
    
    
    
    void Person :: set_address(string the_address)
    {
         address = the_address;
    }
    
    
    
    
    
    
    const unsigned long Person :: get_ID()
    {
             return ID;
    }
    
    
    
    
    
    
    void Person :: set_ID(unsigned long the_ID)
    {
         ID = the_ID;
    }
    
    
    Student :: Student() : Person()
    {
             status = Fulltime;
             department = Business;
             nCourses = 0;
             enrolled[MAX_COURSES_FOR_STUDENT];
    }
    
    
    istream & operator >> (istream & in, Student& r)
    {
            string s_name;
            string s_address;
            int s_ID;
            int status_label;
            int department_label;
            Course Course1;
            cout << "Enter student name: ";
            getline(in, s_name);
            r.set_name(s_name);
            cout << "\nEnter student address: ";
            getline(in, s_address);
            r.set_address(s_address);
            cout <<"\nEnter student ID: ";
            in >> s_ID;
            r.set_ID(s_ID);
            cout <<"What is their status as a student? Enter 0 for Fulltime,"
                 <<"\n 1 for Partime, or 2 for Exchange.";
            in >> status_label;
            if (status_label == 0)
            {
              r.status = Fulltime;
            }
            else if (status_label == 1)
            {
              r.status = Partime;
            }
            else if (status_label == 2)
            {
              r.status = Exchange;
            }
            else
            {
                cout <<"Error, invalid student label." << endl;
            }
            
            cout <<"What department is the student in? Enter 0 for Business, 1 for Engineering,"
                 <<"\n 2 for Physics, and 3 for Computer Science." << endl;
            in >> department_label;
            if (department_label == 1)
            {
               r.department = Business;
            }
            else if (department_label == 2)
            {
                 r.department = Engineering;
            }
            else if (department_label == 3)
            {
                r.department = Physics;
            }
            else if (department_label == 4)
            {
                 r.department = ComputerScience;
            }
            else 
            {
                 cout << "Error, invalid department label." << endl;
            }
            cout << "How many courses are you taking?";
            in >> r.nCourses;
            
            cout << "List the courses you are taking. ";
            for (vector<Course>::size_type i = 0; i < r.nCourses; i++)
            {
                cin >> Course1;
                r.enrolled[i] = Course1;
                
            }
          
            
            cout << "Student Data entered." << endl;
            return in;
    }
    
    
    ostream & operator << (ostream & out, Student& r)
    {
            out << "Student name is: " << r.get_name() << endl;
            out << "Student address is: " << r.get_address() << endl;
            out << "Student ID is: " << r.get_ID();
            out << "Student status is: " << r.status << endl;
            out << "Student department is: " << r.department << endl;
            out << "Student is taking " << r.nCourses << " classes." << endl;
            out << "Students class list is: " << endl;
            for (vector<Course>::size_type i = 0; i < MAX_COURSES_FOR_STUDENT; i++)
            {
                if (i < r.nCourses)
              {
              out << r.enrolled[i];
              }
                else
              {
                out << "Empty class slots." << endl;
              }
            }
            
            return out;
    }
      
    
    
    
    
    
    
    int main()
    {
    
    
        Student rob;
        cin >> rob; 
        cout << rob;
        //University test;
        //test.run();
        system("pause");
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    I implemented many of the changes you suggested...now it won't compile. In my input and output operators, it isn't allowing me to access the private variables, enrolled, status and department, even though I am overloading a friend function of the same class!!
    You should post the error messages.

    A few other things:
    • The member variables of Course are public, so you don't need to declare friends.
    • This declares the return value to be const:
      Code:
      const virtual string get_name();
      You should declare the member function const:
      Code:
      virtual string get_name() const;
    • Your function declarations must match the function definitions. (This is what probably causes the errors you mentioned.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Thanks so much. I think I'm almost there. It not lets me input a class, but it crashes when I try to input the next class.

    Also, in the Course struct, the compiler won't let me overload the input and output operators without putting "friend" in the function declaration.

    I think the only error that remains is in the for loop of the input operator under the Student class. Am I not working with the vector correctly? I still get a run time error after I input the first Course...

    Thanks again.

    Code:
    #include <iostream>
    #include <vector>
    
    
    
    
    using namespace std;
    
    
    struct Course {
           Course();
           Course(string the_name, long the_courseID);
           friend ostream & operator << (ostream & out, const Course& r);
           friend istream & operator >> (istream & in, Course& r);
           string name;
           long courseID;
    };
    
    
    enum EStudentStatus {Fulltime, Partime, Exchange };
    
    
    char* studentstatusLabels[] = {"Fulltime", 
                                          "Partime",
                                          "Exchange" };
                                          
    enum EDepartment { Business, Engineering, Physics, ComputerScience };
    
    
    char* departmentLabels[] = {"Business",
                                       "Engineering",
                                       "Physics",
                                       "ComputerScience" };
    enum ERank {Instructor, AssociateProfessor, Professor, Dean };
    
    
    char* rankLabels[] = {"Instructor",
                                 "AssociateProfessor",
                                 "Professor",
                                 "Dean" };
                                 
                                 
    class University {
          
          public:
                void run();
                 
          private:
      //            Student* l_students;
      //            Faculty* l_faculties;
    };
    
    
    class Person {
          
          public:
                 Person();
                 Person(string the_name, string the_address, unsigned long the_ID);
                 virtual ~Person(){};
                 virtual string get_name() const;
                 virtual void set_name(string the_name);
                 virtual string get_address() const;
                 virtual void set_address(string the_address);
                 virtual unsigned long get_ID() const;
                 virtual void set_ID(unsigned long the_ID);
          
          private:
                  string name;
                  string address; //office location for faculty
                  unsigned long ID;
    };
    
    
    const unsigned MAX_COURSES_FOR_STUDENT = 10;
    
    
    class Student : virtual public Person {
          
          public:
                 Student();
                 friend istream & operator >> (istream & in, Student& r);
                 friend ostream & operator << (ostream & out, const Student& r);
                 
                 
                 /*
                 EStudentStatus get_status();
                 void set_status(EStudentStatus the_status);
                 EDepartment get_department();
                 void set_department(EDepartment the_department);
                 Course* get_courses();
                 void set_courses(int courseCount);
                 int get_numCourses();
                 void set_numCourses(int courseCount);
                 */
    
    
                 
          
                  EStudentStatus status;
                  EDepartment department;
                  vector <Course> enrolled;
                  int nCourses;
    };
    
    
    const unsigned MAX_COURSES_FOR_FACULTY = 5;
    const unsigned MAX_GRADERS = 10;
    
    
    class Faculty : virtual public Person {
          
          public:
                 
                 
          private:
                  EDepartment departmnet;
                  ERank rank;
                  Person* supervisor;
                  Course* offered[MAX_COURSES_FOR_FACULTY];
                  int nCourses;
                  Person* graders[MAX_GRADERS];
                  int nGraders;
    };
    
    
    
    
    
    
    
    
    
    
    Course :: Course()
    {
        name = "No name";
        courseID = 0;   
    }
    
    
    
    
    
    
    
    
    
    
    Course :: Course(string the_name, long the_courseID)
    {
           name = the_name;
           courseID = the_courseID;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    ostream & operator << (ostream & out, Course& r)
    {
            out << "Course Name: ";
            out << r.name;
            out << "\nCourse ID: ";
            out << r.courseID;
            
            return out;
    }
    
    
    
    
    
    
    
    
    
    
    istream & operator >> (istream & in, Course& r)
    {
            cout << "\nEnter Course Name: ";
            in >> r.name;
            cout << "\nEnter Course ID: ";
            in >> r.courseID;
            cout << "\nCourse Added.";
            return in;
    }
    
    
    
    
    
    
    Person :: Person()
    {
             name = "empty";
             address = "empty";
             ID = 0;
    }
    
    
    
    
    
    
    Person :: Person(string the_name, string the_address, unsigned long the_ID)
    {
           name = the_name;
           address = the_address;
           ID = the_ID;
    }
    
    
    
    
    string Person :: get_name() const
    {
           return name;
    }
    
    
    
    
    
    
    void Person :: set_name(string the_name)
    {
         name = the_name;
    }
    
    
    
    
    
    
    string Person :: get_address() const
    {
           return address;
    }
    
    
    
    
    
    
    void Person :: set_address(string the_address)
    {
         address = the_address;
    }
    
    
    
    
    
    
    unsigned long Person :: get_ID() const
    {
             return ID;
    }
    
    
    
    
    
    
    void Person :: set_ID(unsigned long the_ID)
    {
         ID = the_ID;
    }
    
    
    Student :: Student() : Person()
    {
             status = Fulltime;
             department = Business;
             nCourses = 0;
             enrolled[MAX_COURSES_FOR_STUDENT];
    }
    
    
    istream & operator >> (istream & in, Student& r)
    {
            string s_name;
            string s_address;
            int s_ID;
            int status_label;
            int department_label;
            Course Course1;
            cout << "Enter student name: ";
            getline(in, s_name);
            r.set_name(s_name);
            cout << "\nEnter student address: ";
            getline(in, s_address);
            r.set_address(s_address);
            cout <<"\nEnter student ID: ";
            in >> s_ID;
            r.set_ID(s_ID);
            cout <<"What is their status as a student? Enter 0 for Fulltime,"
                 <<"\n 1 for Partime, or 2 for Exchange.";
            in >> status_label;
            if (status_label == 0)
            {
              r.status = Fulltime;
            }
            else if (status_label == 1)
            {
              r.status = Partime;
            }
            else if (status_label == 2)
            {
              r.status = Exchange;
            }
            else
            {
                cout <<"Error, invalid student label." << endl;
            }
            
            cout <<"What department is the student in? Enter 0 for Business, 1 for Engineering,"
                 <<"\n 2 for Physics, and 3 for Computer Science." << endl;
            in >> department_label;
            if (department_label == 1)
            {
               r.department = Business;
            }
            else if (department_label == 2)
            {
                 r.department = Engineering;
            }
            else if (department_label == 3)
            {
                r.department = Physics;
            }
            else if (department_label == 4)
            {
                 r.department = ComputerScience;
            }
            else 
            {
                 cout << "Error, invalid department label." << endl;
            }
            cout << "How many courses are you taking?";
            in >> r.nCourses;
            
            cout << "List the courses you are taking. ";
            for (vector<Course>::size_type i = 0; i < r.nCourses; i++)
            {
                cin >> Course1;
                r.enrolled[i] = Course1;
                
            }
          
            
            cout << "Student Data entered." << endl;
            return in;
    }
    
    
    ostream & operator << (ostream & out, Student& r)
    {
            out << "Student name is: " << r.get_name() << endl;
            out << "Student address is: " << r.get_address() << endl;
            out << "Student ID is: " << r.get_ID();
            out << "Student status is: " << r.status << endl;
            out << "Student department is: " << r.department << endl;
            out << "Student is taking " << r.nCourses << " classes." << endl;
            out << "Students class list is: " << endl;
            for (vector<Course>::size_type i = 0; i < MAX_COURSES_FOR_STUDENT; i++)
            {
                if (i < r.nCourses)
              {
              out << r.enrolled[i];
              }
                else
              {
                out << "Empty class slots." << endl;
              }
            }
            
            return out;
    }
      
    
    
    
    
    
    
    int main()
    {
    
    
        Student rob;
        cin >> rob; 
        cout << rob;
        //University test;
        //test.run();
        system("pause");
        return 0;
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    You still have not included the <string> header file. Your code may be compiling without it but that simply means that one of your other header files is bringing in the string header on the sly. This is something you should not rely upon when considering transporting your code to other compilers.


    Objects passed into operator<< (stream insertion) should be const. Not the stream passed in, but rather the object you are outputting.


    At the very least, your "labels" should be const char* but really those should be const std::string objects instead.


    Stylistically, if you are going to have all this in a single source file, then you should have your code implementation placed near the classes/structs they are a part of. Code for struct Course for example should be near the definition of said struct instead of after the definitions of the University/Person/Student/Faculty classes. The way you have it means a small bit of extra effort to find where the implementation is when coding/debugging. Code for such objects is typically implemented in their own source file with the class definition placed in an accompanying header file.


    Maybe it's just me, but stylistically an overloaded operator>> (stream extraction) should not itself be performing output. Maybe others can voice their opinion here.


    The system function technically requires the <cstdlib> header. Using such a function call for the purpose you seem to be using it is to be avoided if possible. There is nothing wrong with throwing a call (or two) to cin.get() instead of calling system("pause") here.


    For the "friend" issue, you don't need those two lines in the definition of the Course struct at all. They can be removed.


    Code:
    for (vector<Course>::size_type i = 0; i < r.nCourses; i++)
    {
        cin >> Course1;
        r.enrolled[i] = Course1;
    }
    You need to be using the vector's push_back function, for example:
    Code:
    for (vector<Course>::size_type i = 0; i < r.nCourses; i++)
    {
        cin >> Course1;
        r.enrolled.push_back(Course1);
    }
    "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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by Richieh
    Also, in the Course struct, the compiler won't let me overload the input and output operators without putting "friend" in the function declaration.
    You probably left the declaration in the struct definition. Take it out. The only reason to declare the function in the struct definition is to declare it a friend, otherwise the declaration would be that of a member function, which it isn't.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Problem
    By audinue in forum C++ Programming
    Replies: 18
    Last Post: 08-15-2009, 01:49 PM
  2. Inheritance Problem
    By bigmac(rexdale) in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 09:19 PM
  3. Inheritance problem
    By patricio2626 in forum C++ Programming
    Replies: 15
    Last Post: 04-04-2007, 08:52 PM
  4. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 10:14 AM
  5. Need Help with Inheritance assignment
    By mejv3 in forum C++ Programming
    Replies: 6
    Last Post: 11-07-2005, 11:56 PM

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