dimanche 29 mars 2015

Is it possible to set the class functions in the two functions?


I don't know how to call my class functions into printData(Testscore&) and readData(TestScore).


Also, could someone tell me why my Average() isn't being called to the main? I just learned about using static member variables and static member functions and was wondering if I am using them incorrectly.


The readData function:



  1. Does not use copy constructor.

  2. Reads all instance variables like the student's names and all their grades.

  3. Uses functions to store the variables, name, element of each grade of array pointed to private pquiz, and static member grades of how many grades to read.


The printData function:



  1. Writes the name and average grade of the quizzes.

  2. Uses copy constructor.


This is my program so far:



#include <iostream>
#include <string>

using namespace std;

class TestScore {
private:
static int grades;
string name;
double *pquiz;
double average;
public:
TestScore();
~TestScore();
void setName(string);
static void setGrades(int);
void setPquiz(double *);
void setAverage(double);
string getName();
static int getGrades();
double getPquiz();
void readData(TestScore &);
void printData(TestScore);
double Average(double *, int);
static void Grade(int);
};

TestScore::TestScore() {
name="";
pquiz=new double[grades];
average=0;
}

void TestScore::setName(string name1) {
if(name1!="1") {
name=name1;
}
}

void TestScore::setPquiz(double *pquiz1) {
if(pquiz>=0) {
pquiz=pquiz1;
}
}

void TestScore::setGrades(int grades1) {
if(grades1>=0) {
grades=grades1;
}
}

void TestScore::setAverage(double average1) {
if(average1>=0) {
average=average1;
}
}

string TestScore::getName() {
return name;
}

int TestScore::getGrades() {
return grades;
}

double TestScore::getPquiz() {
return *pquiz;
}


double Average(double *pquiz, int grade) {
int count;
double total=0;
double average=0;
for(count=0; count<grade; count++) {
total+=pquiz[count];
}
average=total/grade;
return average;
}

void readData(TestScore&) {

}
void printData(TestScore) {

}

TestScore::~TestScore() {
delete [] pquiz;
pquiz=0;
}

int TestScore::grades=0;
void TestScore::Grade(int a) {
grades+=a;
}



int main() {
const int grades = 3;
const int students = 4;
TestScore exam;
string student;
int grade;
double *pquiz;
double average;
for(int i=0; i<students; i++) {
cout<<"Student "<<(i+1)<<": ";
cin>>student;
exam.setName(student);
cout<<endl;

for(int count=0; count<grades; count++) {
cout<<"Quiz "<<(count+1)<<": ";
cin>>pquiz[count];
exam.setPquiz(pquiz);
exam.getPquiz();

while(pquiz[count]<0) {
cout<<"Error, invalid test score, please try again."<<endl;
cout<<"Quiz "<<(count+1)<<": ";
cin>>pquiz[count];
}
}
exam.setAverage(average);
cout<<exam.getName()<<" average is "<<Average(pquiz, grade)<<endl<<endl;
}
readData(exam);
printData(exam);

return 0;
}



Aucun commentaire:

Enregistrer un commentaire