By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Using Inheritance in C++

P: n/a
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[i];
}
//verify
for (int i=0;i<nNumGrades; i++)
{
cout << g[i]<< ",";
}

int total = 0;
for (int i=0; i<nNumGrades;i++)
{
total += g[i];
}
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;
}


Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
NotEnough wrote:
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: pe***@writeopen.com
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(--)
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.