Explanation about Constructors and Inheritence

Recently, I started working with classes and, today, class inheritance. I created a simple program to expand my perception of inheritance. The program calculates the average grade of a class. I understand the vast majority of the code I have written, but there are some exceptions (listed below the code). Any and all help would be appreciated.

Code
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "stdafx.h"
#include <iostream>

using namespace std;

class CAverage {
    private:
        double VSubCount, VAverage, VMark, VSum, VNum;
    public: CAverage(int); // Constructor.
        void MTake_action() {
            MAsk_input(); // Calls the method “MAsk_input()“ within this class.
            MCalculate_average(); // Calls the method “MCalculate_average()“ within 
                                  // this class.
            MPrint_result(); // Calls the method “MPrint_result()“ within this class.
        }

        void MCalculate_average() {
            VAverage = VSum / VNum;
        }

        void MAsk_input() {
            VSum = 0;
            VNum = 0;
            int VNumber;
            for (int i = 0; i < VSubCount; i++) {
                cout << "Enter your " << i + 1 << " mark: ";
                cin >> VNumber;
                if (VNumber > 0) {
                    VMark = VNumber;
                    VSum += VMark;
                    VNum++;
                }
            }
        }

        void MPrint_result()
        {
            system("cls");

            if (((VSum / 3) <= 0) || ((VSum / 3) > 10)) {
                cout << "Incorrect input." << endl;
            } else {
                cout << "Average: " << VAverage << endl;
            }
        }
};

// Creates a child class and makes that this class could view/get public methods,
// variables, etc of “CAverage“.
class CGroup : public CAverage {
    private:
        int VClassMembers;
        void MAsk_input() {
            for (int i = 0; i < VClassMembers; i++) {
                system("cls");

                cout << "[" << i + 1 << " student]" << endl;

                CAverage::MAsk_input(); // Calls the method “MAsk_input()“ within
                                        // the parent class (“CAverage“).
            }
        }
    public: CGroup(int, int);
        void MTake_action() {
            MAsk_input(); // Calls the method “MAsk_input()“ within this class.
            CAverage::MCalculate_average(); // Calls the method “MCalculate_average()“
                                            // within the parent class (“CAverage“).
            CAverage::MPrint_result(); // Calls the method “MPrint_result()“ within the
                                       // parent class (“CAverage“).
        }
};

CAverage::CAverage(int VSubjectCount) {
    VSubCount = VSubjectCount;
}

CGroup::CGroup(int VOther, int VInteger) : CAverage(VOther) {
    VClassMembers = VInteger;
}

int main() {
    CGroup avg(2, 5); // Creates an object, named “avg“.

    avg.MTake_action(); // Calls the child classes' method “MTake_action()“.

    return 0;
}

So, how would one explain these parts?
1
2
3
4
5
6
7
CAverage::CAverage(int VSubjectCount) {
    VSubCount = VSubjectCount;
}

CGroup::CGroup(int VOther, int VInteger) : CAverage(VOther) {
    VClassMembers = VInteger;
}

I think that this
 
CAverage(int);

and this
 
CGroup(int, int);

call the constructors? Or, are they themselves the constructors?
And, are all of the comments, made by me, correct?
Last edited on
CAverage(int); (line 9, inside the definition of class CAverage) declares, but doesn't define a constructor for that class taking a single int as an argument. Later on you're defining it at line 73.

it would be more natural to write those definitions properly:
1
2
3
4
5
6
7
CAverage::CAverage(int VSubjectCount), VSubCount(VSubjectCount)
{
}

CGroup::CGroup(int VOther, int VInteger) : CAverage(VOther),  VClassMembers(VInteger)
{
}


That's just the syntax of constructor definition: bases and members are listed after the colon and before the opening brace, each followed by () or {} containing the arguments to initialize that member-or-base with: http://en.cppreference.com/w/cpp/language/initializer_list
Topic archived. No new replies allowed.