469,889 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

Using Inheritance in C++

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
1 1727
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.

Similar topics

4 posts views Thread by Dave Theese | last post: by
5 posts views Thread by Morgan Cheng | last post: by
10 posts views Thread by davidrubin | last post: by
2 posts views Thread by Tony Johansson | last post: by
19 posts views Thread by Kamilche | last post: by
22 posts views Thread by Matthew Louden | last post: by
8 posts views Thread by Titatoutati | last post: by
6 posts views Thread by burningodzilla | last post: by
6 posts views Thread by Bart Simpson | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.