Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Classes (http://www.go4expert.com/forums/classes-t16905/)

xyzabc 14Apr2009 01:22

Classes
 
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;
}


xpi0t0s 14Apr2009 20:58

Re: Classes
 
>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);
//...
};


xyzabc 15Apr2009 02:39

Re: Classes
 
Quote:

Originally Posted by xpi0t0s (Post 45677)
>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);
//...
};


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.

xyzabc 15Apr2009 20:42

Re: Classes
 
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;
}


xpi0t0s 15Apr2009 21:35

Re: Classes
 
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?

xyzabc 16Apr2009 09:17

Re: Classes
 
Quote:

Originally Posted by xpi0t0s (Post 45732)
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?

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?

xpi0t0s 16Apr2009 13:03

Re: Classes
 
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) ;
            }
//...
};


xyzabc 17Apr2009 08:56

Re: Classes
 
Quote:

Originally Posted by xpi0t0s (Post 45758)
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) ;
            }
//...
};


That makes more sense thanks for explaining using my example. I appreciate all the help you give.


All times are GMT +5.5. The time now is 17:20.