472,373 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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


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

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

//open student grade file
if (ifl == NULL)
cout << "Could not open the file\n";
// 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];
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 1776
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.

#include <iostream>

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

class Base {
int m_number;

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 {
ExtendedBase(int value) { setNumber(value); }
~ExtendedBase() {}

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

int main(int, char**) {
ExtendedBase x(42);
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--
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error...
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
by: Kamilche | last post by:
I have looked at many object-oriented programming frameworks out there for C. Though the ideas presented are intriguing, and I've used some of them in my own work, they all suffered some drawback...
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
by: Titatoutati | last post by:
Hi all, So, what I am trying do to is to manage some classes with javascript. It's working, but there are some things that I don't understand... Next is my sample code, and I have indicated the...
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.