Thread: Classes and objects

  1. #1
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20

    Classes and objects

    Need a help please. I have a program calculating average of students grades:

    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    
    using namespace std;
    
    
    const int TOTAL_STUDENTS = 10;                
    const int TOTAL_GRADES = 10;                 
    
    
    void readInput(fstream *file, string[], int grades[][TOTAL_GRADES]);          
    void calcAvg(string names[], int grades[][TOTAL_GRADES], float average[]);    
    void printData(string names[], int grades[][TOTAL_GRADES], float average[]);  
    
    
    int main()
    {
      string names[TOTAL_STUDENTS];               
      int grades[TOTAL_STUDENTS][TOTAL_GRADES];  
      float average[TOTAL_STUDENTS];             
      fstream inputfile;                          
      string fileName;                            
    
    
      cout << "Hello there!" << endl;
      cout << "This is a program to calculate students' average test scores" << endl;
      cout << "-------------------------------------------------------------" << endl;
    
    
      cout << "Now, enter the file name: ";   
      cin >>  fileName;
      cout << endl;
    
    
        inputfile.open(fileName.c_str());
    
    
        if(inputfile.is_open())
        {
            readInput(&inputfile, names, grades);
            calcAvg(names, grades, average);
            printData(names, grades, average);
        }
        else
      {
       cout << "Please enter a correct file name or make sure file is in the same directory as the program." << endl;
    
    
       return -1;
      }
    
    
      inputfile.close();
    
    
      return 0;
    }
    
    
    void readInput(fstream *file, string names[], int grades[][TOTAL_GRADES])
    {
      for(int i = 0; i < TOTAL_STUDENTS; i++)
        {
         *file >> names[i];
       for(int j = 0; j < TOTAL_GRADES; j++)
         {
          *file >> grades[i][j];
         }
        }
    }
    
    
    void calcAvg(string names[], int grades[][TOTAL_GRADES], float average[])
    {
        float sum;
        for(int i = 0; i < TOTAL_STUDENTS; i++)
        {
            sum = 0;
            for(int j = 0; j < TOTAL_GRADES; j++)
            {
          sum += grades[i][j];
            }
            sum = (float)sum/TOTAL_GRADES;
            average[i] = sum;
        }
    }
    
    
    void printData(string names[], int grades[][TOTAL_GRADES], float average[])
    {
        for(int i = 0 ; i < TOTAL_STUDENTS; i++)
        {
            cout << names[i] << " ";
            for(int j = 0; j < TOTAL_GRADES; j++)
            {
                cout << grades[i][j] << " ";
            }
            cout << fixed << showpoint << setprecision(2) << average[i] << endl;
        }
    }
    I need to re-write this program in classes. I have header file:
    Code:
    #ifndef STUDENTTYPE_H
    #define STUDENTTYPE_H
    #include <iomanip>
    
    
    const int TOTAL_STUDENTS = 10;
    const int TOTAL_GRADES = 10;
    
    
    class StudentType
    {
      public:
        StudentType();
        readInput(ifstream&):void;
        calcAvg():void;
        printData(ostream&) const:void;
      private:
        string names;
        int grades[TOTAL_GRADES];
        float average;
    };
    
    
    #endif // STUDENTTYPE_H
    I am OK with main program. But I don't know how the implementation file should look like. Any suggestions please?
    Code:
    #include "StudentType.h"
    #include <iostream>
    
    
    
    
    StudentType::StudentType()
    {
      name = "";
      for (int i=0; i<TOTAL_STUDENTS; i++)
      {
        grades[i]={0};
      }
      average = 0.0;
    }
    ??????
    Thank you

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have to implement the functions (actually you have almost the body of them already).
    As you implemented the constructor , you shall do the other functions.
    I mean you write
    Code:
    StudentType::functionName(parameterList)
    {
          /*body of the function*/
          /*because of the operator :: you get access to the private fields of StudentType class*/
    }
    I hope that I understood the problem and this helps

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest overloading operator>> and operator<< for I/O instead:
    Code:
    class StudentType
    {
    public:
        StudentType();
        void calculateAverage();
        friend std::istream& operator>>(std::istream& in, StudentType& student);
        friend std::ostream& operator<<(std::ostream& out, const StudentType& student);
    private:
        std::string name;
        int grades[TOTAL_GRADES];
        float average;
    };
    Take a look at your previous implementation of readInput. You have this part to read the data for one student:
    Code:
    *file >> names[i];
    for(int j = 0; j < TOTAL_GRADES; j++)
    {
        *file >> grades[i][j];
    }
    So, all you need to do is to implement operator>> using the same thing, e.g.,
    Code:
    std::istream& operator>>(std::istream& in, StudentType& student)
    {
        in >> student.name;
        for (int i = 0; i < TOTAL_GRADES; ++i)
        {
            in >> student.grades[i];
        }
        return in;
    }
    This way, your readInput function becomes:
    Code:
    void readInput(fstream& file, StudentType students[])
    {
        for (int i = 0; i < TOTAL_STUDENTS; ++i)
        {
            file >> students[i];
        }
    }
    Notice that I made file an fstream& instead of an fstream*.

    Likewise, your calcAvg function will loop over the students and call the calculateAverage member function for each of them (or you can change it such that operator>> will call calculateAverage), and then your printData function will loop over the students and use the operator<< to print each of them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    Thank you, guys. It did help a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and objects
    By GoldenBoy in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2010, 07:28 AM
  2. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 08:02 AM
  3. classes and objects
    By mixalissen in forum C++ Programming
    Replies: 21
    Last Post: 05-08-2008, 10:18 PM
  4. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 03:19 PM
  5. Classes and objects
    By Drainy in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2005, 11:23 PM

Tags for this Thread