frontier psychiatrist frontier psychiatrist - 1 month ago 5
C++ Question

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



#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?

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?

Answer

I think that this

CAverage(int);

and this

CGroup(int, int);

call the constructors? Or, are they themselves the constructors?

Your second presumption is correct, both are constructors.

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

This snippet initializes the variable VSubCount within the superclass.

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

This is a little more complex, and shows key concepts of inheritance.

: CAverage(VOther)

Is calling the parent constructor, to initialize the private member VSubCount, in CAverage, since CGroup cannot access it.

VClassMembers = VInteger;

initializes the member VClassMembers in the subclass. Otherwise, your comments are correct.

Comments