Using Inheritance in C++

N

NotEnough

I have a program assigment due next week. I have a a simple c++ grade
average program below. How do I turn it into an inheritance. Where Grade
is the base class. And Quiztest - Project - LabTest - TestGrade are four
devired class.

/*-------------------------------------------------------------------------------
*File Name: ReadTest.cpp
*Abstract: Implement a class to process a list of grades
*Student:
*Date:
*------------------------------------------------------------------------------*/

#include<iostream>
#include<fstream>
#include<iomanip>

using std::setprecision;
using namespace std;
const int MAXGRADE = 20;

main()
{
ifstream ifl; //input file stream
int g[MAXGRADE];
int nNumGrades;
char junk[30];

//open student grade file
ifl.open("StudentRecord.txt");
if (ifl == NULL)
{
cout << "Could not open the file\n";
exit(0);
}
// loop to run through only the ten student grades
for (int k=0;k<10; k++)
{
ifl >> junk;
cout << junk<< " ";
ifl >> junk;
cout << junk <<endl;

//this loop goes through the 4 groups of grades
for (int j=0;j<4; j++)
{
ifl >> nNumGrades;
cout << nNumGrades <<endl;


//this loop prints each grade.
for (int i=0;i<nNumGrades; i++)
{
ifl >> g;
}
//verify
for (int i=0;i<nNumGrades; i++)
{
cout << g<< ",";
}

int total = 0;
for (int i=0; i<nNumGrades;i++)
{
total += g;
}
cout<< "the total of your grades is "<<total<<endl;

//Average calculation
double dAve = static_cast<double> (total)/nNumGrades;
cout<< " Average: "<<setprecision(2)<<fixed<<dAve<<endl;

cout << endl;

}
}

return 0;
}
 
P

Peter MacMillan

NotEnough said:
I have a program assigment due next week. I have a a simple c++ grade
average program below. How do I turn it into an inheritance. Where Grade
is the base class. And Quiztest - Project - LabTest - TestGrade are four
devired class.

A class is a container for code and data. Inheritance allowes class to
borrow from (inherit) other classes. A base class is the parent of a set
of classes.

eg.
//----
#include <iostream>

using std::cout;
using std::endl;

class Base {
private:
int m_number;

public:
Base(): m_number(0) {}
~Base() {}

void setNumber(int value) { m_number = value; }
int getNumber() { return m_number; }
};

//
// notice how ExtendedBase "inherits" from the Base class
//

class ExtendedBase : public Base {
public:
ExtendedBase(int value) { setNumber(value); }
~ExtendedBase() {}

void sayNumber() {
cout << getNumber() << endl;
}
};

int main(int, char**) {
ExtendedBase x(42);
x.sayNumber();
return 0;
}

//----

So what you have to do for your assignment is figure out what those data
structures are supposed to represent, how they relate to the base class
(think: common shared attributes in the base, unique specialized
attributes in the inherited classes)...

HTH to get you started.

--
Peter MacMillan
e-mail/msn: (e-mail address removed)
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top