C++ passing multiple array to function sum using inheritance

questions is: write a program using inheritance allow user to enter grades of his students 5~8 students as a base class and compute the sums for each students in derived class and compute the average of sums in another derived class

i created 3 classes in 1 header file and 1 cpp file how ever i cant seem to get the sum or the average to show up on execution time

header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#ifndef GRADES_H_INCLUDED
#define GRADES_H_INCLUDED

class Grades
{
public:
	 Grades()
	{
		setGrades();
	}
	void getGrades()
	{
		cout<<"No\t exam1\t exam2\n";
		for(int i=0;i<size;i++)
		{
			cout<<"students"<<i+1<<":\t";
			for(int j=0;j<exams;j++)
				cout<<a[i][j]<<"\t";
			cout<<endl;
		};}
protected:
	static const int size=5;
	static const int exams=2;
	int a[size][exams];
	void setGrades()
	{
		for(int i=0;i<size;i++)
		{
			cout<<"enter the grade of the student"<<i+1<<":\n";
			for(int j=0;j<exams;j++)
			{
				cout<<"exam"<<i+1<<":";
				cin>>a[i][j];
			}
		}
	}
};
class GradesSum : public Grades {
public:
	GradesSum(){
		theSum=0;
		getSum();
	}
	void getSum(){
		for (int i=0;i<5;i++)
			theSum+=a[i][exams];
	}
	int printSum(){
		return theSum;
	}

protected:
	int theSum;
};

class GradesAve: public GradesSum{
public:
double TheAverage()
	{
		return static_cast<double>(theSum)/5;
	}
};


#endif 


main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
#include "Grades.h"
	int main()
	{
		Grades st_Grade;
		st_Grade.getGrades();
		GradesSum GSum;
		Grades * ppoly1 = &GSum;
		cout<<"sum"<<GSum.printSum()<<'\n';
		GradesAve st_GAve;
		st_GAve.TheAverage();

		return 0;
	}



ok let me see. First off, you have no need for subtype polymorphism in the main. Second GSum did not initialize sum, because at the getSum function you are adding the value of a[1][sum] to theSum, but the a[1][exam has no value].

I suggest adding this to Grades

1
2
3
4
5
public:
           Grades(Grades &obj)//This is a copy constructor
           {
                   *this=obj;//This instance of the class will take on the state of another
           }


Then in GradesSum
1
2
3
4
         GradesSum(Grades &obj):Grades(obj)
         {
                getSum();
          }

In GradesAve
1
2
3
4
        GradesAve(GradesSum &obj)
        {
             *this=obj;
        }

In main
1
2
3
4
5
6
   Grades st_Grade();
   st_Grade.getGrades();
   GradesSum gSum(st_Grade);
   cout<<"sum "<<gSum.printSum()<<endl;
   GradesAve st_GAve;
   cout<<st_GAve.TheAverage()<<endl;;


I haven't tested this, but it should work. However there are far more effective methods. In all honesty you also need to work on your naming conventions.
Last edited on
Grades.h, Line 47: You'r accessing a non existing array item. exams is pointing to the first item behind your array a[i].

main.cpp, Lines 6, 8 and 11: st_Grade, GSum and st_Gave are different and completely unrelated objects.

main.cpp, Line 9: Please do explain this statement.


Some tips:

Grades.h, Line 12: The name of your method indicates a getter method but it doesn't return any value but prints them on stdout.

Grades.h, Line 49: The name of your method indicates a printing method but it's just a getter!
Topic archived. No new replies allowed.