Help with Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indiaj0nes
    New Member
    • Feb 2008
    • 3

    Help with Classes

    Hi,
    I am Supposed to be reading from a student file and store it in array of pointers to student Objects(Objects of Student class).

    The file contains data like:
    102022 Jonathan James L
    Dayton, Ohio
    775665
    M 2 25 2.5
    ENGR


    Where the 1st line contains the student id, first name, last name. 2nd line is the home town.3rd line is the phone number,4th line contains Sex, Year, Total Credits, GPA, Major

    I am able to read the file and store in Array of pointers to student objects. However i am stuck at the below issue:

    Then i need to write another class Course which needs to have Dynamically alllocated array of Student Objects
    I am not sure how to link the course class to a student class.
    Please help.

    Here is my student.h file:
    Code:
    #include <iostream>
    #include<vcl.h>
    #include <fstream>
    #include<string>
    #include<stdlib>
    #ifndef STUDENT_H
    #define STUDENT_H
    
    
    //#define FILE_SIZE 102400;
    
    using namespace std;
    
    class student
    {
    	private:
    	int Student_Id;
    	string Name;
    	string HomeTown;
    	string PhoneNo;
    	string Gender;
    	int Year;
    	int TotalCredit;
    	float Gpa;
    	string Major;
    
    	public:
    
    	student();
    	student(int Student_Id, string Name);
    
    	void change(char *r,char *t);
    	float modify(int s,float g);
    	int SetStudentInfo(int Student_Id_In,string Name_In,
    	string HomeTown_In,string PhoneNo_In,
    	string Gender_In,int Year_In,int TotalCredit_In,
    	float Gpa_In,string Major_In );
    	void OutputData();
    	int GetStudentInfo();
    	int GetStudentId();
    	string GetStudentName();
    
     };
    
    
    //Constructor
    
    student::student()
    {
     Student_Id = NULL;
     Name = " ";
     HomeTown = " ";
     PhoneNo = " ";
     Gender = " ";
     Year = NULL;
     TotalCredit = NULL;
     Gpa = NULL;
     Major  = " ";
    
    }
    
    student::student(int Student_Id,string Name)
    {
    this->Student_Id = Student_Id;
    this->Name = Name;
    
    }
    
     student::SetStudentInfo(int Student_Id_In,string Name_In,
    	string HomeTown_In,string PhoneNo_In,
    	string Gender_In,int Year_In,int TotalCredit_In,
    	float Gpa_In,string Major_In)
    
    	{
    		Student_Id=Student_Id_In;
    		Name = Name_In;
    		HomeTown= HomeTown_In;
    		PhoneNo=PhoneNo_In;
    		Gender=Gender_In;
    		Year= Year_In;
    		TotalCredit = TotalCredit_In;
    		 Gpa= Gpa_In;
    		Major=Major_In;
    
    	}
    
    
    
    	   void	student::OutputData()
    		{
    
    		cout<<Student_Id<<endl;
    		cout<<Name<<endl;
    		cout<<HomeTown<<endl;
    		cout<<PhoneNo<<endl;
    		cout<<Gender<<" "<<Year<<" "<<Gpa<<endl;
    		cout<<Major;
    		system("Pause");
    		}
    
    	  int student::GetStudentId()
    	   {
    	   return Student_Id;
    	   }
    
    	   string student::GetStudentName()
    	   {
    		   return Name;
    	   }
    
    #endif
    I tried to make some headway in Course Class:
    Code:
    #include <iostream>
    #include<vcl.h>
    #include <fstream>
    #include<string>
    #include<stdlib>
    #include "Student.h"
    
    using namespace std;
    
    class Course{
    
    	private:
    		int Student_Id;
    		string Name;
    
    		student *s[500];
    		int num_students;
    
    	public:
    		Course();
    		Course(int Student_Id,string Name);
    
    
    		void addStudent(student *s[]);
    
    };
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    [code=cpp]
    class Course{

    private:
    int Student_Id;
    string Name;

    // student *s[500];///dont have like this
    //use this
    student *s;
    int num_students;

    public:
    Course();
    Course(int Student_Id,stri ng Name);


    // void addStudent(stud ent *s[]);
    ///change the previous one to
    void addStudent(stud ent *s);

    };
    [/code]
    What doubt you have now?
    Initialize the course object
    Allocate memory for the variable s
    start storing the elements one by one

    Raghuram
    Last edited by gpraghuram; Feb 19 '08, 02:42 AM. Reason: Code change

    Comment

    • indiaj0nes
      New Member
      • Feb 2008
      • 3

      #3
      Thanks...

      I also intialized using a constructor like below:
      Code:
      Course::Course(int Student_Id,string Name)
      {
      	this->Student_Id = Student_Id;
      	this->Name = Name;
         //	students = NULL;
      	num_students = 0;
      }

      Now, I also tried to access the objects of the student class inside this function but it throws an error like

      [C++ Error] course.h(50): E2288 Pointer to structure required on left side of -> or ->*


      Code:
      void Course::addStudent(student *s)
      {
      
      cout<<"Output from Course Object"<<endl;
      cout<<"Student ID"<<s[0]->Student_Id;
      
      }

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by indiaj0nes
        Thanks...

        I also intialized using a constructor like below:
        Code:
        Course::Course(int Student_Id,string Name)
        {
        	this->Student_Id = Student_Id;
        	this->Name = Name;
           //	students = NULL;
        	num_students = 0;
        }

        Now, I also tried to access the objects of the student class inside this function but it throws an error like

        [C++ Error] course.h(50): E2288 Pointer to structure required on left side of -> or ->*


        Code:
        void Course::addStudent(student *s)
        {
        
        cout<<"Output from Course Object"<<endl;
        cout<<"Student ID"<<s[0]->Student_Id;
        
        }
        it should be s[0].Student_Id and not
        s[0]->Student_Id

        Raghuram

        Comment

        • indiaj0nes
          New Member
          • Feb 2008
          • 3

          #5
          Originally posted by gpraghuram
          it should be s[0].Student_Id and not
          s[0]->Student_Id

          Raghuram
          I am supposed to be using Array of Pointers to student Objects hence i was doing s[0]->Student_Id.


          Also another error i get is the course class not being able to access s[0]->Student_Id because Student_Id is declared as Private??

          Also how do i call it in the main class? like this?

          for (i = 0; i < Total; i++) {
          course.addStude nt(&s);
          }

          Comment

          Working...