CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    1

    input from a file and storing in array of objects

    hi..i m trying to solve an assignment for my own practice with FILEing in c++.
    i have found the assignment here (due date was 27th march, so its not my school assignment, only practice)

    http://www.cs.fsu.edu/~gaitrosd/clas...ent5/hw05.html


    1-i have to make a class student with three derived classes english, history, math with a function that calculates avg.

    2-read the data from an input file and store it using an array of appropriate type.(polymorphism)
    first line of input file has first and last name.
    2nd line of input file has subject name and marks.

    3-then output file...

    how can i read input file to store data in appropriate object??

    this is what i made..i m confused and need help.

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;

    ////////////////////////////////////////////////////////////

    class student
    {

    protected:
    string fname,lname,course;

    public:

    student (string f,string l,string c): fname(f),lname(l),course(c)
    {}

    };

    ////////////////////////////////////////////////////////////////

    class english : public student
    {
    private:
    int atten,project,midterm,fexam;
    public:

    english(string n1,string n2, string s, int a, int p, int m, int f):
    atten(a),project(p),midterm(m),fexam(f),student(n1,n2,s)
    {}

    string get_fname() const
    {return fname;}

    string get_lname() const
    {return lname;}

    string get_course() const
    {return course;}

    float avg ()
    {return (atten*10/100+project*30/100+midterm*30/100+fexam*30/100);}
    };

    /////////////////////////////////////////////////////////////////

    class history : public student
    {
    private:
    int term,mid,final;
    public:
    history (string n1,string n2, string s, int t, int m, int f):
    term(t),mid(m),final(f),student(n1,n2,s)
    {}

    string get_fname() const
    {return fname;}

    string get_lname() const
    {return lname;}

    string get_course() const
    {return course;}

    float avg ()
    {return (term*25/100+mid*35/100+final*40/100);}
    };

    /////////////////////////////////////////////////////////////////

    class math : public student
    {
    private:
    int quiz1,quiz2,quiz3,quiz4,quiz5,t1,t2,final;
    public:
    math (string n1,string n2, string s, int q1, int q2, int q3, int q4, int q5, int t1, int t2, int f):
    quiz1(q1),quiz2(q2), quiz3(q3), quiz4(q4), quiz5(q5),student(n1,n2,s)
    {}

    string get_fname() const
    {return fname;}

    string get_lname() const
    {return lname;}

    string get_course() const
    {return course;}

    float avg ()
    {return ((quiz1+quiz2+quiz3+quiz4+quiz5)*15/100 + t1*25/100 + t2*25/100+final*35/100);}
    };

    ///////////////////////////////////////////////////////////////////

    int main ()
    {
    char fileName[30];

    cout<<"Enter File Name: ";
    cin>>fileName;

    string name1,name2,sub;
    int num,a,p,m,f;

    ifstream fin(fileName);

    if (fin.fail())
    {cout<<"Sorry, Input File not found.."<<endl;exit (1);}

    fin.get(num);
    fin.get(

    fin.close();

    return 0;
    }

  2. #2
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: input from a file and storing in array of objects

    From the assignment description, it is quite clear that the instructor is referring to arrays containing base class pointers.
    To know which Student object to create (i.e. History, English, etc), you need to read the subject string and decide. You can pass the parameters needed for that Student Object to calculate average in its Constructor something like:
    EnglishStudent( int firstSemGrade, int homeworkGrade ) etc
    You need to allocate this object using new, of course and store its pointer in the Student array.
    One you have this array ready, then comes calculating averages.
    To calculate average, you can provide a virtual pure function in Student class like
    virtual int CalcAvg() = 0; And you need to override it in the derived classes to implement the specific average calculating algorithm.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: input from a file and storing in array of objects

    Try using copy and insert iterator together.
    Thanks for your help.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: input from a file and storing in array of objects

    english, math and history looks like types of course, not types of student.

    public inheritance means "is a kind of".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured