Classes

Discussion in 'C++' started by xyzabc, Apr 13, 2009.

  1. xyzabc

    xyzabc New Member

    Joined:
    Apr 13, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys I just found this forum and hopefully can get a little help. I am taking a c++ course and have an assignment due this week that I need a little advice on. I will list the problem and my attempt. Thanks in advance!!!

    I wanted to know if the constuctor is coded correctly considering the following problem. Also, since I need to declare the member functions could someone please give me an example of how to declare the function using the class Student.

    The problem:
    a) Construst a class named Student consisting of an integer student identification number,an array of five double precision grades,and an integer representing the total number of grades entered. The constructor for this class should initialize all Student data members to zero. Included in the class should be member functions to (1)enter a student id,(2)enter a single test grade and update the total number of grades entered,(3)compute an average grade and display the student ID followed by the average grade.

    b) Include the class constructed in part a within the context of a complete program.Your program should declare two objects of type Student and accept and display data for the two objects to verify operation of the member functions.




    Code:
    #include<iostream>
    using namespace std;
     
    class Student
    {
          private:
                  int stidnum;
                  double grades[5];
                  int totnumgrades;
          public:
                 Student(int=0,double[5]=0,int=0);
                 int studentid(int);
                 double testgrade(double);
                 double average(double);
     
    };
     
    Student::Student(int stidnum,double grades[5],int totnumgrades)    //constructor
    {     
           stidnum = 0;
           grades[5]=0;
           totnumgrades=0;
     
    }
     
    double testgrade()
    {
     
     
     }
     
    double average()
    {
     
     
    }
     
     
     
     
    int main()
    {
     
       system("pause");
        return 0;
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    >I wanted to know if the constuctor is coded correctly

    No, because the assignments to stidnum, grades and totnumgrades will assign values to the arguments passed in, and I think your intent is to initialise the member attributes.

    If we rewrite the constructor as:
    Code:
    Student::Student(int quux,double gronk[5],int snert)
    {     
    }
    
    then would you assign to quux, gronk and snert, or still to stidnum, grades and totnumgrades, and if the latter three, why?

    > Also, since I need to declare the member functions could someone please give me an example of how to declare the function using the class Student.

    What's wrong with the ones you've already got? i.e.:
    Code:
    class Student
    {
    //...
                 int studentid(int);
                 double testgrade(double);
                 double average(double);
    //...
    };
    
     
  3. xyzabc

    xyzabc New Member

    Joined:
    Apr 13, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Thank you xpi0t0s! The problem with the functions is that I do not understand how to use the functions without having to redeclare variables inside of each function. I want to be able to use the class Students globally I think.
     
  4. xyzabc

    xyzabc New Member

    Joined:
    Apr 13, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Ok I have gotten my program to do almost what it is supposed to do. I am having a problem with averaging the numbers in the array. please can anyone explain why it is not averaging correctly. Here is my code so far.

    Code:
    #include<iostream>
    using namespace std;
    class Student
    {
      private:
                  int idnum;
                  double grades[5];
                  int totnumgrades;
      public:    
                 Student(int=0,double[5]=0,int=0);
                 int studentid();
                 double testgrade();
                 double average();
                 
    };
    Student::Student(int stidnum,double grades[5],int totnumgrades)    //constructor
    {     
                  
    }
    int Student::studentid()
    {
       
        cout<<"Please enter your student id number\n"<<endl;
        
        cin>>idnum;
        
        return idnum;
    }
    double Student::testgrade()
    {
         int i;
         
         cout<<"\nPlease enter a single test grade:\n "<<endl;
         cin>>grades[1];
         
         return grades[1];
    }
    double Student::average()
    {
        int i;
        int sum=0;
        int average=0;
        
        cout<<"\nPlease enter four more grades to calculate your average"<<endl;
        
        for(i=1;i<5;i++)
        {
         cin>>grades[i];
        }
        
        for (i=0;i<5;i++)
         {
          sum+=grades[i];
          average = sum/5;             
         
         }
         
         return average;
         
         
    }
     
    int main()
    {
        Student a;
        Student b;
        
        
        
        cout<<"\nThe student id number entered is:\n"<<a.studentid()<<endl;
        
        cout<<"\nThe test grade you entered has been updated as:  \n"<<b.testgrade()<<endl;
        
        cout<<"\nThe average of the grades entered is\n"<<b.average()<<endl;
        
        
      
        
        
        
        
        system("pause");
        return 0;
    }
    
                 
    
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    In Student::testgrade() you need to read in and return grades[0].

    Still the strange constructor. What do you want it to do? Should it be doing nothing at all, which is what the code will currently do, or should it be initialising the Student attributes?
     
  6. xyzabc

    xyzabc New Member

    Joined:
    Apr 13, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Well the problem says to just initialize all the Student class values to 0. I guess I am just a little confused on how to use the constructor properly. What exactly even is the point of a constructor? Why cant you just initialize all the values to 0 without doing this method?
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    A constructor constructs (i.e. initialises) the object, and the point of a ctor is to do that automatically rather than you having to do that manually.

    So the line
    Code:
    Student a;
    
    will automatically call a.Student() for you. Here's how to write a constructor that initialises Student's attributes to zero:

    Code:
    class Student
    {
      private:
                  int idnum;
                  double grades[5];
                  int totnumgrades;
      public:    
                 Student()
                 {
                   idnum=0;
                   totnumgrades=0;
                   for (int i=0; i<5; grades[i++]=0) ;
                 }
    //...
    };
    
     
  8. xyzabc

    xyzabc New Member

    Joined:
    Apr 13, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    That makes more sense thanks for explaining using my example. I appreciate all the help you give.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice