![]() |
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> |
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)> 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 |
Re: Classes
Quote:
|
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> |
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? |
Re: Classes
Quote:
|
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;Code:
class Student |
Re: Classes
Quote:
|
| All times are GMT +5.5. The time now is 22:00. |