关于构造函数和继承的解释



最近,我开始研究类,今天是类继承。我创建了一个简单的程序来扩展我对继承的理解。该程序计算一个班级的平均成绩。我理解我所编写的绝大多数代码,但也有一些例外(在代码下面列出)。我将不胜感激。

#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;
}

那么,如何解释这些部分呢?

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

我认为这

CAverage(int);

CGroup(int, int);

调用构造函数?或者,他们自己就是构造者吗?

我的评论都对吗?

我认为这

CAverage(int);

CGroup(int, int);

调用构造函数?或者,他们自己就是构造者吗?

你的第二个假设是正确的,它们都是构造函数。

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

这个代码段初始化超类中的变量VSubCount

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

这有点复杂,并显示了继承的关键概念。

: CAverage(VOther)

调用父构造函数,初始化私有成员VSubCount,因为CGroup不能访问它。

VClassMembers = VInteger;

初始化子类中的成员VClassMembers。否则,你的评论是正确的。

最新更新