473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<vcl.h>
  3. #include <fstream>
  4. #include<string>
  5. #include<stdlib>
  6. #ifndef STUDENT_H
  7. #define STUDENT_H
  8.  
  9.  
  10. //#define FILE_SIZE 102400;
  11.  
  12. using namespace std;
  13.  
  14. class student
  15. {
  16.     private:
  17.     int Student_Id;
  18.     string Name;
  19.     string HomeTown;
  20.     string PhoneNo;
  21.     string Gender;
  22.     int Year;
  23.     int TotalCredit;
  24.     float Gpa;
  25.     string Major;
  26.  
  27.     public:
  28.  
  29.     student();
  30.     student(int Student_Id, string Name);
  31.  
  32.     void change(char *r,char *t);
  33.     float modify(int s,float g);
  34.     int SetStudentInfo(int Student_Id_In,string Name_In,
  35.     string HomeTown_In,string PhoneNo_In,
  36.     string Gender_In,int Year_In,int TotalCredit_In,
  37.     float Gpa_In,string Major_In );
  38.     void OutputData();
  39.     int GetStudentInfo();
  40.     int GetStudentId();
  41.     string GetStudentName();
  42.  
  43.  };
  44.  
  45.  
  46. //Constructor
  47.  
  48. student::student()
  49. {
  50.  Student_Id = NULL;
  51.  Name = " ";
  52.  HomeTown = " ";
  53.  PhoneNo = " ";
  54.  Gender = " ";
  55.  Year = NULL;
  56.  TotalCredit = NULL;
  57.  Gpa = NULL;
  58.  Major  = " ";
  59.  
  60. }
  61.  
  62. student::student(int Student_Id,string Name)
  63. {
  64. this->Student_Id = Student_Id;
  65. this->Name = Name;
  66.  
  67. }
  68.  
  69.  student::SetStudentInfo(int Student_Id_In,string Name_In,
  70.     string HomeTown_In,string PhoneNo_In,
  71.     string Gender_In,int Year_In,int TotalCredit_In,
  72.     float Gpa_In,string Major_In)
  73.  
  74.     {
  75.         Student_Id=Student_Id_In;
  76.         Name = Name_In;
  77.         HomeTown= HomeTown_In;
  78.         PhoneNo=PhoneNo_In;
  79.         Gender=Gender_In;
  80.         Year= Year_In;
  81.         TotalCredit = TotalCredit_In;
  82.          Gpa= Gpa_In;
  83.         Major=Major_In;
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.        void    student::OutputData()
  90.         {
  91.  
  92.         cout<<Student_Id<<endl;
  93.         cout<<Name<<endl;
  94.         cout<<HomeTown<<endl;
  95.         cout<<PhoneNo<<endl;
  96.         cout<<Gender<<" "<<Year<<" "<<Gpa<<endl;
  97.         cout<<Major;
  98.         system("Pause");
  99.         }
  100.  
  101.       int student::GetStudentId()
  102.        {
  103.        return Student_Id;
  104.        }
  105.  
  106.        string student::GetStudentName()
  107.        {
  108.            return Name;
  109.        }
  110.  
  111. #endif
I tried to make some headway in Course Class:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<vcl.h>
  3. #include <fstream>
  4. #include<string>
  5. #include<stdlib>
  6. #include "Student.h"
  7.  
  8. using namespace std;
  9.  
  10. class Course{
  11.  
  12.     private:
  13.         int Student_Id;
  14.         string Name;
  15.  
  16.         student *s[500];
  17.         int num_students;
  18.  
  19.     public:
  20.         Course();
  21.         Course(int Student_Id,string Name);
  22.  
  23.  
  24.         void addStudent(student *s[]);
  25.  
  26. };
Feb 19 '08 #1
4 2700
gpraghuram
1,275 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. class Course{
  2.  
  3.     private:
  4.         int Student_Id;
  5.         string Name;
  6.  
  7. //        student *s[500];///dont have like this
  8. //use this
  9.                                  student *s;
  10.         int num_students;
  11.  
  12.     public:
  13.         Course();
  14.         Course(int Student_Id,string Name);
  15.  
  16.  
  17.     //    void addStudent(student *s[]);
  18. ///change the previous one to
  19. void addStudent(student *s);
  20.  
  21. };
  22.  
What doubt you have now?
Initialize the course object
Allocate memory for the variable s
start storing the elements one by one

Raghuram
Feb 19 '08 #2
Thanks...

I also intialized using a constructor like below:
Expand|Select|Wrap|Line Numbers
  1. Course::Course(int Student_Id,string Name)
  2. {
  3.     this->Student_Id = Student_Id;
  4.     this->Name = Name;
  5.    //    students = NULL;
  6.     num_students = 0;
  7. }

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 ->*


Expand|Select|Wrap|Line Numbers
  1. void Course::addStudent(student *s)
  2. {
  3.  
  4. cout<<"Output from Course Object"<<endl;
  5. cout<<"Student ID"<<s[0]->Student_Id;
  6.  
  7. }
Feb 19 '08 #3
gpraghuram
1,275 Expert 1GB
Thanks...

I also intialized using a constructor like below:
Expand|Select|Wrap|Line Numbers
  1. Course::Course(int Student_Id,string Name)
  2. {
  3.     this->Student_Id = Student_Id;
  4.     this->Name = Name;
  5.    //    students = NULL;
  6.     num_students = 0;
  7. }

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 ->*


Expand|Select|Wrap|Line Numbers
  1. void Course::addStudent(student *s)
  2. {
  3.  
  4. cout<<"Output from Course Object"<<endl;
  5. cout<<"Student ID"<<s[0]->Student_Id;
  6.  
  7. }
it should be s[0].Student_Id and not
s[0]->Student_Id

Raghuram
Feb 19 '08 #4
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.addStudent(&s);
}
Feb 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real...
1
by: Robert | last post by:
I have two tables (classes and students). Right now, I'm generating a report in asp that shows a list of classes, the enrollment for each class, and how many seats are available (see query below)....
2
by: cm500 | last post by:
I'm very new to databases so bear with me. What I need is a way to track the training for the employees at my firm. I have 40 classes that I will be teaching on various subjects and various...
2
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen...
9
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
5
by: QbProg | last post by:
Hello, I did some experiments with VC++ 2005, and some ILDasm. Please tell me if I have understood the concepts of C++ programming under .NET, since I'm a bit confused! -- There are 4 types of...
9
by: Chrissy | last post by:
I took a C# class as an elective and received an incomplete in it and am desparate for help. I have two assignments left (arrays and inheritance) and would gladly pay anyone that can assist me with...
6
by: Minor | last post by:
I am a beginning student in java and really need some help. I am totally lost and don't know where to begin. This is my last assignment and I just need to submit something. I have decided computer...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.