473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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
*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 1811
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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...
5
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...
10
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...
2
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...
19
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...
22
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...
3
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...
8
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...
6
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"...
6
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.